Update: new sections in the nginx config for handling the wordpress admin section!
Update: new upstart script.
Update Feb 9, 2016 No longer need ngx_cache_purge compiled in, Ubuntu 14.04+ specific.
In honor of Antirez’ fantastic post here: On the web server scalability and speed are almost the same thing, I’m sharing my Nginx configuration for WordPress Network (previously WordPress-Mu).
I’m assuming you are running ubuntu and you know the basics of system administration. I’m also assuming you’re using some sort of fastcgi backend. I suggest this excellent howto from interfacelab as an excellent starting point.
First, go get the nginx mainline PPA. Might need to install common-software-properties first.
|
1 2 3 |
sudo apt-get install -y software-properties-common apt-add-repository -y ppa:nginx/development apt-get update && apt-get install -y nginx-extras |
Next,in your nginx.conf, add this line in the http section:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
http { ... #this bit here keeps filesystem stats down to a minimum # at the expense of some ram. open_file_cache max=5000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; #ramming speed! fastcgi_cache_path /dev/shm/nginx levels=1:2 keys_zone=WORDPRESS:32m inactive=64m; ... } |
This creates a cache of 64MB. It’s stored in memory, so be sure you’ve got overhead. If you’re running a blog, unless you’ve got HUGE files, it’s unlikely you’ll need this much cache, and you can probably safely pare this down further to save memory.
And the final piece, the server configuration for nginx. Note the usage of the variable $nocache, and also the purge location.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
server { listen [::]:80; # both ipv4 and ipv6! server_name .example.com; # if you are using subdomains gzip on; gzip_http_version 1.1; gzip_vary on; gzip_comp_level 9; gzip_proxied any; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_buffers 16 8k; gzip_disable "MSIE [1-6].(?!.*SV1)"; client_max_body_size 8M; client_body_buffer_size 128k; access_log /var/log/nginx/access.log; keepalive_timeout 10; root /var/www/wordpress; index index.php; #on server block ##necessary if using a multi-site plugin server_name_in_redirect off; ##necessary if running Nginx behind a reverse-proxy # port_in_redirect off; #this is if you use w3total-cache and want to use it's minification #features with rewrites instead of get variables for a cdn. location ~* \.(css|js) { if (!-f $request_filename) { rewrite ^/wp-content/w3tc(.*?)/min/w3tc_rewrite_test$ /wp-content/w3tc$1/min/index.php?w3tc_rewrite_test=1 last; rewrite ^/wp-content/w3tc(.*?)/min/(.+\.+.(css|js))$ /wp-content/w3tc$1/min/index.php?file=$2 last; rewrite ^/wp-content/w3tc(.*?)/min/(.+\.(css|js))$ /wp-content/w3tc$1/min/index.php?file=$2 last; expires 365d; } } # handle static files. location / { try_files $uri @wordpress; if_modified_since before; #keeps it new for the user expires 30d; # client side caching } # ugly but necessary regex. location @wordpress { rewrite ^.*/files/(.*) /wp-includes/ms-files.php?file=$1; if (!-e $request_filename) { rewrite ^.+/?(/wp-.*) $1 last; rewrite ^.+/?(/.*\.php)$ $1 last; rewrite ^(.+)$ /index.php?q=$1 last; } # use browser side caching for these expires 30d; add_header Cache-Control private; } # enable purging of stale content. # see this plugin: # http://wordpress.org/extend/plugins/nginx-proxy-cache-purge/ # activate it network wide location ~ /purge(/.*) { fastcgi_cache_purge WORDPRESS $host$1$request_method; allow SOME.IP.ADD.RESS; deny all; } location ~ \.php$ { # Zero-day exploit defense # http://forum.nginx.org/read.php?2,88845,page=3 # Won't work properly (404 error) if the file is not stored on this #server, which is entirely possible with php-fpm/php-fcgi. # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on #another machine. And then cross your fingers that you won't get hacked. try_files $uri =404; # checks to see if the visitor is logged in, a commenter, # or some other user who should bypass cache set $nocache ""; if ($http_cookie ~ (comment_author_.*|wordpress_logged_in.*|wp-postpass_.*)) { set $nocache "Y"; } #the usual fastcgi pass, I like unix sockets, less resource contention fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param REMOTE_ADDR $http_client_ip; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_pass unix:/tmp/php5-fastcgi.socket; # here is the fastcgi caching magic: # bypass cache if logged in. # Be sure that this is above all other fastcgi_cache directives fastcgi_no_cache $nocache; fastcgi_cache_bypass $nocache; # use stale cache if the backend fails. fastcgi_cache_use_stale error timeout invalid_header http_500; # cache key is based on the host and the uri, you could add other # key variables here as it suites your needs fastcgi_cache_key $host$request_uri$request_method; fastcgi_cache WORDPRESS; # what to cache, and for how long fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 1m; } location /nginx_status { # copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/ stub_status on; access_log off; allow SOME.IP.ADD.RESS; deny all; } } |
Be sure to install the nginx_proxy_cache_purge plugin, and activate it network wide. And that’s it!
Just for kicks, try:
|
1 |
ab -c 1000 -n 10000 http://yourdomainhere.com/ |
Post your results in the comments!
If you happen to be using a recent ubuntu, try this out for your upstart script
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
description "nginx http daemon" author "George Shammas " start on (filesystem and net-device-up IFACE=lo) stop on runlevel [!2345] env DAEMON=/usr/local/bin/nginx env PID=/var/run/nginx.pid expect fork respawn pre-start script $DAEMON -t if [ $? -ne 0 ] then exit $? fi end script post-stop script start-stop-daemon --stop --pidfile $PID --name nginx --exec $DAEMON --signal TERM end script exec $DAEMON |
|
1 |
Home
34 Comments
Pingback: Webserver Extrem-Tuning (für z.B. Wordpress). Teil 1: nginx, php-fpm & php-apc installieren | Kristian Söhl
Pingback: WordPress deployment: super simple and super fast with nginx caching » gehrcke.de
Pingback: Nginx + fastcgi_cache 加速 | WP奇才