Serving two sites from one server using Nginx

I have a Rails application and it runs on my server, and now I would like to add another one.

I want Nginx to check what the request is for and to separate traffic based on the domain name

Both sites have their own nginx.conf, symbolically linked to sites, but I get an error message starting with nginx Starting nginx: nginx: [emerg] duplicate listen options for 0.0.0.0:80 in /etc/nginx/sites-enabled/bubbles:6

They listen to 80, but for different things.

Site number 1

 upstream blog_unicorn { server unix:/tmp/unicorn.blog.sock fail_timeout=0; } server { listen 80 default deferred; server_name walrus.com www.walrus.com; root /home/deployer/apps/blog/current/public; location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; } try_files $uri/index.html $uri @blog_unicorn; location @blog_unicorn { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://blog_unicorn; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; } 

Second site:

 upstream bubbles_unicorn { server unix:/tmp/unicorn.bubbles.sock fail_timeout=0; } server { listen 80 default deferred; server_name bubbles.com www.bubbles.com; root /home/deployer/apps/bubbles/current/public; location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; } try_files $uri/index.html $uri @bubbles_unicorn; location @bubbles_unicorn { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://bubbles_unicorn; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; } 
+64
proxy nginx
Dec 03
source share
3 answers

The documentation states:

The default_server parameter, if present, will cause the server to become the default server for a pair of addresses: port.

It is also obvious that there can only be one server by default .

And it also says:

The listening directive may have several additional parameters specific to socket system calls. They can be specified in any listening directive, but only once for a given address: a pair of ports.

So, you should remove default and deferred from one of the listen 80 directives. The same applies to the ipv6only=on directive.

+104
Dec 03
source share

Just click on the same problem, but the duplicate default_server directive was not the only reason for this message.

You can use the backlog parameter in only one of the server_name directives.

example

site 1:

 server { listen 80 default_server backlog=2048; server_name www.example.com; location / { proxy_pass http://www_server; } 

site 2:

 server { listen 80; ## NOT NOT DUPLICATE THESE SETTINGS 'default_server backlog=2048;' server_name blogs.example.com; location / { proxy_pass http://blog_server; } 
+10
Jun 02 '16 at 10:22
source share

I had the same problem. I fixed this by modifying my /etc/nginx/sites-available/example2.com file. I changed the server block to

 server { listen 443 ssl; # modified: was listen 80; listen [::]:443; #modified: was listen [::]:80; . . . } 

And in /etc/nginx/sites-available/example1.com I commented out listen 80 and listen [::]:80 because the server block was already configured for 443.

0
Mar 03 '18 at 1:56
source share



All Articles