0

I'm stuck with very strange Nginx' behavior. I have 2 RoR sites, that are working with Puma.First site (app1.com) is working like should, but when i try to access app2.com, i'm just getting app1.com pages but with domain name of app2.com. The most interestin this that if in the nginx.conf i replace include /etc/nginx/sites-enabled/;* to line with direct link to site (i.e. sites-enabled/app2; sites-enabled/app1;) i can get correct response from app2, but app1 changes places with app1. What i missing?

So this is my Nginx config:

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

And for second app:

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

The nginx.conf file is:

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;

}

To give full info, i should say that all apps hosts on Digital Ocean VPS, but nameservers of app1 is on DO (but was bought on godaddy, i just transfered nameservers and then added A and CNAME records on DO account), and domain name of app2 is on godaddy so i added an A and CNAME records to make it works.

7
  • Are you sure that the server_name statement for app2 is correct? It must be an exact match of the domain name you use to access the website. Nov 14, 2017 at 12:59
  • @RichardSmith of course i'm sure, it works if i turn off app1. And ofc i did ctrl+c ctrl+v from godaddy, where is that domain name was bought.
    – Denys
    Nov 14, 2017 at 13:42
  • It works if you turn off app1 because it becomes the only server block and will respond to any domain name. See this document. Nov 14, 2017 at 14:13
  • @RichardSmith i'm sorry for this probably stupid question, but what should I do? I see nothing special in this document. Btw, I did this before and everything were working.
    – Denys
    Nov 14, 2017 at 14:36
  • @RichardSmith also how happens if in the nginx.conf i replace include /etc/nginx/sites-enabled/;* to line with direct link to site (i.e. sites-enabled/app2; AND sites-enabled/app1;) i can get correct response from app2, but app1 changes places with app1.
    – Denys
    Nov 14, 2017 at 14:59

1 Answer 1

0

Ok, i figured it out, what was wrong. Guys, don't forget to put into server_name directive not only your domain but also domain with www. I have no idea why should i do it, but it works (on other server i don't have to add this)

1
  • This can happen if your A records are set wrong. You should set the www A record to point to your server as well and include a simple WWW to Non-WWW redirection in both your apps config file. www to non-www
    – Bert
    Nov 20, 2017 at 8:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .