Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I serve multiple domains on the same port with nginx

Tags:

nginx

For development, I'd like to serve multiple projects on different local domains, all on port 80. In my hosts file I direct local.example.com to localhost, same for local.example2.com.

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

I've read the nginx documentation and this blog post. But I think I must be missing something.

I've added to my nginx.conf:

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

Then in sites I add configuration like example.conf:

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

and example2.conf:

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

Now the weird thing is that nginx seems to load the alphabetically first config, but on the second url, it serves the resources from the first server definition too. Nginx seems to totally ignore the server_name. How should I configure for this use case?

UPDATE:

It appears that if you use only one separator in the domain name (e.g. example1.local), it works just fine. I didn't further pursue this, because I have better things to do, but it's odd.

like image 750
iwein Avatar asked Oct 22 '13 06:10

iwein


People also ask

Can NGINX host multiple domains?

Installing NGINXNginx supports hosting multiple domains using server blocks. All your websites can be stored on a single server if you select a VPS as your hosting platform, allowing you the freedom to take charge of the situation yourself. “apt-get update” doesn't install new versions of software.

How do I run multiple domains on one server?

Specify two domains in DNS. Create two e.g. virtual hosts (in Apache) corresponding to those domains. Set each document root to point your WordPress installation. Configure WordPress to service both those domains.

How do I run multiple ports on NGINX?

To make Nginx Listen on multiple ports for a single virtual host file, you can add multiple listen directives. If you want to make Nginx listen for different virtual hosts on different ports, you can use different ports in listen directive in different virtual host files. It's that easy!

How many connections can NGINX handle?

NGINX can handle a maximum of 512 concurrent connections. In newer versions, NGINX supports up to 1024 concurrent connections, by default.


1 Answers

Apparently, nginx doesn't like the format of my server names. If I remove the 'local' subdomain it seems to work much better. I'm now working with example.dev and example2.dev and the problem is gone.

like image 58
iwein Avatar answered Sep 28 '22 00:09

iwein