change localhost hostname in nginx

Viewed 14343

I have multiple local sites and I want to configure nginx to have a different host of each website.

In /var/www I have 2 sites: site1 and site2

Then in /etc/nginx/sites-available/ I created 2 different configurations server for each one. I have the files site1 and site2 which content is like:

server {
        listen 80;

        root /var/www/site1;
        index index.html index.htm;

        server_name localhost;

        location / {
                try_files $uri $uri/ /index.html;
        }
}

and

server {
            listen 7777;

            root /var/www/site2;
            index index.html index.htm;

            server_name localhost;

            location / {
                    try_files $uri $uri/ /index.html;
            }
    }

I access to them with http://localhost:80 for site1 and http://localhost:7777 for site2. That works perfectly. I also could add the hostname in /etc/hosts like this:

127.0.0.1 localhost site1 site2

and I could access them with http://site1:80 and http://site2:7777. But I have to access always to the port number. I want to access them by http://site1 and http://site2.

Is there a solution to do that ?

2 Answers

To set /etc/hosts for windows 10.

Go to c:\Windows\System32\Drivers\etc\hosts directory and open the file with a text editor and then add this line 127.0.0.1 localhost site1 site2.

Then open any browser and hit site1/. Make sure it is http://, not https://.

Related