easyengine (ee) note: If you are using easyengine, you can accomplish everything in this article using following commands:
ee site create example.com --wpsc
Assumption:
- You already installed PHP, MySQL, Nginx & Postfix
- You already installed a fresh WordPress or moved an existing WordPress to current server
Based on these assumptions, we will jump to directly a WordPress-Nginx configuration part.
Standard WordPress-Nginx configuration with WP Super cache support:
As you are getting into Nginx, I hope you don’t need my help with configuring WP Super Cache plugin.
Still, make sure you are using “Use mod_rewrite to serve cache files. (Recommended)” option under “Advanced” tab.
Following configuration supports:
- Static Page Caching using Disk
- Direct browser cache for static content like images, js, css, etc
server { server_name example.com www.example.com; access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log debug; root /var/www/example.com/htdocs; index index.php; set $cache_uri $request_uri; # POST requests and urls with a query string should always go to PHP if ($request_method = POST) { set $cache_uri 'null cache'; } if ($query_string != "") { set $cache_uri 'null cache'; } # Don't cache uris containing the following segments if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $cache_uri 'null cache'; } # Don't use the cache for logged in users or recent commenters if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") { set $cache_uri 'null cache'; } # Use cached or actual file if they exists, otherwise pass request to WordPress location / { try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; } location ~ \.php$ { try_files $uri =404; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; } # Cache static files for as long as possible location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { expires max; log_not_found off; access_log off; } }
Note:
Don’t Forget:
Always test your Nginx configuration and then reload it. All changes to Nginx config must be followed with these commands:
nginx -t && service nginx reload
Important Note:
WP Super Cache caching will conflict with plugins that uses query vars. WooCommerce is an example known plugin known to work with above configuration. Reason is following line:
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
It doesn’t passes $args to /index.php. If you set last try_files argument to /index.php?$args
as with other WordPress-Nginx configuration, WP Super Cache, itself will break!
Must Read:
- Checklist For Perfect WordPress-Nginx Setup – It will help you verify if your caching will work after PHP/MySQL crash.
- Catch the complete list of WordPress-Nginx tutorials here.
Thank you very much.
How can i know the file is serving from cache content?
Toufiq, look at the page source code on the bottom and you’ll find a comment like that:
It only has to be enabled via admin first. But since it is a default configuration, you’re good to go.
You can go throuh complete checklist here – http://rtcamp.com/wordpress-nginx/tutorials/checklist/
Can you please write a guide for the people who are not familiar with Linux and command line? Is there any tutorial for installing Nginx using cPanel? I am using wp super cache and i want to add Nginx to my website but i don’t know how to install Nginx. I am looking for a tutorial to install it through cPanel.
Sorry to say but using cpanel with Nginx is wrong thing to do. Cpanel itself is so bloated!
If you want easier method try using http://rtcamp.com/easyengine with a VPS hosting like DigitalOcean
Using easyengine – you can get wordpress-nginx sites up & running using 1-line of commands.
Thanks. I am very impressed with the ease and speed at which the nginx site was enabled with wp-super cache. Even the permalinks work without any change to the configuration. Two issues:
(i) Can you pl indicate the configuration changes required if wordpress is installed in a folder and not in the root.
(ii) the domain does not resolve to www. While domain.com works, http://www.domain.com does not. I think it requires a vhost change.
My point on the www domain not resolving has nothing to do with the vhost config because it already includes that. It has to be set up at the domain registrar (namecheap). I did that and everything works well now.
I think having the wordpress in a folder requires a slight modification to the configuration. I am experimenting with that at the moment and shall revert.
The solution to enabling permalinks to a wordpress installation in a folder is to add the following location block to the vhost configuration file (to be found at /etc/nginx/sites-available/mydomain.com
Restart nginx with the command
Glad to see you found solution for subdir setup. It’s correct one.
Also, for www-issue, you may like to add www to non-www redirect (or other way round) – http://rtcamp.com/tutorials/nginx/www-non-www-redirection/
hello sir thanks for the tips
i have install nginx and add the configuration above and active my wp super cache plugin (im using super cache config same as hostgator community suggestion)
after all done, i still view this message on top admin
–Mod rewrite may not be installed!
It appears that mod_rewrite is not installed. Sometimes this check isn’t 100% reliable, especially if you are not using Apache. Please verify that the mod_rewrite module is loaded. It is required for serving Super Cache static files. You will still be able to use legacy or PHP modes.
Warning! Garbage collection is not scheduled!
Garbage collection by this plugin
—
should i ignrore those message ? because i already check caching method on your post here https://rtcamp.com/wordpress-nginx/tutorials/checklist/
and i see the time stamp are remain the same
how about garbage collection ? do i need to cleanup on sheceduler time ?
thank you
You can ignore that message as long as your cache test is working properly.
Also, try installing http://wordpress.org/plugins/nginx-helper/
It should fix that message.