Muhammad Yana Mulyana

Setup Nginx for Rails Application in Ubuntu

Setup Nginx for Rails Application in Ubuntu - Hello, this is setup nginx for ruby on rails application use ubuntu, puma and lets encrypt SSL. This is just note for my own self and not tutorial, cause i'm lazy person 😁

upstream puma {
  server unix:///home/rails/apps/muhammadyana.me/shared/tmp/sockets/muhammadyana.me-puma.sock;
}

server {
  server_name muhammadyana.me www.muhammadyana.me;
 
  root /home/rails/apps/muhammadyana.me/current/public;
  access_log /home/rails/apps/muhammadyana.me/current/log/nginx.access.log;
  error_log /home/rails/apps/muhammadyana.me/current/log/nginx.error.log info;

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

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
    proxy_pass http://puma;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;

  listen 443 ssl;
  ssl_certificate /etc/letsencrypt/live/muhammadyana.me/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/muhammadyana.me/privkey.pem; 
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

}


server {
  if ($host = www.muhammadyana.me) {
    return 301 https://$host$request_uri;
  }


  if ($host = muhammadyana.me) {
    return 301 https://$host$request_uri;
  }

  listen 80 deferred;
  server_name muhammadyana.me www.muhammadyana.me;
  return 404; # managed by Certbot
}