Search code examples
phpwordpressnginxhttp-status-code-404permalinks

Nginx with worpress as sub-directory returning 404 in posts


After adding various configurations into the Wordpress subdirectory, I can access without any issues the WP homepage, but I'm still not able to access the posts, returning 404.

www.test.com/blog - WP homepage, works perfectly fine

www.test.com/blog/test-post-for-blog - WP Post, 404

Here is the config:

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

  server_name www.test.com;

  location ^~ /media {
    alias /var/local/test/static/media;
  }

  location ^~ /icons {
    alias /var/local/test/static/icons;
  }

  location /blog {
    alias /var/www/html;
    index  index.html index.htm index.php;
    try_files $uri $uri/ /blog/index.php?$args /blog/index.php?q=$uri&$args;
    # try_files $uri $uri/ /blog/index.php?q=$uri$args;

    location ~ \.php$ {
      try_files $uri =404;
      include fastcgi_params;
      fastcgi_intercept_errors on;
      fastcgi_index index.php;
      fastcgi_pass blog-test:9000;
      fastcgi_split_path_info ^(/blog)(/.*)$;
      fastcgi_param SCRIPT_FILENAME $request_filename;
      fastcgi_param PATH_INFO $fastcgi_path_info;
    }
  }
}

And the log:

2022/02/09 14:47:25 [error] 32#32: *45 open() "/etc/nginx/html/index.php" failed (2: No such file or directory), client: 10.10.10.10, server: www.test.com, request: "GET /blog/test-post-for-blog/ HTTP/1.1", host: "www.test.com", referrer: "https://www.test.com/blog/"
10.10.10.10 - admin [09/Feb/2022:14:47:25 +0000] "GET /blog/test-post-for-blog/ HTTP/1.1" 404 187 "https://www.test.com/blog/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36"

Checking the logs, it seems to look into the wrong place, which is /etc/nginx/html/index.php, but if I add the alias in the php location block, then the homepage will stop working as well, which gets me a bit confused.

Currently, I'm not quite sure if the problem is the alias in the blog block ( nginx recommends not using alias together with try_files, apparently due to a bug ), or anything else. If it is indeed the alias directive, I'll try to add root, together with some rewrite rules to avoid modifying the file structure.

It really took me more than it should to figure it out, and still can not actually understand what might be the issue here.

UPDATE 1 :

This is extremely odd. With the following blog config, separating the php location from the blog, the WP posts are accesible, but not the homepage and admin, returning 404:

  location /blog {
    alias /var/www/html;
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    alias /var/www/html;
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    fastcgi_pass blog-test:9000;
    fastcgi_split_path_info ^(/blog)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

UPDATE 2 :

Now this works ( the blog and the posts can be accesed ) , but the admin seems to be going into a redirect loop:

  location /blog {
    alias /var/www/html;
    index index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri&$args;
  }

  location ~ \.php$ {
    alias /var/www/html;
    try_files $uri $uri/ /index.php?$args;
    include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    fastcgi_pass blog-test:9000;
    fastcgi_param SCRIPT_FILENAME $request_filename;
  }

UPDATE 3 & FIX :

This apparently works, but the config seems to be really unpleasant and unoptimised. If I try to nest the php location into the blog block, then the php files will download instead of rendering. If I try to use alias instead of root, some pages will not show resulting in 404. In any case, this seems to be functional:

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

  location /blog {
    rewrite ^/blog(.*)$ /$1 break;
    try_files $uri $uri/ /blog/index.php?$args;
  }
          
  location ~ \.php$ {
    rewrite ^/blog(.*)$ $1 break;
    fastcgi_pass blog-test:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_split_path_info ^(/blog)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

Solution

  • UPDATE 3 & FIX :

    This apparently works, but the config seems to be really unpleasant and unoptimised. If I try to nest the php location into the blog block, then the php files will download instead of rendering. If I try to use alias instead of root, some pages will not show resulting in 404. In any case, this seems to be functional:

      root /var/www/html; 
      index index.php;
    
      location /blog {
        rewrite ^/blog(.*)$ /$1 break;
        try_files $uri $uri/ /blog/index.php?$args;
      }
              
      location ~ \.php$ {
        rewrite ^/blog(.*)$ $1 break;
        fastcgi_pass blog-test:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_split_path_info ^(/blog)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param PATH_INFO $fastcgi_path_info;
      }