Search code examples
wordpressnginxsubdirectorypermalinks

Wordpress & Nginx try_files problem with a website in a subdirectory with permalinks


So, I have a custom website, and a wordpress site located in a different document root, which should work from https://www.example.com/university-guru/study-abroad

so, the WP site's location is /university-guru/study-abroad and is phisically located in /home/www/blog

the part of nginx's config file for this WP blog is:

        location ^~ /university-guru/study-abroad {
                alias /home/www/blog;
                index index.php;

                access_log  /var/log/nginx/blog.log main;
                error_log /var/log/nginx/blog.err.log warn;

                if (!-f $request_filename) {
                        rewrite [^/]$ $uri/ permanent;
                }

                try_files $uri $uri/ /home/www/blog/index.php$is_args$args;

                location ~ \/university-guru\/study-abroad.+\.php$ {
                        fastcgi_split_path_info ^/university-guru/study-abroad(.+\.php)(.*)$;
                        fastcgi_index index.php;
                        fastcgi_pass unix:/var/php-fpm-sockets/php74-blog.socket;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_param PATH_INFO $fastcgi_path_info;
                        include fastcgi_params;
                        fastcgi_intercept_errors        on;
                        fastcgi_ignore_client_abort     off;
                        fastcgi_connect_timeout 60;
                        fastcgi_send_timeout 180;
                        fastcgi_read_timeout 180;
                        fastcgi_buffer_size 128k;
                        fastcgi_buffers 4 256k;
                        fastcgi_busy_buffers_size 256k;
                        fastcgi_temp_file_write_size 256k;
                }

                add_header Content-Security-Policy "";
                break;
        }

The site itself does come up, as well as wp-admin, but the problem is that permalinks with post name and most of the URLs (publishing new posts with this permalink setup...) don't work as it doesn't seem to look for the document in a proper form.

For example, clicking on the Hello World article comes up with a 404 as

https://www.example.com/university-guru/study-abroad/hello-world/

and the error log throws

2021/04/26 21:33:20 [error] 2660705#0: *222405 "/home/www/blog/hello-world/index.php" is not found (2: No such file or directory), client: 116.203.57.178, server: www.example.com, request: "GET /university-guru/study-abroad/hello-world/ HTTP/2.0", host: "www.example.com"

I'd say that the proper place to look for the doceument woudl be /home/www/blog/index.php but it adds the url to it

Any ideas?


Solution

  • This is how I resolved this.

    The initial problem was that WP is generating a wrong query string as it's residing inside an embeded URL and mostly that was the cause of page not found for post-name permalinks, or anything that is not query string based.

    For example, what I needed for "pretty" permalinks, is a fallback to index.php?q=hello-world for the Hello World test article, while it was generating something link index.php?q=/university-guru/study-abroad/hello-world, which was apparently wrong, and resulted with a 404.

    So what I did at the end, is to create a capture group in my basic location directive for the WP embeded site, and have try_files use that capture for location, directory and finally fall back to a query argument

            location ~ /university-guru/study-abroad(.*)$ {
                    alias /home/www/blog/;
                    index index.php;
    
                    access_log  /var/log/nginx/blog.log main;
                    error_log /var/log/nginx/blog.err.log debug;
    
                    try_files  $1 $1/ /university-guru/study-abroad/index.php?q=$1$args;
    
                    location ~* \.php$ {
                            fastcgi_pass unix:/var/php-fpm-sockets/php74-blog.socket;
                            fastcgi_index index.php;
                            fastcgi_split_path_info ^/university-guru/study-abroad(.+\.php)(.*)$;
                            include fastcgi_params;
                            fastcgi_param SCRIPT_FILENAME /home/www/blog$fastcgi_script_name;
                            fastcgi_intercept_errors        on;
                            fastcgi_ignore_client_abort     off;
                            fastcgi_connect_timeout 60;
                            fastcgi_send_timeout 180;
                            fastcgi_read_timeout 180;
                            fastcgi_buffer_size 128k;
                            fastcgi_buffers 4 256k;
                            fastcgi_busy_buffers_size 256k;
                            fastcgi_temp_file_write_size 256k;
                    }
            }
    

    At this point it seems to be doing the job for me