Nginx and WordPress permalinks are broken if blog is not in root

Ahmet Alp Balkan asked:

So I have installed nginx and WordPress on my server. However, my blog is not in domain root. (e.g. example.com/blog and contents are in /var/www/example.com/blog)

I am using settings below in nginx and my WordPress installation was using url rewrite rules in Apache. (e.g. permalinks like example.com/blog/2012/hello-world)

However, interestingly, all these URL rewrite rules go into index.php in / (not in blog/). I want to fix that but I could not figure out how. Another interesting thing is, example.com/blog (home page) works fine.

For example, I guess since example.com/blog/2012/hello-world is not a real file, try_files is executing example.com/index.php. (The url is remains same but that index.php gets executed). Any help is appreciated!

server {
    listen   80;

    root /var/www/example.com;
    index index.php;

    server_name 192.34.59.214;

    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9$
    location ~ .php$ {
            #fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

My answer:


You need another location section for WordPress.

location /blog/ {
    try_files $uri $uri/ /blog/index.php
}

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.