Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

6 Answers 6

You should avoid using if directive if possible. The above Apache's rewrite rules can be convert to Nginx syntax as belows:

try_files $uri $uri/ /index.php;
share|improve this answer
    
I came across that rule late last night, but could not post the solution, what are your thoughts on my updated answer? –  chrishough Oct 3 '11 at 14:11
    
I just updated the current setup I have, if you have time, take a peak, let me know what you think :) I really appreciate it. –  chrishough Oct 4 '11 at 3:49

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.

share|improve this answer
    
I tried starting with that, and it never worked. keeps failing on the include fastcgi.conf; –  chrishough Oct 3 '11 at 14:09
    
If you look at your own config you'll see you have include fastcgi_params; and thus your naming is different. The wiki page is obviously meant to teach not to be something you can just copy-paste and not think about at all. –  Martin Fjordvald Oct 3 '11 at 17:31
    
I just updated the current setup I have, if you have time, take a peak, let me know what you think :) I really appreciate it. –  chrishough Oct 4 '11 at 3:49

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/

share|improve this answer
    
i had not seen that plugin before, I am going to have to take a peak at that and I will let you know how it goes –  chrishough Oct 3 '11 at 14:12
    
I just updated the current setup I have, if you have time, take a peak, let me know what you think :) I really appreciate it. –  chrishough Oct 4 '11 at 3:49

after a ton more research I was able to get it working by using the following. Here is the domain's config:

server {
  listen 80;
  server_name *.noconformity.co 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;
  }
  # ------------------------------------------------------------------------
  # remove all sub domains and www
    if ($http_host !~* "^(sub1|sub2)\.noconformity\.co$"){
        set $rule_0 1$rule_0;
    }
    if ($http_host ~ "^([^.]+)\.noconformity\.co$"){
        set $rule_0 2$rule_0;
    }
    if ($rule_0 = "21"){
        rewrite ^/(.*)$ http://noconformity.co/$1 permanent;
        break;
    }

    include /etc/nginx/sites-available/wordpress.conf;

}

with an include to all of the shared wordpress configurations:

# ------------------------------------------------------------------------
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;
        }
    try_files $uri $uri/ /index.php?$args;
}
# ------------------------------------------------------------------------
# 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;
}
# ------------------------------------------------------------------------
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
}
# ------------------------------------------------------------------------
# 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
# ------------------------------------------------------------------------

basically I added the following block:

try_files $uri $uri/ /index.php?$args;

to the location block:

# ------------------------------------------------------------------------
    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;
            }
        try_files $uri $uri/ /index.php?$args;
    }

can this be done better? please let me know, I love learning more about nginx

share|improve this answer
up vote 0 down vote accepted

after further review I have updated my answer to the following setup based on http://wiki.nginx.org/WordPress

site config:

server {
  listen 80;
  server_name *.noconformity.co 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;
  }
    # ------------------------------------------------------------------------
  # remove all sub domains and www
    if ($http_host !~* "^(sub1|sub2)\.noconformity\.co$"){
        set $rule_0 1$rule_0;
    }
    if ($http_host ~ "^([^.]+)\.noconformity\.co$"){
        set $rule_0 2$rule_0;
    }
    if ($rule_0 = "21"){
        rewrite ^/(.*)$ http://noconformity.co/$1 permanent;
        break;
    }

    include /etc/nginx/sites-available/wordpress.conf;

}

and the wordpress.conf

# ------------------------------------------------------------------------
location / {
    index index.html index.php index.htm;
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include fastcgi_params;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_intercept_errors on;
            fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        }
        try_files $uri $uri/ /index.php?$args;
}
# ------------------------------------------------------------------------
# Login Short Cut
if ($uri ~* "/login") {
    rewrite ^/login(/.*)? /wp-admin$1;
}
# ------------------------------------------------------------------------
location = /favicon.ico { 
    log_not_found off;
    access_log off;
}
# ------------------------------------------------------------------------
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}
# ------------------------------------------------------------------------
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
}
# ------------------------------------------------------------------------
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}

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.

share|improve this answer

If you are using Webmin, then you need to go to servers and edit the configuration file and add this.

try_files $uri $uri/ /index.php?$args;

I tried adding this line manually but it did not work either

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.