Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Reverse Proxy SSL / Minification

I am trying to use NginX as a reverse proxy for a few IIS Servers. The goal is to have NginX sit in from of the IIS / Apache servers caching static items such as CSS / JS / Images. I am also trying to get NginX to automatically minify js / css files using its perl module.

I found a sample script for minification here:

http://petermolnar.eu/linux-tech-coding/nginx-perl-minify-css-js/

With the scrip everything works fine, except the reverse proxy breaks.

Questions:

  1. Is what i am trying to accomplish even possible? I want NginX to first minify the scripts before saving them to cache.
  2. Can nginX automtically set the proper expires headers so that static items are cached as long as possible, and only replaced when querystrings are changed (jquery.js?timestamp=march-2012)
  3. Can NginX GZIP the resources before sending them out.
  4. Can NGinx Forward requests or serve up a "Down For Maintenance page" if it cannot connec to back end server.

Any help would be greatly appreciated.

Here is what i have in my sites-enabled/default so far.

    server {


    location / {

        proxy_pass             http://mywebsite.com;
        proxy_set_header       Host $host;
        proxy_cache            STATIC;
        proxy_cache_valid      200  1d;
        proxy_cache_use_stale  error timeout invalid_header updating
                              http_500 http_502 http_503 http_504;
    }


    location @minify {
                    perl Minify::minify_handler;
            }

            location ~ \.css$ {
                    try_files $uri.min.css @minify;
            }




            location /*.js {
                 expires 30d;
            }



}
like image 659
Frank Avatar asked Jul 12 '12 00:07

Frank


1 Answers

Nginx is the ideal solution for reverse-proxy, it's also Unix way "do one thing and do it well". So I'd advice you to split content serve and minification process out instead of using third-party plugins to do many things at once.

Best practice is to do minify&obfuscate phase on local system before you do a deployment on production, this is easy to say and not hard to do, see the google way to compress static assets. Once you got assets ready-to-use, we can setup nginx configuration.

Answers:

  1. use minify&obfuscate before deploy it on production

  2. you can find assets by regexp (directory name or file extension)

    location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ { gzip_static on; expires max; add_header Cache-Control public; add_header Last-Modified ""; add_header ETag ""; break; }

  3. use gzip on and gzip_static on to serve gzipped files instead of compress it every time when request is coming.

  4. use try_files to detect the maintenance page exists or not

    try_files $uri /system/maintenance.html @mywebsite;

    if (-f $document_root/system/maintenance.html) { return 503; }

See the full nginx config for your case:

http {
  keepalive_timeout         70;

  gzip                      on;
  gzip_http_version         1.1;
  gzip_disable              "msie6";
  gzip_vary                 on;
  gzip_min_length           1100;
  gzip_buffers              64 8k;
  gzip_comp_level           3;
  gzip_proxied              any;
  gzip_types                text/plain text/css application/x-javascript text/xml application/xml;

  upstream mywebsite {
    server                  192.168.0.1 # change it with your setting
  }

  server {
    try_files               $uri /system/maintenance.html @mywebsite;

    location @mywebsite {
      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_pass            http://mywebsite;
    }

    location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
      gzip_static       on;
      expires           max;
      add_header        Cache-Control public;
      add_header        Last-Modified "";
      add_header        ETag "";
      break;
    }

    if (-f $document_root/system/maintenance.html) {
      return            503;
    }

    location @503 {
      error_page 405 = /system/maintenance.html;
      if (-f $document_root/system/maintenance.html) {
        rewrite         ^(.*)$ /system/maintenance.html break;
      }
      rewrite           ^(.*)$ /503.html break;
    }

  }

}
like image 103
Anatoly Avatar answered Sep 21 '22 09:09

Anatoly