Nginx and WordPress setup clean SEO friendly URLs

Open your nginx configuration file:

# nano /etc/nginx/nginx.conf

Locate your domain (example.com) virtual hosting configuration and before “location ~ \.php$ {” add following lines:

if (!-e $request_filename) {
  rewrite  ^(.*)$  /index.php?q=$1  last;
  break;
}

At the end your configuration should look as follows:

server {
    server_name     example.com;
    index           index.php;
    root            /var/www/example.com;

    if (!-e $request_filename) {
      rewrite  ^(.*)$  /index.php?q=$1  last;
      break;
    }  

    location ~ .php$ {
       fastcgi_pass  localhost:9999;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_NAME $fastcgi_script_name;
       fastcgi_param REQUEST_URI $request_uri;
       fastcgi_param DOCUMENT_URI $document_uri;
       fastcgi_param DOCUMENT_ROOT $document_root;
       fastcgi_param REMOTE_ADDR $remote_addr;
       fastcgi_param REMOTE_PORT $remote_port;
       fastcgi_param SERVER_ADDR $server_addr;
       fastcgi_param SERVER_PORT $server_port;
       fastcgi_param SERVER_NAME $server_name;
       fastcgi_param SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
       fastcgi_param QUERY_STRING $query_string;
       fastcgi_param REQUEST_METHOD $request_method;
       fastcgi_param CONTENT_TYPE $content_type;
       fastcgi_param CONTENT_LENGTH $content_length;
    }
}

Save and close the file. Restart nginx:

# /etc/init.d/nginx restart

Comments 9

  1. Hone wrote:

    How about a permanent redirect for all images in this directory to a different subdomain:

    http://www.domain.com/images/

    to:

    http://images.domain.com/images/

    ?

    Posted 23 Jul 2007 at 7:53 am
  2. Aleksandar wrote:

    Add this line in http://www.domain.com configuration:

    rewrite  ^/images(.*)$  http://images.domain.com/images1  permanent;
    
    Posted 23 Jul 2007 at 11:43 pm
  3. Gerry Kirk wrote:

    You’ve covered the bases for a lot of configuration needs all in one simple page. Thanks so much!

    Posted 31 Aug 2007 at 4:08 pm
  4. Ales wrote:

    I dunno, but this is the only version of the rewrite that worked for me

    server {
    listen 80;
    charset utf-8;
    server_name xx.xx.com;
    access_log /var/log/nginx/blogi.vest.si.access.log main;
    root /var/www/html/xx.xx.com;
    index index.php;

    # rewrite rule - fancy urls for wordpress mu 1.25 rewrite ^.*/files/(.*) /wp-content/blogs.php?file=$1;
    if (!-e $request_filename) {
    rewrite ^.+?(/wp-.*) $1 last;
    rewrite ^.+?(/.*\.php)$ $1 last;
    rewrite ^(.+)$ /index.php?q=$1 last;
    break;
    }
    …}

    Everything else resulted in “No input file found.”

    Posted 18 Oct 2007 at 3:34 pm
  5. Aleksandar wrote:

    Hi, Ales
    Look again, you might have missed something…
    My configuration is valid. :)

    Posted 19 Oct 2007 at 2:46 am
  6. Aleksandar wrote:

    Ah.. I just noticed, based on your input, you are running wordpress mu..
    This is for wordpress not for wordpress mu.

    Posted 19 Oct 2007 at 2:56 am
  7. Andrew CHalkley wrote:

    Thank you for your help! Worked a treat!

    Posted 26 Apr 2008 at 3:59 pm
  8. G wrote:

    Thanks for your help.

    Posted 18 May 2008 at 5:01 pm
  9. Dilip P wrote:

    this is my config file and i’ve not been able to get it working

    server {

    listen 80;
    server_name http://www.domain.in;
    rewrite ^/(.*) http://domain.in permanent;

    }

    server {

    listen 80;
    server_name domain.in;

    access_log /home/pdilip/public_html/domain.in/log/access.log;
    error_log /home/pdilip/public_html/domain.in/log/error.log;

    location / {
    root /home/pdilip/public_html/domain.in/public/;
    index index.html index.htm index.php;

    }

    location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/pdilip/public_html/domain.in/public/$fastcgi_script_name;

    }

    }

    Posted 06 Jul 2008 at 5:06 pm

Trackbacks & Pingbacks 1

  1. From links for 2008-04-16 « The Inscription on 16 Apr 2008 at 8:41 am

    [...] Aleksandar Savic - Nginx and WordPress setup clean SEO friendly URLs Open your nginx configuration file: (tags: Wordpress Nginx) [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *