Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test nginx subdomains on localhost

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?

like image 988
madhead - StandWithUkraine Avatar asked Apr 10 '12 19:04

madhead - StandWithUkraine


People also ask

Can you test nginx locally?

Making the changes directly to the configuration files on the server means you can test your changes basically straight away. However, If you have a load balancer directing traffic to more than one instance, you have to make the same change in several places.

Can you have subdomains on localhost?

You need to set your /etc/hosts or C:\Windows\system32\drivers\etc\hosts (as administrator) to reflect the "subdomain". So add 127.0. 0.1 dev. localhost to either file (depending on your platform).


1 Answers

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

like image 185
troseman Avatar answered Sep 30 '22 12:09

troseman