Website accessible with Nginx root ‘…/html/example.com’ but not with ‘…/html/example.com/blog’

[ad_1]

Hello all. So I have a Nodejs app in the root of my domain ([example.com](https://example.com)) and now I’m trying to set up a blog in a subdirectory ([example.com/blog]). I have a partially working Nginx config and partially because I cannot access the posts of my site (404 Not Found Nginx) and I can only access the homepage and this makes me wonder if somehow the folder structure and the Nginx config has any conflicting statements and I’m doubting it is the ‘root’ part in the config.

Right now in the location block of my blog I have assigned root as ‘/var/www/html/example.com’. This is working as I can see the homepage of my blog. But my wordpress files and installations are in ‘/var/www/html/example.com/blog’. And if I change root to ‘/var/www/html/example.com/blog’, then I see ‘404 Not Found Nginx’ for blog homepage as well.

Why is this happening?

The reason I need to know and fix this is because I get this error log when I try to access my posts:

​

2022/09/02 15:43:43 [error] 86838#86838: *6 “/var/www/html/example.com/blog/advantages-of-social/index.php” is not found (2: No such file or directory), client: 142.52.23.144, server: www.example.com, request: “GET /blog/advantages-of-social/ HTTP/1.1”, host: “www.example.com”, referrer: “https://www.example.com/blog/”

Somehow the index.php is being looked at inside a directory which doesn’t exist.

​

Here’s the full Nginx config:

server {

server_name example.com;
return 301 https://www.example.com$request_uri;

}

server {

server_name www.example.com;

# Web App
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection ‘upgrade’;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $http_cf_connecting_ip;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}

# WordPress Blog
location /blog {

access_log /var/log/nginx/blog_access.log;
error_log /var/log/nginx/blog_error.log;

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

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

# try_files $uri $uri/ /blog/index.php?q=$uri&$args;
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/php8.1-fpm.sock;

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

}
}

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer