How to test nginx subdomains on localhost

madhead picture madhead · Apr 10, 2012 · Viewed 26.4k times · Source

I want to test nginx subdomains before uploading config to the server. Can i test it on localhost? I try

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://localhost:8080;
    }
}

server {
    listen       80;
    server_name  sub.localhost;

    location / {
        proxy_pass http://localhost:8080/sub;
    }
}

And it does not work. Shoulld i change my hosts file in order to make it work? Also, after uploading site to the server should i change DNS records and add sub.mydomain.com?

Answer

troseman picture troseman · May 2, 2012

Yes, add '127.0.0.1 sub.localhost' to your hosts file. That sub has to be resolved somehow. That should work.

Then once you're ready to go to the net, yes, add an a or cname record for the subdomain sub.

When I use proxy_pass I also include the proxy.conf from nginx. http://wiki.nginx.org/HttpProxyModule