Skip to content

Instantly share code, notes, and snippets.

@johnnncodes
Forked from tompave/nginx.conf
Created September 20, 2019 17:43
Show Gist options
  • Select an option

  • Save johnnncodes/33eaceb868c4715a2ffacbf3fe15a219 to your computer and use it in GitHub Desktop.

Select an option

Save johnnncodes/33eaceb868c4715a2ffacbf3fe15a219 to your computer and use it in GitHub Desktop.

Revisions

  1. @tompave tompave revised this gist Apr 16, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    #
    # Tested with:
    # Ubuntu 12.04
    # nginx 1.4.1 - 1.4.5
    # nginx 1.4.1 - 1.4.7
    # Rails 3.2 - 4.0
    # Unicorn, Thin and Puma
    #
  2. @tompave tompave revised this gist Apr 16, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -68,6 +68,10 @@ events {
    # --------------------------------------------------------

    http {
    # disables emitting nginx version in error messages
    # and in the “Server” response header field
    server_tokens off;

    # MIME types
    include /etc/nginx/mime.types;

  3. @tompave tompave revised this gist Mar 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    #
    # Author: Tommaso Pavese
    # [email protected]
    # www.wonderingmachine.com
    # http://tommaso.pavese.me
    #
    # License: http://www.wtfpl.net/
    #
  4. @tompave tompave revised this gist Feb 14, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    #
    # Tested with:
    # Ubuntu 12.04
    # nginx 1.4.1 - 1.4.4
    # nginx 1.4.1 - 1.4.5
    # Rails 3.2 - 4.0
    # Unicorn, Thin and Puma
    #
  5. @tompave tompave revised this gist Feb 14, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -69,8 +69,7 @@ events {

    http {
    # MIME types
    include mime.types; # the path is relative to this conf file,
    # in this case /etc/nginx/mime.types
    include /etc/nginx/mime.types;

    # fallback MIME type if the client doesn't specify it.
    default_type application/octet-stream;
    @@ -96,6 +95,7 @@ http {
    gzip_types text/plain text/xml text/css
    text/comma-separated-values
    text/javascript application/x-javascript
    application/javascript application/json
    application/atom+xml;
    # text/html is included by default

  6. @tompave tompave revised this gist Feb 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -174,7 +174,7 @@ http {

    # This is weird, but it would still work.
    upstream rails_app_three {
    server unix:/tmp/rails_app_three.sock fail_timeout=0;
    server unix:/tmp/rails_app_three.sock fail_timeout=0;
    server 192.168.0.7:8080 fail_timeout=0;
    server 127.0.0.1:3000 weight=3 fail_timeout=0;
    server backendworker.example.com weight=5 fail_timeout=0;
  7. @tompave tompave revised this gist Feb 7, 2014. 1 changed file with 28 additions and 9 deletions.
    37 changes: 28 additions & 9 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -69,8 +69,11 @@ events {

    http {
    # MIME types
    include mime.types; # relative path, that is: /etc/nginx/mime.types
    default_type application/octet-stream; # fallback type
    include mime.types; # the path is relative to this conf file,
    # in this case /etc/nginx/mime.types

    # fallback MIME type if the client doesn't specify it.
    default_type application/octet-stream;

    # log
    access_log /var/log/nginx/access.log combined;
    @@ -83,7 +86,7 @@ http {
    # use unix sendfile()
    sendfile on;

    # compression
    # gzip compression
    gzip on;
    gzip_http_version 1.0;
    gzip_proxied any;
    @@ -97,8 +100,11 @@ http {
    # text/html is included by default


    # According to the HTTP standard headers with underscores are perfectly valid,
    # but nginx defaults to dropping them as a safety measure when working with CGI scripts.
    # According to the HTTP standard, headers with underscores are perfectly valid.
    # However, nginx defaults to dropping headers containing underscores, as they
    # might introduce ambiguities when mapping headers to CGI variables.
    #
    # Since this is not a problem with Ruby on Rails, we can safely enable them.
    underscores_in_headers on;


    @@ -147,20 +153,33 @@ http {
    # If more than one destination server is specified, the requests
    # are distributed using a weighted round-robin balancing method.
    #
    # Important:
    # Unicorn workers can all listen on the same Unix socket (or TCP port)
    # at the same time.
    # However, this might not be true for other servers.
    # A cluster of Thins, for example, will require a Unix socket per process.
    # Look at the second upstream block for an example.

    # This works well with Unicorn (yes, even with several workers)
    upstream rails_app_one {
    server unix:/tmp/rails_app_one.sock fail_timeout=0;
    }


    # This will work with a cluster of 3 Thin servers
    upstream rails_app_two {
    server unix:/tmp/rails_app_two.sock fail_timeout=0;
    server unix:/tmp/rails_app_two.0.sock fail_timeout=0;
    server unix:/tmp/rails_app_two.1.sock fail_timeout=0;
    server unix:/tmp/rails_app_two.2.sock fail_timeout=0;
    }

    # This is weird, but it would still work.
    upstream rails_app_three {
    server unix:/tmp/rails_app_three.sock fail_timeout=0;
    server 192.168.0.7:8080 fail_timeout=0;
    server 127.0.0.1:3000 weight=3 fail_timeout=0;
    server backendworker.example.com weight=5 fail_timeout=0;
    }



    # --------------------------------------------------------
    # servers

  8. @tompave tompave revised this gist Feb 3, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -87,6 +87,7 @@ http {
    gzip on;
    gzip_http_version 1.0;
    gzip_proxied any;
    gzip_vary on;
    gzip_min_length 500;
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/xml text/css
  9. @tompave tompave revised this gist Feb 3, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -280,7 +280,7 @@ http {
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails_app_two;
    }
    }

    # Location block for the assets.
    # See: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
    @@ -304,7 +304,7 @@ http {

    # Or:
    # expires 12h;
    # add_header Cache-Control 'public, must-revalidate';
    # add_header Cache-Control "public, must-revalidate";

    # The 'try_files' directive is NOT inherited from the parent block.
    try_files $uri =404;
  10. @tompave tompave revised this gist Feb 2, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -255,8 +255,8 @@ http {

    # ----------------------------

    # This server block features an efficient configuration for
    # static assets
    # This server block features an efficient configuration
    # to serve the pre-gzipped version of the static assets

    server {
    listen 80;
  11. @tompave tompave revised this gist Feb 2, 2014. 1 changed file with 47 additions and 6 deletions.
    53 changes: 47 additions & 6 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -6,15 +6,13 @@
    #
    # License: http://www.wtfpl.net/
    #
    #
    # Tested with:
    # Ubuntu 12.04
    # nginx 1.4.1 - 1.4.4
    # Rails 3.2 - 4.0
    # Unicorn, Thin and Puma
    #
    # Tested in production with Unicorn and Thin.
    # Also works with Puma (tested in dev).
    #
    #
    #
    # Docs:
    # http://nginx.org/en/docs/
    @@ -257,6 +255,8 @@ http {

    # ----------------------------

    # This server block features an efficient configuration for
    # static assets

    server {
    listen 80;
    @@ -270,20 +270,61 @@ http {
    root /var/www/winteriscoming/current/public;
    try_files $uri $uri/index.html $uri.html @rails_app;

    error_page 500 502 503 504 /500.html;


    location @rails_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails_app_two;
    }
    }

    error_page 500 502 503 504 /500.html;
    # Location block for the assets.
    # See: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
    #
    # '^~ /assets/' is a 'prefix string', not a regex.
    # '^~' will prevent further location searching after this location is matched.
    #
    # 'root' is inherited from the parent block. With the default Rails directory
    # structure it should point to 'app_root/public'.
    #
    location ^~ /assets/ {
    # To serve the pre-gzipped version of the files, if available.
    # The non-compressed versions MUST be available too, or a 404 will be returned.
    # The general gzip directives in the 'http' block are still applied.
    gzip_static on;

    # Since Rails automatically appends digest fingerprints to asset file names,
    # we don't need to worry about expiring them.
    expires max;
    add_header Cache-Control public;

    # Or:
    # expires 12h;
    # add_header Cache-Control 'public, must-revalidate';

    # The 'try_files' directive is NOT inherited from the parent block.
    try_files $uri =404;

    # We could also use a less strict directive that falls back to the Rails app
    # if a requested asset cannot be found. In this case, the cache headers here
    # configured will be ignored and Rails will set them automatically.
    # try_files $uri @rails_app;

    # Normally the Rails app takes care of serving the static file 'public/404.html'
    # when a 404 occours. Here, however, it will be handled by nginx.
    error_page 404 /404.html;
    }
    }

    }




    # A note on $uri and the try_files directive:
    #
    # try_files /maintenance.html $uri $uri/index.html $uri.html @rails_app;
  12. @tompave tompave revised this gist Jan 31, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -219,7 +219,8 @@ http {
    #
    # This is just a named location to be used in try_files.
    location @rails_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # client IP
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # the client IP
    proxy_set_header X-Real-IP $remote_addr; # the client IP (again)
    proxy_set_header X-Forwarded-Proto $scheme; # pass scheme (for HTTPS)
    proxy_set_header Host $http_host; # the full host, for redirects within Rails
    proxy_redirect off; # disable nginx redirect-rewrite logic
    @@ -271,6 +272,7 @@ http {

    location @rails_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
  13. @tompave tompave revised this gist Jan 31, 2014. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -234,9 +234,8 @@ http {
    # can also be served by 'try_files $uri', above. Still, this
    # location is more precise (thus has higher priority) and can
    # be used to further customize the error response.
    location = /500.html {
    root /var/www/example/current/public;
    }
    #
    # location = /500.html { }
    }

    # A redirect from the naked domain to www
    @@ -279,9 +278,6 @@ http {
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
    root /var/www/winteriscoming/current/public;
    }
    }

    }
  14. @tompave tompave revised this gist Jan 27, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -231,9 +231,9 @@ http {
    error_page 500 502 503 504 /500.html;

    # This location block isn't strictly required, as '/500.html'
    # can also be served by 'try_files $uri', above. This location,
    # however is more precise, thus has higher priority, and can be
    # used to further customize the error response.
    # can also be served by 'try_files $uri', above. Still, this
    # location is more precise (thus has higher priority) and can
    # be used to further customize the error response.
    location = /500.html {
    root /var/www/example/current/public;
    }
  15. @tompave tompave revised this gist Jan 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -230,7 +230,7 @@ http {
    # Customize what html file should be served for different errors.
    error_page 500 502 503 504 /500.html;

    # This location block isn't strictly required, as '/500.html' will
    # This location block isn't strictly required, as '/500.html'
    # can also be served by 'try_files $uri', above. This location,
    # however is more precise, thus has higher priority, and can be
    # used to further customize the error response.
  16. @tompave tompave revised this gist Jan 27, 2014. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -226,8 +226,14 @@ http {
    proxy_pass http://rails_app_one; # http is to be used even for https traffic
    }

    # Customize what html file to render for error codes

    # Customize what html file should be served for different errors.
    error_page 500 502 503 504 /500.html;

    # This location block isn't strictly required, as '/500.html' will
    # can also be served by 'try_files $uri', above. This location,
    # however is more precise, thus has higher priority, and can be
    # used to further customize the error response.
    location = /500.html {
    root /var/www/example/current/public;
    }
  17. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -112,10 +112,10 @@ http {
    # - a server{} block
    # - an upstream{} block
    #
    # The convention is to store configuration files in:
    # The convention is to store configuration files in:
    # /etc/nginx/sites-availbale/
    #
    # and then symlink them into:
    # and then symlink them into:
    # /etc/nginx/sites-enabled/
    #
    # with:
    @@ -145,7 +145,7 @@ http {
    #
    # We can also use IP addresses and domain names, and mix them together.
    #
    # If more that one destination server is specified, the requests
    # If more than one destination server is specified, the requests
    # are distributed using a weighted round-robin balancing method.
    #
    upstream rails_app_one {
  18. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -213,7 +213,6 @@ http {
    #
    try_files /maintenance.html $uri $uri/index.html $uri.html @rails_app;

    error_page 500 502 503 504 /500.html;

    # The location directory allows very creative configurations.
    # http://nginx.org/en/docs/http/ngx_http_core_module.html#location
    @@ -226,6 +225,12 @@ http {
    proxy_redirect off; # disable nginx redirect-rewrite logic
    proxy_pass http://rails_app_one; # http is to be used even for https traffic
    }

    # Customize what html file to render for error codes
    error_page 500 502 503 504 /500.html;
    location = /500.html {
    root /var/www/example/current/public;
    }
    }

    # A redirect from the naked domain to www
    @@ -258,7 +263,6 @@ http {

    root /var/www/winteriscoming/current/public;
    try_files $uri $uri/index.html $uri.html @rails_app;
    error_page 500 502 503 504 /500.html;

    location @rails_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    @@ -267,6 +271,11 @@ http {
    proxy_redirect off;
    proxy_pass http://rails_app_two;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
    root /var/www/winteriscoming/current/public;
    }
    }

    }
  19. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -201,9 +201,11 @@ http {

    # http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
    #
    # Tries files in sequence until one is found.
    # Tries URIs in sequence until one is found.
    # First we try to serve static files directly from nginx.
    # The rails application is the last alternative.
    #
    # See comment block at the end of the file for detailed examples.
    # The last one is the rails app.
    #
    # /maintenance.html can be symlinked into public/ to stop requests
    # before they reach the rails app (e.g. while updating the DB).
  20. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -149,15 +149,15 @@ http {
    # are distributed using a weighted round-robin balancing method.
    #
    upstream rails_app_one {
    server unix:/tmp/rails_app_one.sock fail_timeout=0;
    server unix:/tmp/rails_app_one.sock fail_timeout=0;
    }


    upstream rails_app_two {
    server unix:/tmp/rails_app_two.sock fail_timeout=0;
    server 192.168.0.7:8080 fail_timeout=0;
    server 127.0.0.1:3000 weight=3 fail_timeout=0;
    server backendworker.example.com weight=5 fail_timeout=0;
    server unix:/tmp/rails_app_two.sock fail_timeout=0;
    server 192.168.0.7:8080 fail_timeout=0;
    server 127.0.0.1:3000 weight=3 fail_timeout=0;
    server backendworker.example.com weight=5 fail_timeout=0;
    }


  21. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -157,7 +157,7 @@ http {
    server unix:/tmp/rails_app_two.sock fail_timeout=0;
    server 192.168.0.7:8080 fail_timeout=0;
    server 127.0.0.1:3000 weight=3 fail_timeout=0;
    server one.backend.example.com weight=5 fail_timeout=0;
    server backendworker.example.com weight=5 fail_timeout=0;
    }


  22. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -157,6 +157,7 @@ http {
    server unix:/tmp/rails_app_two.sock fail_timeout=0;
    server 192.168.0.7:8080 fail_timeout=0;
    server 127.0.0.1:3000 weight=3 fail_timeout=0;
    server one.backend.example.com weight=5 fail_timeout=0;
    }


  23. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@
    #
    #
    # In order to bind to port 80, the master process must be executed as root:
    # sudo /usr/sbin/nginx -c /etc/nginx/nginx.conf
    # $ sudo /usr/sbin/nginx -c /etc/nginx/nginx.conf
    #

    # How to execute the worker processes
  24. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@
    # [email protected]
    # www.wonderingmachine.com
    #
    # License: http://www.wtfpl.net/
    #
    # Tested with:
    # Ubuntu 12.04
    # nginx 1.4.1 - 1.4.4
  25. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,8 @@
    #
    # Tested with:
    # Ubuntu 12.04
    # nginx v 1.4.1 - 1.4.4
    # Rails 3.2 - 4.0
    # nginx 1.4.1 - 1.4.4
    # Rails 3.2 - 4.0
    #
    # Tested in production with Unicorn and Thin.
    # Also works with Puma (tested in dev).
  26. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    # www.wonderingmachine.com
    #
    # Tested with:
    # Ubuntu 12.04
    # nginx v 1.4.1 - 1.4.4
    # Rails 3.2 - 4.0
    #
  27. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,17 @@
    # A commented nginx configuration file for Ruby on Rails
    #
    # Production tested with Unicorn and Thin.
    # Should work well also with Puma.
    #
    # Author: Tommaso Pavese
    # [email protected]
    # www.wonderingmachine.com
    #
    # Tested with:
    # nginx v 1.4.1 - 1.4.4
    # Rails 3.2 - 4.0
    #
    # Tested in production with Unicorn and Thin.
    # Also works with Puma (tested in dev).
    #
    #
    #
    # Docs:
    # http://nginx.org/en/docs/
  28. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ worker_processes 1;
    # "open files" is intended as UNIX open file descriptors.
    # This overrides the limit set by the OS for the user the workers run as.
    # Run `ulimit -a` in a shell to see the current limit.
    worker_rlimit_core 2048;
    worker_rlimit_nofile 2048;


    error_log /var/log/nginx/error.log warn;
  29. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx.conf
    Original file line number Diff line number Diff line change
    @@ -108,7 +108,7 @@ http {
    # /etc/nginx/sites-availbale/
    #
    # and then symlink them into:
    # /etc/nginx/sites-enabled
    # /etc/nginx/sites-enabled/
    #
    # with:
    # $ ln -s /etc/nginx/sites-availbale/example.com.conf /etc/nginx/sites-enabled/example.com.conf
  30. @tompave tompave revised this gist Jan 24, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -219,12 +219,12 @@ http {

    # A redirect from the naked domain to www
    #
    # This defaults to port 80, so it can't handle HTTPS rewrites.
    # This listens to port 80, thus it can't handle HTTPS traffic.
    # (therefore, using $scheme instead of http is a bit useless)
    #
    # If the certificate is compatible with the naked domain
    # it can be improved by replicating the SSL connection of the
    # real server.
    # If the certificate is also compatible with the naked domain,
    # then this server block can be improved by replicating here
    # the SSL configuration of the main server.
    server {
    listen 80;
    server_name example.com;