Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jlrdw's avatar
Level 75

Nginx server_name but in WIndows 10

I cannot seem to get a server name to work in nginx. Local host is all that is working:

    listen 81; 
    listen [::]:81;

    server_name localhost;    #  This works

  #  Would like server_name site1.test;

And then the url will be:

http://localhost:81/some/route      // Works perfect with localhost

But I would like something like:

http://site1.test/some/route      // or site1.whatever here

And I did add the server name I want inside the host file:

127.0.0.1       site1.test

I am new to nginx, is there something I am missing? I have been over many stackoverflow and serverfallt post.

Or does a server_name only work on Linux?

Any ideas appreciated.

0 likes
7 replies
apex1's avatar
apex1
Best Answer
Level 12

"You need to know your server’s public IP address and the domains you want to route to the server. Assuming that my server’s public IP address is 203.0.113.5, the lines I would add to my file would look something like this:

/etc/hosts
127.0.0.1   localhost
. . .

203.0.113.5 example.com www.example.com
203.0.113.5 test.com www.test.com

This will intercept any requests for example.com and test.com and send them to your server, which is what we want if we don’t actually own the domains that we are using.

Save and close the file when you are finished."

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

Might be helpful.

3 likes
jlrdw's avatar
Level 75

@apex1 the following file works perfect:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        # server_name  tjhs;
        server_name  localhost;

        location /mini4/ {
            root   www;
            index  index.php index.html index.htm;
            try_files $uri $uri/ /mini4/index.php?$args;

        }


        location /tjhs/ {
            root   www;
            index  index.php index.html index.htm;
            try_files $uri $uri/ /tjhs/index.php?$args;

        }

  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            include "fastcgi.conf";
            
            root           www;
            #try_files $uri $uri/ /404.php?$args;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    


}


I just can't get a "named server" like laravel.test or test.laravel to work. I thought on Windows it should be:

127.0.0.1       laravel.test

In the Windows host file. But I will try another ip to see what happens.

Edit:

I tried the following:

    server {
            listen 81;
            server_name test.laravel;
           
            root /nginx/www/laravel842/public;

            location / {
                index index.html index.htm index.php;
                try_files $uri $uri/ /index.php?$query_string;
                }
		
            location = /favicon.ico { access_log off; log_not_found off; }
            location = /robots.txt  { access_log off; log_not_found off; }
	       error_page 404 /index.php;


        location ~ \.php$ {
        try_files $uri /index.php = 404;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        }

And tried both of these in windows host file:

    127.0.0.1           test.laravel

    Also tried

	192.168.0.7       test.laravel

192.168.0.7 was an available ip address.

Neither work, browser just goes to a search page. However with the same setup. if I put

http://localhost:81/

It still works. Still cannot get a server_name working in Windows 10.

jlrdw's avatar
Level 75

@apex1 I got it working. WIndows host:

127.0.0.1       laravel.local

Still did not work, but since I am using listen 81; instead of 80, I had to add the :81 like:

http://laravel.local:81/

Now all is working, thanks for your reply, it made me think about what I might be missing.

3 likes
jeffallen's avatar

Thanks, this was helpful. I was thinking about using Nginx locally since production will be Nginx, and I am also a WIndows user. I haven't tried Docker Sail setup yet, just Xampp so far.

jlrdw's avatar
Level 75

@jeffallen Remember I am new to nginx, so perhaps don't just use my config above until you read and learn more about nginx. Above is just in development. On production, I would probably go by some DIgital Ocean Guides as a minimum to ensure security.

But so far it's working.

Edit:

Also there is laravel forge, have a look:

https://forge.laravel.com/docs/1.0/introduction.html

and

https://forge.laravel.com/

Just thought I make you aware of tools like forge.

2 likes
Snapey's avatar

you have to do two things

Tell Nginx what hostnames it is serving, eg 'laravel.test' and what port to use

Tell windows where to find it by an entry in your hosts file

Then when you access the site by name in your browser, windows thinks, oh, I need to send this to 127.0.0.1 but in doing so it also puts the domain it is requesting in the headers. Nginx sees this header and checks to see if it has a virtual host with that name, if so it passes the request to the relevant resources in that virtual host

2 likes
jlrdw's avatar
Level 75

@snapey The only problem I had is the server name. But that is when all is in web and you have to point to public as document root.

However on a setup like I normally use in Apache using the guide from your site (used for years now):

http://novate.co.uk/deploy-laravel-5-on-shared-hosting-from-heart-internet/

In that case nginx worked out of the box with just a simple location block:

    server {
        listen       80;
        server_name  localhost;

       location /laravel859 {
            root   www;
            index  index.php index.html index.htm;
            try_files $uri $uri/ /laravel859/index.php?$args;

        }

       // a couple more server blocks here

It was that easy. Just me, but I prefer having "main laravel" out of web altogether.

Edit: But of course I wanted to understand the other setup (pointing to public as doc root) in nginx as well.

1 like

Please or to participate in this conversation.