使用Nginx从一台服务器提供两个站点

问题描述 投票:63回答:3

我有一个Rails应用程序在我的服务器上运行,现在我想添加另一个。

我希望Nginx检查请求是什么,并根据域名分割流量

两个站点都有自己的nginx.conf符号链接到启用站点,但是我从nginx Starting nginx: nginx: [emerg] duplicate listen options for 0.0.0.0:80 in /etc/nginx/sites-enabled/bubbles:6开始出错

他们都在聆听80但不同的事情。

网站#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;
}

网站二:

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;
}
proxy nginx
3个回答
103
投票

The documentation says:

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

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

它还说:

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

所以,你应该从default指令中删除deferredlisten 80。同样适用于ipv6only=on指令。


10
投票

刚刚遇到同样的问题,但重复的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;
    }

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。

© www.soinside.com 2019 - 2024. All rights reserved.