Example Nginx Configuration File Simple Installation for Nginx 0.6.x

A template for your simple installation nginx 0.6.x configuration file. Edit to suit.

Filename: nginx-conf-example.txt ( download )

Size: 7 KB

Document type: text/plain ( Replace description with document text )

  1. Walter McGinnis, 2007-08-13
  2. updated based on http://brainspl.at/nginx.conf.txt
  3. see there for comments on each option
    user your_user_account your_user_account_or_group_if_different;

worker_processes 6;
pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

  1. configure log format
    log_format main ‘$remote_addr – $remote_user [$time_local] ’
    ’"$request" $status $body_bytes_sent “$http_referer” ’
    ’"$http_user_agent" “$http_x_forwarded_for”’;
access_log logs/access.log main;
  1. main error log
    error_log logs/nginx_error.log debug;
  1. rewrite_log on; # I have yet to find where this gets saved to :(
sendfile on; tcp_nopush on; tcp_nodelay off;
  1. output compression saves bandwidth
    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  1. your_app -—————————————————————————-
  1. redirect “www.your_app.com” to “your_app.com”
  2. you may want to do the reverse
    server {
    listen 80;
    server_name www.your_app.com;
    location / {
    rewrite ^(.*)$ http://your_app.com$1 last;
    }
    }
server { listen 443; server_name www.your_app.com; location / { rewrite ^(.*)$ https://your_app.com$1 last; } }
  1. add listeners on ports here as needed for your mongrel cluster
  2. i.e. server 127.0.0.1:8001;
  3. server 127.0.0.1:8002;
  4. etc
    upstream your_app {
    server 127.0.0.1:8000;
    }
  1. for non-ssl traffic, i.e. http
    server {
    listen 80;
    server_name your_app.com;
  1. Set the max size for file uploads
  2. we set this high here and manage it
  3. within our app’s system setting for max file size
    client_max_body_size 500M;
access_log logs/access.your_app.log main; error_log logs/error.your_app.log debug;
  1. doc root
    root /your_home_directory_path/apps/your_app/public;
  1. this rewrites all the requests to the maintenance.html
  2. page if it exists in the doc root. This is for capistrano’s
  3. disable web task
    if (-f $document_root/system/maintenance.html) {
    rewrite ^(.*)$ /system/maintenance.html last;
    break;
    }
location / {
  1. needed to forward user’s IP address to rails
    proxy_set_header X-Real-IP $remote_addr;
  1. needed for HTTPS
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect false;
    proxy_max_temp_file_size 0;
  1. If the file exists as a static file serve it directly without
  2. running all the other rewite tests on it
    if (-f $request_filename) {
    break;
    }
  1. check for index.html for directory index
  2. if its there on the filesystem then rewite
  3. the url to add /index.html to the end of it
  4. and then break to send it to the next config rules.
    if (-f $request_filename/index.html) {
    rewrite (.*) $1/index.html break;
    }
  1. this is the meat of the rails page caching config
  2. it adds .html to the end of the url and then checks
  3. the filesystem for that file. If it exists, then we
  4. rewite the url to have explicit .html on the end
  5. and then send it on its way to the next config rule.
  6. if there is no file on the fs then it sets all the
  7. necessary headers and proxies to our upstream mongrels
    if (-f $request_filename.html) {
    rewrite (.*) $1.html break;
    }
if (!-f $request_filename) { proxy_pass http://your_app; break; } } error_page 500 502 503 504 /500.html; location = /500.html { root /your_home_directory_path/apps/your_app/public; } }
  1. for ssl encrypted traffic, i.e. https
    server {
    listen 443;
    server_name your_app.com;
  1. Set the max size for file uploads
  2. we set this high here and manage it
  3. within our app’s system setting for max file size
    client_max_body_size 500M;
access_log logs/access.your_app.log main; error_log logs/error.your_app.log debug;
  1. doc root
    root /your_home_directory_path/apps/your_app/public;
  1. this rewrites all the requests to the maintenance.html
  2. page if it exists in the doc root. This is for capistrano’s
  3. disable web task
    if (-f $document_root/system/maintenance.html) {
    rewrite ^(.*)$ /system/maintenance.html last;
    break;
    }
location / {
  1. needed to forward user’s IP address to rails
    proxy_set_header X-Real-IP $remote_addr;
  1. needed for HTTPS
    proxy_set_header X_FORWARDED_PROTO https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect false; proxy_max_temp_file_size 0;
  1. If the file exists as a static file serve it directly without
  2. running all the other rewite tests on it
    if (-f $request_filename) {
    break;
    }
  1. check for index.html for directory index
  2. if its there on the filesystem then rewite
  3. the url to add /index.html to the end of it
  4. and then break to send it to the next config rules.
    if (-f $request_filename/index.html) {
    rewrite (.*) $1/index.html break;
    }
  1. this is the meat of the rails page caching config
  2. it adds .html to the end of the url and then checks
  3. the filesystem for that file. If it exists, then we
  4. rewite the url to have explicit .html on the end
  5. and then send it on its way to the next config rule.
  6. if there is no file on the fs then it sets all the
  7. necessary headers and proxies to our upstream mongrels
    if (-f $request_filename.html) {
    rewrite (.*) $1.html break;
    }
if (!-f $request_filename) { proxy_pass http://your_app; break; } } error_page 500 502 503 504 /500.html; location = /500.html { root /your_home_directory_path/apps/your_app/public; } }

}

Discuss This Topic

There are 0 comments in this discussion.

join this discussion

Creative Commons Attribution-Share Alike 3.0 New Zealand License
Example Nginx Configuration File Simple Installation for Nginx 0.6.x by Kieran P is licensed under a Creative Commons Attribution-Share Alike 3.0 New Zealand License