nginx ADVANCED config. Trying to do better caching and trying to detect logged-in users. I also have a nginx reverse proxy config ready, to use it in front of apache, but i prefer to stick with this first. Pls notice that this config is not yet officially supported so use at your own risk. pls review and share your suggests. I've implemented 2 caching strategies: server side fast_cgi microcaching (10seconds) and client side expires set to 2hrs for pages.
Before dealing with caching and other advanced settings do read the NGINX admin guide here: http://nginx.com/resources/admin-guide/
(updated Apr. 2014, and tested with GS 3.3.1)
Before dealing with caching and other advanced settings do read the NGINX admin guide here: http://nginx.com/resources/admin-guide/
Code:
## define here or in /etc/nginx/nginx.conf if you have multiple sites sharing the same cache
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=GETSIMPLE:10m inactive=15m;
##
map $http_cookie $logged_in {
default 0;
~GS_ADMIN_USERNAME 1; # This will return true if user is logged in
}
server {
listen 80;
server_name *.mysite.org *.mysite.com;
return 301 http://mysite.org$request_uri;
}
server {
listen 80;
server_name mysite.org;
charset utf-8;
access_log off;
root /var/www/mysite.org;
index index.php;
location / {
try_files $uri $uri/ /index.php?id=$uri&$args;
fastcgi_cache_bypass $logged_in;
fastcgi_no_cache $logged_in;
}
location ~* /admin/.*\.php$ {
try_files $uri /admin/index.php?id=$uri&$args;
include /etc/nginx/fastcgi_params;
fastcgi_cache_bypass $logged_in;
fastcgi_no_cache $logged_in;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/www/php.sock;
}
location ~* \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
try_files $uri =404;
fastcgi_cache_bypass $logged_in;
fastcgi_no_cache $logged_in;
fastcgi_pass unix:/var/run/www/php.sock;
fastcgi_cache GETSIMPLE;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_valid 200 302 10s;
fastcgi_cache_valid 404 2m;
expires 2h;
}
location ~* \.(?:ico|js|gif|jpg|png)$ {
expires 14d;
}
location ~* \.(htm|css|html)$ {
expires 2d;
}
# this blocks direct access to the XML files (but sitemap.xml) - that hold all the data
location ~* \.xml$ { deny all; }
location ~* \.xml\.bak$ { deny all; }
location = /sitemap.xml { allow all; }
# this prevents hidden files (beginning with a period) from being served
location ~ /\. { deny all; }
location ^~ /uploads/ {
if ($request_uri ~* \.php$) {return 403;}
}
}
(updated Apr. 2014, and tested with GS 3.3.1)