Nginx 无法正确与多个应用程序协同工作

Nginx 无法正确与多个应用程序协同工作

我被非常奇怪的 Nginx 行为所困扰。我有 2 个 RoR 网站,它们与 Puma 一起工作。第一个网站 (app1.com) 工作正常,但当我尝试访问 app2.com 时,我只得到 app1.com 页面,但域名是 app2.com。最有趣的是,如果在nginx.conf 我更换 包括 /etc/nginx/sites-enabled/;* 与站点直接链接对齐(即 sites-enabled/app2; sites-enabled/app1;)我可以从 app2 获得正确响应,但 app1 与 app1 交换位置。我遗漏了什么?

这是我的 Nginx 配置:

upstream puma {
  server unix:///home/dev/www/apps/app1/shared/tmp/sockets/puma_app1.sock;
}

server {
  listen 80;
  server_name app1.com
  root /home/dev/www/apps/app1/current/public;

  access_log /home/dev/www/apps/app1/shared/log/nginx.access.log;
  error_log /home/dev/www/apps/app1/shared/log/nginx.error.log info;

try_files $uri/index.html $uri @puma;

location @puma {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $host;
     proxy_redirect off;
     proxy_pass http://puma;
#     proxy_set_header X-NginX-Proxy true;

}


location /home/dev/www/apps/app1/current/public/assets/* {
#location ~* ^/assets/ {
 expires 1y;
    add_header Cache-Control public;

    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

对于第二个应用程序:

upstream puma2{
  server unix:///home/dev/www/apps/app2/shared/tmp/sockets/puma_app2.sock;
}

server {
  listen 80;
  server_name app2.com;
  root /home/dev/www/apps/app2/current/public;
  access_log /home/dev/www/apps/app2/shared/log/nginx.access.log;
  error_log /home/dev/www/apps/app2/shared/log/nginx.error.log info;

  try_files $uri/index.html $uri @puma2;
location @puma2 {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $host;
     proxy_redirect off;
     proxy_pass http://puma2;
#     proxy_set_header X-NginX-Proxy true;

}


location /home/dev/www/apps/app2/current/public/assets/* {
 expires 1y;
    add_header Cache-Control public;

    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

nginx.conf 文件是:

user  dev;
worker_processes  auto;

error_log /var/log/nginx/error.log error;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;
    tcp_nopush     on;
  #  include /etc/nginx/sites-enabled/app2;
   # include /etc/nginx/sites-enabled/app1;
include /etc/nginx/sites-enabled/*;  
  keepalive_timeout  65;

    gzip  on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml
    application/xml+rss text/javascript application/javascript;

}

为了提供完整的信息,我应该说所有应用程序都托管在 Digital Ocean VPS 上,但 app1 的名称服务器在 DO 上(但在 godaddy 上购买,我只是转移了名称服务器,然后在 DO 帐户上添加了 A 和 CNAME 记录),并且 app2 的域名在 godaddy 上,所以我添加了 A 和 CNAME 记录以使其工作。

答案1

server_name好的,我明白了,哪里出错了。伙计们,别忘了不仅在指令中输入您的域名,还要输入带有的域名www。我不知道为什么要这样做,但它有效(在其他服务器上我不需要添加这个)

相关内容