ChatGPT解决这个技术问题 Extra ChatGPT

Serving two sites from one server with Nginx

I have a Rails app up and running on my server and now I'd like to add another one.

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

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

They are both listening on 80 but for different things.

Site #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;
}

Site two:

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;
}
FYI, the syntax 'default' changed to 'default_server' in 0.8.21 nginx.org/en/docs/http/request_processing.html
@spuder Thanks, this suddenly fixed my "default_server" parameter can be specified for the default "listen" directive only error

C
Community

The documentation says:

The default_server parameter, if present, will cause the server to become the default server for the specified address:port pair.

It's also obvious, there can be only one default server.

And it is also says:

A listen directive can have several additional parameters specific to socket-related system calls. They can be specified in any listen directive, but only once for the given address:port pair.

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


Okay I did that and got past the error checking on nginx starting. Site 2 works as expected, site 1 returns 400's from nginx. Any suggestions?
same as this: stackoverflow.com/questions/7334193/… No errors but "-" 400 0 "-" "-" in the access log
If you have no error in nginx error log then the error was returned from unicorn. Check it. To confirm this assumption you can try to request some static file from your /assets/ location.
Can't see anything wrong there. If I hit the IP directly, I get Site 2 correctly, but the domain returns the 400. Is this a DNS configuration issue? I'm using A (Address) for both www and @ via namecheap on both sites. One works the other doesn't
I requested an asset directly and it 400'd as well
G
Guy

Just hit this same issue, but the duplicate default_server directive was not the only cause of this message.

You can only use the backlog parameter on 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;
    }

l
logandihel

I was having the same issue. I fixed it 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 had already been configured for 443.


This does not solve the problem. Now one server is listening for port 80 and one is listening for port 443. The question was about running two servers for the same port but different domains though.