Host two rails app using Puma & Nginx on domain and subdomain each

marvindanig asked:

I have two rails applications and both use separate POSTGRES, nginx and puma instances on the same Ubuntu 16.04 machine. One is currently live on a domain and the other is supposed to go on a subdomain of the same domain.

Their nginx configs will sit on separate files for automatic deployments.

The first rails server has been [live](root website) for a while and is served under HSTS using Letsencrypt on the root domain (example.com). The second rails app is supposed to go on a subdomain (news.example.com) and will also be served under HSTS with Letsencrypt.

Here’s how my nginx config of the live domain looks like:

root domain

upstream puma_bubblin_production { 
  server unix:/var/www/bubblin.io/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name bubblin.io www.bubblin.io;
    return 301 https://$host$request_uri; 
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name www.bubblin.io;
  ssl_certificate /etc/letsencrypt/live/www.bubblin.io/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/www.bubblin.io/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

  rewrite ^ https://bubblin.io$request_uri permanent;

}

server {
  server_name bubblin.io;
  root /var/www/bubblin.io/current/public;
  try_files $uri/index.html $uri @puma_bubblin_production;

  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 504 /500.html;
  error_page 503 @503;

  location @puma_bubblin_production {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Forwarded-Proto http;
    proxy_pass http://puma_bubblin_production;
    # limit_req zone=one;
    access_log /var/www/bubblin.io/shared/log/nginx.access.log;
    error_log /var/www/bubblin.io/shared/log/nginx.error.log;
  }

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  listen 443 ssl http2; # managed by Certbot
  listen [::]:443 ssl http2;
  ssl_certificate /etc/letsencrypt/live/bubblin.io/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/bubblin.io/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

  # Add HSTS header with preloads
  add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";


}

sub domain

This is not working.

upstream puma_news_bubblin_io { 
  server unix:/var/www/news/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    listen [::]:80;
    server_name news.bubblin.io;
    return 301 https://$host$request_uri; 
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;

  ssl_certificate /etc/letsencrypt/live/news.bubblin.io/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/news.bubblin.io/privkey.pem; # managed by Certbot

  rewrite ^ https://news.bubblin.io$request_uri permanent;

}

server {
  server_name news.bubblin.io;
  root /var/www/news/current/public;
  try_files $uri/index.html $uri @puma_news_bubblin_io;

  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 504 /500.html;
  error_page 503 @503;

  location @puma_news_bubblin_io {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Forwarded-Proto http;
    proxy_pass http://puma_news_bubblin_io;
    # proxy_pass http://localhost:3030;
    # limit_req zone=one;
    access_log /var/www/news/shared/log/nginx.access.log;
    error_log /var/www/news/shared/log/nginx.error.log;
  }

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  listen 443 ssl http2; # managed by Certbot
  listen [::]:443 ssl http2;


  ssl_certificate /etc/letsencrypt/live/news.bubblin.io/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/news.bubblin.io/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


  # Add HSTS header with preloads
  add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";


}

Both the configs above are pulled into nginx.conf per following clause:

include /etc/nginx/sites-enabled/*;

How should I do this?

My answer:


Your site news.bubblin.io is using certificates intended for testing. The certificate chain, indeed, is:

Certificate chain
 0 s:CN = news.bubblin.io
   i:CN = Fake LE Intermediate X1
 1 s:CN = Fake LE Intermediate X1
   i:CN = Fake LE Root X1

Your main site www.bubblin.io uses a proper Let’s Encrypt certificate chain.

Certificate chain
 0 s:CN = www.bubblin.io
   i:C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
 1 s:C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
   i:O = Digital Signature Trust Co., CN = DST Root CA X3

Regenerate your certificate, but this time do not use options such as --staging or --test which would cause fake certificates to be created.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.