0

I am encountering a weird problem on my NGINX server.

As explained in the Codex I moved wordpress from my root directory to a subdirectory /blog/.

It shows the blog index successfully, but if I want to show anything else, like a specific post or archive page it serves the root index.php

Even non-existent URLs are served as the root index.php

If I remove the root index.php it returns an 404 error.

Maybe its due to my nginx and fastcgi settings, but I really have no clue:

server {
    listen 80;
    listen [::]:80;

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

    client_max_body_size 10M;

    # Make site accessible from http://localhost/
    server_name


            set $no_cache 0;
            if ($request_method = POST){set $no_cache 1;}
            if ($query_string != ""){set $no_cache 1;}
            if ($http_cookie = "PHPSESSID"){set $no_cache 1;}
            if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {set $no_cache 1;}
            if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in"){set $no_cache 1;}



    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_cache  microcache;
            fastcgi_cache_key $scheme$host$request_uri$request_method;
            fastcgi_cache_valid 200 301 302 30s;
            fastcgi_cache_use_stale updating error timeout invalid_header http_500;
            fastcgi_pass_header Set-Cookie;
            fastcgi_no_cache $no_cache;
            fastcgi_cache_bypass $no_cache;
            fastcgi_pass_header Cookie;
            fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }


    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

}

Edit: Updated with whole Server Block

Snowball
  • 181
  • 1
  • 8
  • What are the rewrite rules for your URLs? – Tero Kilkanen Sep 18 '15 at 14:51
  • @TeroKilkanen I have pasted the whole server block. Which rewrite rules do you mean? I have just added a few permanent redirects. Where else could I have redirect? This is the server block from my sites-available folder – Snowball Sep 18 '15 at 15:20
  • Actually I meant the `try_files` line. I guess this is some issue with the WordPress location setting. Did you update WordPress's directory in its settings? – Tero Kilkanen Sep 18 '15 at 15:24

1 Answers1

1

The try_files statement is sending all none-existent files to index.php in the web root. I needed to add a second location specifically for the blog to the configuration:

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

Now it works fine.

(Thanks to Steve E. who pointed this out on StackOverflow)

Snowball
  • 181
  • 1
  • 8