Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Nginx Unicorn

ZeroC0D3 Team edited this page Jan 19, 2018 · 3 revisions

NGINX Unicorn Configuration

Standard configuration for NGINX at /etc/nginx/site-available/[default]

NGINX Unicorn for Development

upstream faye_server {
  server dev.zeroc0d3lab.com:9292;
}

upstream staging {
  server unix:/home/deploy/zeroc0d3lab.dev/current/tmp/sockets/unicorn-staging.socket fail_timeout=0;
}

server {
  listen 80;
  server_name dev.zeroc0d3lab.com;
  root /home/deploy/zeroc0d3lab.dev/current/public;

  client_max_body_size 300M;
  keepalive_timeout 120;
  send_timeout 120;
  underscores_in_headers on;

  error_page 500 /500.html;
  try_files /system/maintenance.html $uri/index.html $uri @unicorn;

  proxy_http_version 1.1;
  chunked_transfer_encoding off;
  proxy_buffering off;
  proxy_cache off;

  proxy_connect_timeout 43200000;
  proxy_read_timeout    43200000;
  proxy_send_timeout    43200000;

  location ~ ^/(assets)/ {
    root /home/deploy/zeroc0d3lab.dev/current/public;
    gzip_static on;
    expires 1y;
    add_header Cache-Control public;
    break;
  }

  location @unicorn {
    proxy_set_header X-Request-Start "t=${msec}000";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://staging;
  }

  location /pub-sub {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://faye_server;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  if ($http_user_agent ~* (EasouSpider) ) {
    return 403;
  }

}

server{
  listen 443;
  ssl on;
  ssl_certificate     /etc/ssl/star_zeroc0d3lab_com.pem;
  ssl_certificate_key /etc/ssl/zeroc0d3lab.key;

  server_name dev.zeroc0d3lab.com;
  root /home/deploy/zeroc0d3lab.dev/current/public;

  client_max_body_size 300M;
  keepalive_timeout 120;
  send_timeout 120;

  error_page 500 /500.html;
  try_files /system/maintenance.html $uri/index.html $uri @unicorn;

  proxy_http_version 1.1;
  chunked_transfer_encoding off;
  proxy_buffering off;
  proxy_cache off;

  proxy_connect_timeout 43200000;
  proxy_read_timeout    43200000;
  proxy_send_timeout    43200000;

  location ~ ^/(assets)/ {
    root /home/zeroc0d3/staging/public;
    gzip_static on;
    expires 1y;
    add_header Cache-Control public;
    break;
  }

  location @unicorn {
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Request-Start "t=${msec}000";
    proxy_set_header X-Forwarded-Forr $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://staging;
  }

  location /pub-sub {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://faye_server;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  if ($http_user_agent ~* (EasouSpider) ) {
    return 403;
  }

}

NGINX for Unicorn Production

upstream faye_server {
  server zeroc0d3lab.com:9292;
}

upstream production {
  server unix:/home/deploy/zeroc0d3lab/current/tmp/sockets/unicorn-production.socket fail_timeout=0;
}

server {
  listen 80;
  server_name zeroc0d3lab.com;
  root /home/deploy/zeroc0d3lab/current/public;

  client_max_body_size 300M;
  keepalive_timeout 120;
  send_timeout 120;
  underscores_in_headers on;

  error_page 500 /500.html;
  try_files /system/maintenance.html $uri/index.html $uri @unicorn;

  proxy_http_version 1.1;
  chunked_transfer_encoding off;
  proxy_buffering off;
  proxy_cache off;

  proxy_connect_timeout 43200000;
  proxy_read_timeout    43200000;
  proxy_send_timeout    43200000;

  location ~ ^/(assets)/ {
    root /home/deploy/zeroc0d3lab/current/public;
    gzip_static on;
    expires 1y;
    add_header Cache-Control public;
    break;
  }

  location @unicorn {
    proxy_set_header X-Request-Start "t=${msec}000";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://production;
  }

  location /pub-sub {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://faye_server;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  if ($http_user_agent ~* (EasouSpider) ) {
    return 403;
  }

}

server{
  listen 443;
  ssl on;
  ssl_certificate     /etc/ssl/star_zeroc0d3lab_com.pem;
  ssl_certificate_key /etc/ssl/zeroc0d3lab.key;

  server_name zeroc0d3lab.com;
  root /home/deploy/zeroc0d3lab/current/public;

  client_max_body_size 300M;
  keepalive_timeout 120;
  send_timeout 120;

  error_page 500 /500.html;
  try_files /system/maintenance.html $uri/index.html $uri @unicorn;

  proxy_http_version 1.1;
  chunked_transfer_encoding off;
  proxy_buffering off;
  proxy_cache off;

  proxy_connect_timeout 43200000;
  proxy_read_timeout    43200000;
  proxy_send_timeout    43200000;

  location ~ ^/(assets)/ {
    root /home/deploy/zeroc0d3lab/public;
    gzip_static on;
    expires 1y;
    add_header Cache-Control public;
    break;
  }

  location @unicorn {
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Request-Start "t=${msec}000";
    proxy_set_header X-Forwarded-Forr $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://production;
  }

  location /pub-sub {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://faye_server;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  if ($http_user_agent ~* (EasouSpider) ) {
    return 403;
  }

}

Check Configuration (Test Validation)

sudo nginx -t                    ## or
sudo /etc/init.d/nginx -t

Reload NGINX

sudo service nginx reload        ## or
sudo /etc/init.d/nginx reload

Restart NGINX

sudo service nginx restart       ## or
sudo /etc/init.d/nginx restart

Start | Stop NGINX

sudo service nginx start         ## or
sudo /etc/init.d/nginx start

sudo service nginx stop          ## or
sudo /etc/init.d/nginx stop
Clone this wiki locally