ChatGPT解决这个技术问题 Extra ChatGPT

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

我有一个 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;
}
仅供参考,语法 'default' 在 0.8.21 nginx.org/en/docs/http/request_processing.html 中更改为 'default_server'
@spuder 谢谢,这突然解决了我的 "default_server" parameter can be specified for the default "listen" directive only 错误

C
Community

The documentation says:

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

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

它还说:

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

因此,您应该从 listen 80 指令之一中删除 defaultdeferred。同样适用于 ipv6only=on 指令。


好的,我做到了,并通过了 nginx 启动的错误检查。站点 2 按预期工作,站点 1 从 nginx 返回 400。有什么建议么?
与此相同:stackoverflow.com/questions/7334193/… 没有错误,但访问日志中有“-” 400 0 “-” “-”
如果您在 nginx 错误日志中没有错误,则该错误是从 unicorn 返回的。核实。要确认此假设,您可以尝试从您的 /assets/ 位置请求一些静态文件。
看不出有什么不对的地方。如果我直接点击 IP,我会正确获取站点 2,但域会返回 400。这是 DNS 配置问题吗?我在两个站点上都通过 namecheap 为 www 和 @ 使用 A(地址)。一个有效,另一个无效
我直接请求了一项资产,它也有 400 美元
G
Guy

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

您只能在 server_name 指令之一上使用 backlog 参数。

例子

站点 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;
    }

l
logandihel

我遇到了同样的问题。我通过修改我的 /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。


这并不能解决问题。现在一台服务器正在侦听端口 80,而另一台正在侦听端口 443。问题是关于为同一端口但不同域运行两台服务器。

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅