Wordpress URL Rewrite for Nginx
By rockia on May 31, 2010 with Comments 2
Some people say WordPress works the best on LAMP (Linux, Apache, MySQL and PHP) but I chose LNMP as my server structure. If you have Nginx as your web server, you might be able to see tongs of “404 error” due to the incorrect URL rewrite rules.
To fix this problem, it’s just two simple steps
1. Submit a WordPress configuration file to Nginx server’s conf folder
You can copy the following code to a plain text file and submit it to Nginx’s conf folder, in my case, I named it wordpress.conf.
# if the requested file exists, return it immediately
if (-f $request_filename) {
break;
}
set $supercache_file '';
set $supercache_uri $request_uri;
if ($request_method = POST) {
set $supercache_uri '';
}
# Using pretty permalinks, so bypass the cache for any query string
if ($query_string) {
set $supercache_uri '';
}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
set $supercache_uri '';
}
# if we haven't bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
}
# only rewrite to the supercache file if it actually exists
if (-f $document_root$supercache_file) {
rewrite ^(.*)$ $supercache_file break;
}
# all other requests go to WordPress
if (!-e $request_filename) {
rewrite . /index.php last;
}
If you are tired of copy-n-paste, you can just download the conf file from my site. This file will even take care of the URL rewrite for the plugin “WP Supsercache”.
2. Add one line to nginx.conf to include the wordrpess.conf
Now you have wordpress.conf for Nginx, but you will need to let Nginx knows that this file should be load when server starts. This is easy, we only need to add one line to the nginx.conf:
user www www;
worker_processes 1;
error_log /home/logs/error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
#charse gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 128k;
large_client_header_buffers 4 256k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
output_buffers 4 32k;
postpone_output 1460;
#limit_zone crawler $binary_remote_addr 10m;
include wordpress.conf;
}
Line 46 above is the line I added into nginx.conf. So now whenever Nginx server starts, the wordpress.conf will be loaded automatically and the Nginx url rewrite rules should be corrected for your WordPress.
Filed Under: Programming • Servers
Hi!
I followed both your tutorials – LNMP Setup and this one, and am running into some problems now. It seems LNMP needs to be restarted for the changes to take effect, but this doesn’t work in my case. Every time I try to restart LNMP it shows ‘ok’, but checking the status shows ‘down’ and the site doesn’t resolve.
Even trying to revert to original configuration files gives the same response.
Any ideas of what’s going on.
Thanks for the tuts, btw.
Hi Albert, it’s kind of hard to diagnostic what the problem is exactly. Can you provide more information, like what’s the error printed on the screen or any other details? You can send your problem directly to my email (or use my contact page on the tab bar of my blog). I will help you take a look at it and see if I could help to resolve it.