Nginx Reverse Proxy Redirect


Here’s an example conf file for Nginx doing a reverse proxy redirect and also a static content serving setup:

user  www-data www-data;
worker_processes  2;

events {
    worker_connections  1024;
    use epoll;
}

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name   somesite.com;
        # send all requests to https
	rewrite     ^(.*)   https://$server_name$1 permanent;

        location /resources {
            root   /var/www;
            index  index.html index.htm;
        }

	location / {
            proxy_pass http://127.0.0.1:881;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_max_temp_file_size 0;

            client_max_body_size       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         90;

            proxy_buffer_size           4k;
            proxy_buffers               4 32k;
            proxy_busy_buffers_size     64k;
            proxy_temp_file_write_size  64k;
        }

        error_page 403 404              /40x.html;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    server {
        listen       443;
        server_name  somesite.com;

	gzip on;

        ssl                  on;
        ssl_certificate      /root/cert/certificate.crt;
        ssl_certificate_key  /root/cert/certkey.key;

        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

        location /resources {
 	    root   /var/www;
            index  index.html index.htm;
	}

	location / {
            proxy_pass http://127.0.0.1:881;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_max_temp_file_size 0;

            client_max_body_size       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         90;

            proxy_buffer_size           4k;
            proxy_buffers               4 32k;
            proxy_busy_buffers_size     64k;
            proxy_temp_file_write_size 64k;
        }

	error_page   403 404          /40x.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
             root   html;
        }
	location = /40x.html {
             root   html;
	}                                                    
    }
}
Advertisements

2 thoughts on “Nginx Reverse Proxy Redirect

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s