Search code examples
wordpressnginxcontent-management-system

Configure nginx using reverse_proxy and wordgpress for subdirectory /blog


I wish to configure nginx with a reverse_proxy (proxy_pass) that routes into my app in localhost:3000 and the location /blog it routes to wordpress.

The config file I have is:

server {

    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    #begin ssl_certificates
    .....
    #end ssl_certificates

        server_name example.com;

        root /var/www/example.com; #WP files are in /var/www/example.com/blog
        index index.php;

        location /blog {
                alias /var/www/example.com/blog; #the WP files are in /var/www/example.com/blog also tried /var/www/example.com
                index index.php;

                try_files $uri $uri/ /blog/index.php?$args;
                
                location ~ \.php$ { 
                       include snippets/fastcgi-php.conf;
                       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust the version based on your PHP-FPM version
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       include fastcgi_params;
                }
        }

        location ~ /\.ht {
                deny all;
        }

        location / {
                proxy_pass http://127.0.0.1:3000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_redirect off;
        }
}

server {

        listen 80;

        listen [::]:80 ipv6only=on;

        server_name example.com;

        location / {
                proxy_pass http://localhost:3000;
        }

}

When I go to path https://example.com/blog it shows a 404.

Other configs I tried is moving the WP files to /var/www/example.com, nevertheless I don't think this is the issue because is actually not redirecting to the static files.

I tried using two proxy_passand that actually works:

...

location / {
    proxy_pass http://localhost:3000;
}

location /blog {
    proxy_pass http://localhost:9000;
}
...

If there is not workaround, do you know any other CMS that might work like this?


Solution

  • The problem was that the permits in the folder wasn't the correct ones for the app it was reading. Therefore it couldn't find it.