Skip to content

Instantly share code, notes, and snippets.

@1v
Last active July 14, 2016 18:49
Show Gist options
  • Save 1v/77595e676132a660843c to your computer and use it in GitHub Desktop.
Save 1v/77595e676132a660843c to your computer and use it in GitHub Desktop.
Making production on port 80 and development on any other

Making production on port 80 and development on any other

Go to /etc/nginx/conf.d delete all files in it and create two files MyAppDevelopment.conf and MyAppProduction.conf (change MyApp to your app name).

Add to MyAppDevelopment.conf:

upstream MyAppDevelopment {
  server              unix:///tmp/MyAppDevelopment.sock;
}

server {
  listen              54321;
  server_name         site.com *.site.com;

  access_log          /var/log/nginx/myapp-access.log;

  location / {
    root              /home/luna/www/myapp/public;
    try_files         $uri @app;
    gzip_static       on;
    expires           max;
    add_header        Cache-Control public;
  }

  location @app {
    proxy_pass        http://MyAppDevelopment;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_next_upstream error timeout invalid_header http_502;
    proxy_max_temp_file_size 0;
  }
}

Add to MyAppProduction.conf:

upstream MyAppProduction {
  server              unix:///tmp/MyAppProduction.sock;
}

server {
  listen              localhost:8080;

  access_log          /var/log/nginx/myapp-access.log;

  location / {
    root              /home/luna/www/myapp/public;
    try_files         $uri @app;
    gzip_static       on;
    expires           max;
    add_header        Cache-Control public;
  }

  location @app {
    proxy_pass        http://MyAppProduction;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_next_upstream error timeout invalid_header http_502;
    proxy_max_temp_file_size 0;
  }
}

(no you can't join similar settings)

In /etc/nginx/nginx.conf add:

gzip  on;

# Default server
server {
  listen              8080 default_server;
  listen              54321 default_server;
  return 404;
}

include /etc/nginx/conf.d/*.conf;

Then start servers:

bundle exec puma -e development -b unix:///tmp/MyAppDevelopment.sock --debug
bundle exec puma -e production -b unix:///tmp/MyAppProduction.sock

Or in background:

bundle exec puma -e development -db unix:///tmp/MyAppDevelopment.sock
bundle exec puma -e production -db unix:///tmp/MyAppProduction.sock

See also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment