hello everyone :) I am new to nginx and I am trying to setup the wordpress permalink structure that would normally be added to the .htaccess files.
here is the old rewrite rule from .htacces
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
and here is the current nginx site configuration:
server {
listen 80;
server_name *.noconformity.co;
rewrite ^ http://noconformity.co$request_uri? permanent;
}
server {
listen 80;
server_name noconformity.co ~(sub1|sub2)\.noconformity\.co$;
root /srv/www/www.noconformity.co;
access_log /var/log/nginx-hosts/www_noconformity.co.access;
error_log /var/log/nginx-hosts/www_noconformity.co.error error;
error_page 400 401 402 403 404 500 502 503 504 /error.htm;
location /error.htm {
internal;
}
# ------------------------------------------------------------------------
location / {
index index.html index.php index.htm;
location ~* ^.*\.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# ------------------------------------------------------------------------
# serve favicon
location = /favicon.ico {
log_not_found off;
access_log off;
}
# ------------------------------------------------------------------------
# process robots.txt
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# ------------------------------------------------------------------------
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
# ------------------------------------------------------------------------
# block access to .ht files
location ~ /\.ht {
deny all;
}
# ------------------------------------------------------------------------
# short cuts
if ($uri ~* "/login") {
rewrite ^/login(/.*)? /wp-admin$1;
}
# ------------------------------------------------------------------------
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ \.(css|js)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
expires 180s;
add_header Pragma "public";
add_header Cache-Control "max-age=180, public, must-revalidate, proxy-revalidate";
add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
# END W3TC Browser Cache
# BEGIN W3TC Skip 404 error handling by WordPress for static files
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
if ($request_uri ~ "(robots\.txt|sitemap(_index|[0-9]+)?\.xml(\.gz)?)") {
break;
}
if ($request_uri ~* \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) {
return 404;
}
# END W3TC Skip 404 error handling by WordPress for static files
# ------------------------------------------------------------------------
}
I have come across a few posts with similar examples but nothing is working so far. any pointers?
You should avoid using
if
directive if possible. The above Apache's rewrite rules can be convert to Nginx syntax as belows:There's already thorough documentation on Wordpress with Nginx: http://wiki.nginx.org/Wordpress
There's no reason to try and reinvent the wheel, and certainly no need for a compatibility module.
If you just want WordPress's permalink to work with nginx, you can try using the plugin for WP at http://wordpress.org/extend/plugins/nginx-compatibility/
after a ton more research I was able to get it working by using the following. Here is the domain's config:
with an include to all of the shared wordpress configurations:
basically I added the following block:
to the location block:
can this be done better? please let me know, I love learning more about nginx
after further review I have updated my answer to the following setup based on http://wiki.nginx.org/WordPress
site config:
and the wordpress.conf
please let me know what you think, I am really looking for feedback. if you have suggestions please let me know what to change in detail.