Bouffe
Bouffe

Reputation: 849

Nginx install wordpress under some URI prefix on existing website

We have a website build with react by a partener. And we want to add a blog on the same domain, in a subdirectory:

I tried many solutions, but I always got a 404 on example.org/blog

server {

    server_name example.org;

    root /var/www/project/app/functions/build;

    access_log /var/log/nginx/example.org_access.log;
    error_log /var/log/nginx/example.org_error.log;

    index index.html index.htm;


    location ^~ /blog/ {
    
        access_log /var/log/nginx/blog-example.org_access.log;
        error_log /var/log/nginx/blog-example.orgm_error.log;

        alias /var/www/example.org/blog;
        index /index.php;

        # Add a trailing slash if missing
        if (!-f $request_filename) {
            rewrite [^/]$ $uri/ permanent;
        }
        try_files $uri $uri/ /index.php?$args;
    }

    location / {
       try_files $uri @prerender;
    }
    
    location @prerender {
        // .. config for react stuff
    }


    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_index index.php;
        # Change this to your fpm socket
        fastcgi_pass  unix:/var/run/php/php7.4-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include fastcgi_params;
    }

}

I tried to create a subdomain with a separate nginx site conf to test if everything was OK, and it was. So it's my nginx site config above which is not good.

Do you have any idea? Thanks you!

Solution

Thanks to Ivan Shatsky, I was able to correct my config to make everything works. My main issue why I always had a 404 was because of my index. I had an extra slash: index /index.php => index index.php

location /blog/ {

    access_log /var/log/nginx/blog-example.org_access.log;
    error_log /var/log/nginx/blog-example.org_error.log;


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

    # Add a trailing slash if missing
    if (!-f $request_filename) {
        rewrite [^/]$ $uri/ permanent;
    }

    try_files $uri $uri/ /blog/index.php?$args;
    

    location ~ \.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_index index.php;
        # Change this to your fpm socket
        fastcgi_pass  unix:/var/run/php/php7.4-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include fastcgi_params;
    }
    
}

Upvotes: 1

Views: 1164

Answers (1)

Ivan Shatsky
Ivan Shatsky

Reputation: 15478

Not sure if this is an answer, but the errors I already see are:

  • If your react app doesn't make use of PHP (and I suppose it doesn't) why do you use location ^~ /blog/ { ... } having a PHP handler below this block? This way, no request for /blog/any/path/file.php ever reaches that PHP handler. Use simple location /blog/ { ... } prefix location or move the PHP handler inside the location ^~ /blog/ { ... } making it nested location (preferred).
  • If your directory where the WP is located is /var/www/example.org/blog and your URI pefix is /blog/ you'd better use root /var/www/example.org; instead of alias /var/www/example.org/blog;. It those strings don't match, add the trailing slash at the end of the alias directive argument: alias /var/www/example.org/blog/;
  • You are using try_files directive incorrectly, the last argument supposed to be a URI, so to redirect all the requests to WP index file, you should use try_files $uri $uri/ /blog/index.php?$args;
  • Your PHP handler uses global /var/www/project/app/functions/build root instead of WordPress one (if you'd make the PHP handler nested location, this one would be gone automatically).

Not sure that's all, but lets start from fixing these errors.

Update

The final working configuration was added by OP as the original question update.

Upvotes: 2

Related Questions