1

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)

1
  • @PeterS It's just an example without original addresses. Both addresses are subdomains Commented Mar 23, 2016 at 8:11

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried to listen the same port (80). In this case both addresses open application /home/deploy/clientapp which should be only on the client.example.com
@PavelBabin that's because the proxy has nothing to do in your static website configuration! Do you have both configuration linked in de sites-enabled directory? It should always be port 80, because that's where web application run at.
I have two separate config files for backend and client. I removed proxy settings from client config and result is the same
Are both config files linked in /etc/nginx/sites-enabled/. If you did you reload the configuration nginx -s reload or does it have errors nginx -t?
1) both configs are in /etc/nginx/sites-enabled/. 2) yes, i'm reloading nginx like sudo service nginx restart. 3) no, there is no errors and syntax is ok
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.