How to serve multiple domains on the same port with nginx

For development, I would like to serve several projects in different local domains, all on port 80. In my hosts file, I direct local.example.com to localhost, the same goes for local.example2.com.

Now I'm trying to convince nginx to serve example resources for one url, and example2 resources for another.

I read the nginx documentation and this blog post . But I think I'm missing something.

I added to my nginx.conf:

include /Users/iwein/Sites/conf/*.conf; 

Then on sites I add a configuration like example.conf :

 server { listen 80; server_name local.example.com; … 

and example2.conf :

 server { listen 80; server_name local.example2.com; … 

Now it’s strange that nginx loads the alphabetically first config, but at the second URL it also serves resources from the first server definition. Nginx seems to completely ignore server_name. How do I configure this use case?

UPDATE:

It seems that if you use only one separator per domain name (e.g. example1.local), it works fine. I didn't chase it anymore because I have better things to do, but it's weird.

+8
nginx
source share
1 answer

Apparently nginx doesn't like the format of my server names. If I delete the "local" subdomain, it works much better. Now I am working with example.dev and example2.dev and the problem has disappeared.

+1
source share

All Articles