Tuesday, 28 January 2014

NGINX Configuration with PROXY_CACHE



"PROXY_CACHE is general-purpose cache with automatic lookups before PROXY_PASS, expiration support and so on.  It is usually what you need if you need caching capabilities. Lets see the configuration of proxy_cache on RHEL 5.x.
 
  • Enable GZip:

gzip  on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;   
gzip_buffers 16 8k;
gzip_disable “MSIE [1-6].(?!.*SV1)”;

  • Configure port:

listen       88;
server_name  localhost;          
access_log  logs/host.access.log  main;

  • Cache Path Setup:


proxy_buffering                      on
proxy_cache_path                   /usr/local/cache/proxy levels=1:2
keys_zone=aeon:10m inactive=30m
max_size=256m max_size=1500m inactive=600m;
            proxy_temp_path                    /usr/local/cache/temp;
            proxy_buffer_size                   4k;
            proxy_buffers                                     100 8k;
            client_body_timeout               10;
            client_header_timeout            10;
            keepalive_timeout                   5 5;
            send_timeout                          10;
expires                                     5m;

  • Proxy pass setup:


proxy_pass                              http://192.168.2.201:80;
                        proxy_cache                            aeon;   
                        proxy_cache_key                    $request_uri;
                        proxy_cache_valid                  200 301 302 20m;
                        proxy_cache_valid                  404 1m;
                        proxy_cache_valid                  any 15m;
                        proxy_cache_use_stale           error timeout invalid_header updating;

  • Prevent caching following url:


# This will prevent chaching for all urls matching this pattern.
                        location ~* ^/(.*)/(.*)/DiscountShop\(New\)/ {
                                    proxy_pass                  http://192.168.2.201:80;
                                    proxy_cache                 aeon;
                                    proxy_cache_valid      any 0m;
                                    expires                         0m;
                        }

  • Static file path:

location ~* (css|js|png|jpe?g|gif|ico|swf)$ {
            root D:/docroot/;
            expires max; 
}

Complete configuration files:

  1. nginx.conf


    #user  nobody;
    worker_processes  5;
    error_log  /usr/local/nginx/logs/error.log;
    pid        /usr/local/nginx/logs/nginx.pid;

    events {
        worker_connections  1024;
    }

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

            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  /usr/local/nginx/logs/access.log  main;

            sendfile        on;
            tcp_nopush     on;

            # Enable gzip for nginx
            gzip  on;
            gzip_http_version 1.1;
            gzip_vary on;
            gzip_comp_level 6;
            gzip_proxied any;
            gzip_buffers 16 8k;
      
            # Disable gzip for certain browsers.
            gzip_disable “MSIE [1-6].(?!.*SV1)”;
      
            # Proxy cache configurations.
            # proxy_cache does not work on Windows with ASLR. It works without any issues on Unixes
            #
            proxy_buffering on;
            proxy_cache_path /usr/local/cache/proxy levels=1:2 keys_zone=aeon:10m inactive=30m max_size=256m max_size=1500m inactive=600m;
            proxy_temp_path /usr/local/cache/temp;
            proxy_buffer_size 4k;
            proxy_buffers 100 8k;
            client_body_timeout 10;
            client_header_timeout 10;
            keepalive_timeout 5 5;
            send_timeout 10;

        # Cache timeout 5 minutes.
            expires    5m;

            server {
                listen       88;
                server_name  192.168.2.34;

                #charset koi8-r;

                access_log /usr/local/nginx/logs/host.access.log  main;
      
            # Use this to prevent caching pareticular url, send all requests through the proxy.
            location ~* ^/(.*)/(.*)/DiscountShop\(New\)/ {
                proxy_pass         http://192.168.2.201:80;
                proxy_cache             aeon;
                proxy_cache_valid       any 0m;
                expires            0m;
            }

                location / {
                include     conf.d/proxy.conf;

                # Do not cache static files. Read them from local docroot.
                # Set their expiry time to max, so they'll always use the browser cache after first request.
                # location ~* (swf|jp?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|TTF)$ {
                #     root /home/bvuser/docroot/;
                #     if (!-f /home/bvuser/docroot/${request_uri}) {
                #         proxy_pass         http://192.168.2.201:80;          
                #         proxy_cache             aeon;
                #         proxy_cache_valid       any 1d;
                #         expires            1d;
                #     }
                #     proxy_cache_valid 1d;
                #     expires max;
                # }

                       # Proxy cache configurations.
                # proxy_cache does not work on Windows with ASLR. It works without any issues on Unixes
                #      

                proxy_pass             http://192.168.2.201:80;
                proxy_cache                 aeon;  
                proxy_cache_key             $request_uri;
                proxy_cache_valid           200 301 302 20m;
                proxy_cache_valid           404 1m;
                proxy_cache_valid           any 15m;
                proxy_cache_use_stale       error timeout invalid_header updating;

                }  

                #error_page  404              /404.html;

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

  2. proxy.conf


    proxy_redirect          off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffers           32 4k;


No comments:

Post a Comment