简体   繁体   中英

Rails Nginx: multiple applications

I have two nginx configs for applications:

1) Rails application

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) Static html/js application

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

The problem is both addresses api.example.com and api.example.com open the same application (for api.example.com )

Some basic tutorial on nginx config; https://www.digitalocean.com/community/tutorials/how-to-configure-the-nginx-web-server-on-a-virtual-private-server . You should use a proxy with rails applications not with static html application.

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

To test the configuration you can perform nginx -t and should reload using nginx -s reload to use the new configuration? As stated before this problem has nothing to do with rails, it is just nginx configuration.

The server_name is leading on which website is returned on a request, if there is no default_server it will return the last website loaded into nginx configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM