# ------------------------------------------------------------------- # the contents of this file should be included in the http {} block of nginx.conf # ------------------------------------------------------------------- # configure cache log log_format cache '***$time_local ' '$upstream_cache_status ' 'Cache-Control: $upstream_http_cache_control ' 'Expires: $upstream_http_expires ' '$host ' '"$request" ($status) ' '"$http_user_agent" ' 'Args: $args ' 'Media: $media_flag ' 'Comment author email: $comment_author_email ' 'Comment author: $comment_author ' 'Wordpress logged in: $wordpress_logged_in ' ; access_log /var/log/nginx/cache.log cache; # set up backend with the addresses of the apache servers include /etc/nginx/app-servers.include; # gzip settings gzip on; gzip_disable msie6; gzip_static on; gzip_comp_level 2; gzip_proxied any; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; # proxy cache config proxy_cache_path /mnt/nginx_cache levels=1:2 keys_zone=one:10m inactive=7d max_size=10g; proxy_cache_path /mnt/nginx_cache_media levels=1:2 keys_zone=MEDIA:10m inactive=7d max_size=5g; proxy_temp_path /var/www/nginx_temp; server { listen 80; set $media_flag 0; # set headers for the backend proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # pass headers from backend to client proxy_pass_header Set-Cookie; # max upload size client_max_body_size 8m; # this block was in the default scalr nginx.conf if ( $remote_addr = 127.0.0.1 ) { rewrite ^(.*)$ /500.html last; #return 302; return 500; } # redirect non-www-prefixed domains to www-prefixed domains if ($host ~* ^([a-z0-9]+\.(com|net|org))$) { set $host_with_www www.$1; rewrite ^(.*)$ http://$host_with_www$1 permanent; } # capture cookies for use in the cache key set $wordpress_logged_in ""; set $comment_author_email ""; set $comment_author ""; if ($http_cookie ~* "wordpress_logged_in_[^=]*=([^%]+)%7C") { set $wordpress_logged_in wordpress_logged_in_$1; } if ($http_cookie ~* "comment_author_email_[^=]*=([^;]+)(;|$)") { set $comment_author_email comment_author_email_$1; } if ($http_cookie ~* "comment_author_[^=]*=([^;]+)(;|$)") { set $comment_author comment_author_$1; } # set the proxy cache key set $my_cache_key $scheme$host$uri$is_args$args$wordpress_logged_in$comment_author_email$comment_author; # proxy cache config for media files location ~* \.(gif|jpg|jpeg|png|js|css)$ { proxy_pass http://backend; proxy_cache MEDIA; proxy_cache_key $my_cache_key; proxy_cache_valid 200 302 304 30d; proxy_cache_valid 301 1h; proxy_cache_valid any 1m; proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_404; expires 30d; set $media_flag 1; } # proxy cache config for all other files location / { proxy_pass http://backend; proxy_cache one; proxy_cache_key $my_cache_key; proxy_cache_valid 200 302 304 10m; proxy_cache_valid 301 1h; proxy_cache_valid any 1m; proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_404; set $media_flag 0; } # Don't use the cache for the following locations location /wp-admin { proxy_read_timeout 300; proxy_pass http://backend; } location /wp-login.php { proxy_pass http://backend; } # Error pages location /500.html { root /var/www/nginx-default; } location /502.html { root /var/www/nginx-default; } }