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. and contents are in /var/www/

I am using settings below in nginx and my WordPress installation was using url rewrite rules in Apache. (e.g. permalinks like

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, (home page) works fine.

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

server {
    listen   80;

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


    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$
    location ~ \.php$ {
            # 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 any other answers on Server Fault.

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