Rails Nginx:多个应用程序

Rails Nginx: multiple applications

我有两个用于应用程序的 nginx 配置:

1) Rails 申请

upstream app {
  server unix:/home/deploy/railsapp/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name api.example.com;

  root /home/deploy/railsapp/current/public;

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

  location / {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_pass http://app;
  }

  location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

2) 静态 html/js 应用程序

server {
  listen 81;
  server_name  client.example.com;
  root /home/deploy/clientapp;

  location / {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
   # proxy_pass http://app;
  }

  location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
}

问题是地址 api.example.comapi.example.com 打开同一个应用程序(对于 api.example.com

nginx配置的一些基础教程; https://www.digitalocean.com/community/tutorials/how-to-configure-the-nginx-web-server-on-a-virtual-private-server。您应该将代理与 rails 应用程序一起使用 而不是 与静态 html 应用程序。

server {
  listen 80; #should also be 80
  server_name  client.example.com;

  root /home/deploy/clientapp;
  index index.html index.htm;

  location / {
     try_files $uri $uri/ /index.html;
  }

  location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
}

要测试配置,您可以执行 nginx -t 并且应该使用 nginx -s reload 重新加载以使用新配置?如前所述,此问题与rails无关,只是nginx配置问题。

server_name 是在哪个网站上 return 请求,如果没有 default_server 它会 return 最后一个网站加载到 nginx 配置.