69

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;
}
4

3 回答 3

113

文档说:

default_server 参数(如果存在)将导致服务器成为指定地址:端口对的默认服务器。

也很明显,只能有一个默认服务器。

它还说:

一个监听指令可以有几个特定于与套接字相关的系统调用的附加参数。它们可以在任何监听指令中指定,但对于给定的地址:端口对只能指定一次。

因此,您应该从其中一个指令中删除default和。这同样适用于指令。deferredlisten 80ipv6only=on

于 2012-12-03T20:37:41.960 回答
17

刚刚遇到同样的问题,但重复default_server指令不是此消息的唯一原因。

您只能backlog在其中一个server_name指令上使用参数。

例子

站点 1:

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

站点 2:

server {
    listen 80;    ## NOT NOT DUPLICATE THESE SETTINGS 'default_server backlog=2048;'
    server_name blogs.example.com;
    location / {
        proxy_pass http://blog_server;
    }
于 2016-06-02T10:22:13.697 回答
0

我遇到了同样的问题。我通过修改我的 /etc/nginx/sites-available/example2.com 文件来修复它。我将服务器块更改为

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

在 /etc/nginx/sites-available/example1.com 我注释掉了listen 80listen [::]:80因为服务器块已经配置为 443。

于 2018-03-03T01:56:58.383 回答