Skip to content

Instantly share code, notes, and snippets.

@denji
Created January 2, 2015 08:28
Show Gist options
  • Select an option

  • Save denji/3a0ed1d0af17075a0aff to your computer and use it in GitHub Desktop.

Select an option

Save denji/3a0ed1d0af17075a0aff to your computer and use it in GitHub Desktop.

Revisions

  1. denji revised this gist Jan 2, 2015. 1 changed file with 175 additions and 18 deletions.
    193 changes: 175 additions & 18 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,29 @@

    # for debugging with valgrind
    #daemon off;
    #master_process off;

    #user nobody;
    worker_processes 1;

    worker_rlimit_core 500M;
    working_directory /tmp/nginx_cores/;

    #error_log logs/error.log;
    error_log logs/error.log;
    #error_log logs/error.log notice;
    error_log logs/error.log debug;
    #error_log logs/error.log debug;

    #pid logs/nginx.pid;


    events {
    worker_connections 1024;
    }


    http {

    map_hash_max_size 65536;

    include mime.types;
    #default_type application/octet-stream;
    default_type text/html;
    @@ -28,6 +34,24 @@ http {
    #keepalive_timeout 0;
    keepalive_timeout 65;

    proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=htmlcache:10m;
    # When combined with proxy_hide_header and 'add_header Cache-Control'
    # below, this mapping allows us to override html caching headers while
    # leaving them alone on other types of content.
    map $upstream_http_content_type $new_cache_control_header_val {
    default $upstream_http_cache_control;
    "~*text/html" "no-cache, max-age=0";
    }

    map $uri $new_post_url {
    default no-redirect;
    /news/index /p/index;
    /news/all /p/index;
    include /usr/local/nginx/conf/nginx.redirects.conf;
    }



    # enable gzip:
    gzip on;
    gzip_vary on;
    @@ -49,29 +73,152 @@ http {

    gzip_http_version 1.0;

    pagespeed on;

    #pagespeed FileCacheCleanIntervalMs 10000000000;
    #pagespeed FileCacheCleanIntervalMs 1000;

    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    pagespeed RewriteLevel CoreFilters;
    #pagespeed MapProxyDomain http://www.jefftk.com:8050/ http://www.jefftk.com/;
    pagespeed AnalyticsID "UA-27645543-1";
    pagespeed EnableFilters insert_ga,trim_urls;

    server {
    listen 80;
    server_name jefftk.com;
    rewrite ^(.*) http://www.jefftk.com$1 permanent;
    rewrite ^(.*) http://www.jefftk.com$1 permanent;
    }

    server {
    if ($new_post_url !~ "no-redirect") {
    rewrite ^ $new_post_url permanent;
    }

    # This server represents the external caching layer server which
    # receives user requests and proxies them to the upstream server
    # running on 8090 when the response is not available in
    # the cache. It also services purge requests from the upstream server.
    listen 80;
    server_name www.jefftk.com;
    #pagespeed FileCachePath /var/ngx_pagespeed_cache;
    #pagespeed off;

    #normalize all accept-encoding headers to just gzip
    set $gzip_support "0";
    if ($http_accept_encoding ~* gzip) {
    set $gzip_support "1";
    }

    # Block 2: Define prefix for proxy_cache_key based on the UserAgent.
    set $ua_dependent_ps_capability_list "";
    set $bypass_cache 1;
    if ($http_user_agent ~* "Chrome/|Firefox/|MSIE |Safari|Wget") {
    # User Agents that support lazyload-images (ll), inline-images (ii) and
    # defer-javascript (dj).
    set $ua_dependent_ps_capability_list "ll,ii,dj:";
    set $bypass_cache 0;
    }
    if ($http_user_agent ~*
    "Chrome/[2][3-9]+\.|Chrome/[[3-9][0-9]+\.|Chrome/[0-9]{3,}\.") {
    # User Agents that support lazyload-images (ll), inline-images (ii),
    # defer-javascript (dj), webp (jw) and webp-lossless (ws).
    set $ua_dependent_ps_capability_list "ll,ii,dj,jw,ws:";
    set $bypass_cache 0;
    }

    # All User Agents that represent
    # 1) mobiles
    # 2) tablets
    # 3) desktop browsers that do not have defer-javascript capability at a minimum
    # are made to go to the pagespeed server directly bypassing the proxy_cache.
    if ($http_user_agent ~* "Firefox/[1-2]\.|MSIE [5-8]\.") {
    set $ua_dependent_ps_capability_list "";
    set $bypass_cache 1;
    }
    if ($http_user_agent ~* "Mozilla.*Android.*Mobile*|iPhone|BlackBerry|Opera Mobi|Opera Mini|SymbianOS|UP.Browser|J-PHONE|Profile/MIDP|portalmmm|DoCoMo|Obigo") {
    # These are Mobile User Agents. We don't cache responses for these.
    set $ua_dependent_ps_capability_list "";
    set $bypass_cache 1;
    }
    if ($http_user_agent ~* "Android|iPad|TouchPad|Silk-Accelerated|Kindle Fire") {
    # These are Tablet User Agents. We don't cache responses for these.
    set $ua_dependent_ps_capability_list "";
    set $bypass_cache 1;
    }

    # Skip the cache for .pagespeed. resource. PageSpeed has its own
    # cache for these.
    if ($uri ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+") {
    set $bypass_cache 1;
    }

    # Block 3: Location block for purge requests.
    location ~ /purge(/.*) {
    allow 127.0.0.1;
    allow 209.20.65.89; # our ip: www.jefftk.com
    deny all;
    proxy_cache_purge htmlcache $gzip_support$ua_dependent_ps_capability_list$1$is_args$args;
    }

    # Block 4: Proxy everything except purge requests (above) in to PageSpeed.
    location / {
    proxy_pass http://www.jefftk.com:8090;

    proxy_set_header Host $http_host;
    proxy_cache htmlcache;
    add_header X-Cache $upstream_cache_status;
    proxy_cache_key $gzip_support$ua_dependent_ps_capability_list$uri$is_args$args;
    proxy_cache_bypass $bypass_cache;

    # Change control headers from the upstream, after using them to
    # decide how long we should cache here.
    proxy_hide_header Cache-Control;
    add_header Cache-Control $new_cache_control_header_val;

    # Everything is either html, in which case it's uncacheable by
    # external caches, or longcached, in which case etag and
    # last-modified are not needed.
    proxy_hide_header ETag;
    proxy_hide_header Last-Modified;
    }
    }

    server {
    listen 80;
    listen 8090;
    server_name www.jefftk.com;
    root /var/www;
    index index.html;

    add_header Cache-Control "max-age=600";

    pagespeed on;

    location /test {
    pagespeed off;
    }

    location /jscss {
    pagespeed off;
    }

    #pagespeed InPlaceResourceOptimization on;

    #pagespeed FileCacheCleanIntervalMs 10000000000;
    #pagespeed FileCacheCleanIntervalMs 1000;

    #pagespeed MetadataCacheStalenessThresholdMs 60000;

    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    pagespeed RewriteLevel CoreFilters;
    #pagespeed MapProxyDomain http://www.jefftk.com:8050/ http://www.jefftk.com/;
    pagespeed AnalyticsID "UA-27645543-1";
    pagespeed EnableFilters add_instrumentation,remove_quotes;
    pagespeed EnableFilters insert_ga,trim_urls;
    pagespeed EnableFilters convert_jpeg_to_webp;

    pagespeed RunExperiment off;
    pagespeed ExperimentSpec "id=1;percent=50;default";
    pagespeed ExperimentSpec "id=2;percent=50";

    pagespeed Statistics on;
    pagespeed StatisticsLogging on;
    pagespeed LogDir /var/log/pagespeed;

    #pagespeed ModifyCachingHeaders off;

    pagespeed DownstreamCachePurgeMethod "GET";
    pagespeed DownstreamCachePurgeLocationPrefix "http://www.jefftk.com/purge";

    location ~ \.html$ {
    rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
    }
    @@ -85,10 +232,20 @@ http {
    }
    location /wsgi {
    proxy_pass http://127.0.0.1:8080/wsgi/;
    pagespeed off;
    }
    location ~ "\.pagespeed\.[a-z]{2}\.[^.]{10}\.[^.]+" { }

    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { }
    location ~ "^/ngx_pagespeed_static/" { }
    location ~ "^/ngx_pagespeed_beacon$" { }
    #location /ngx_pagespeed_statistics {
    # allow 127.0.0.1;
    # deny all;
    #}
    #location /ngx_pagespeed_message {
    # allow 127.0.0.1;
    # deny all;
    #}

    # redirect server error pages to the static page /50x.html
    #
  2. denji created this gist Jan 2, 2015.
    100 changes: 100 additions & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@

    #user nobody;
    worker_processes 1;

    worker_rlimit_core 500M;
    working_directory /tmp/nginx_cores/;

    #error_log logs/error.log;
    #error_log logs/error.log notice;
    error_log logs/error.log debug;

    #pid logs/nginx.pid;


    events {
    worker_connections 1024;
    }


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

    sendfile on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout 65;

    # enable gzip:
    gzip on;
    gzip_vary on;

    # Turn on gzip for all content types that should benefit from it.
    gzip_types application/ecmascript;
    gzip_types application/javascript;
    gzip_types application/json;
    gzip_types application/pdf;
    gzip_types application/postscript;
    gzip_types application/x-javascript;
    gzip_types image/svg+xml;
    gzip_types text/css;
    gzip_types text/csv;
    # "gzip_types text/html" is assumed.
    gzip_types text/javascript;
    gzip_types text/plain;
    gzip_types text/xml;

    gzip_http_version 1.0;

    pagespeed on;

    #pagespeed FileCacheCleanIntervalMs 10000000000;
    #pagespeed FileCacheCleanIntervalMs 1000;

    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    pagespeed RewriteLevel CoreFilters;
    #pagespeed MapProxyDomain http://www.jefftk.com:8050/ http://www.jefftk.com/;
    pagespeed AnalyticsID "UA-27645543-1";
    pagespeed EnableFilters insert_ga,trim_urls;

    server {
    listen 80;
    server_name jefftk.com;
    rewrite ^(.*) http://www.jefftk.com$1 permanent;
    }

    server {
    listen 80;
    server_name www.jefftk.com;
    root /var/www;
    index index.html;

    location ~ \.html$ {
    rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
    }

    location ~ ^/?\+$ {
    rewrite ^ https://plus.google.com/103013777355236494008/about permanent;
    }

    location / {
    try_files $uri.html $uri $uri/ =404;
    }
    location /wsgi {
    proxy_pass http://127.0.0.1:8080/wsgi/;
    }

    location ~ "\.pagespeed\.[a-z]{2}\.[^.]{10}\.[^.]+" { }
    location ~ "^/ngx_pagespeed_static/" { }

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