Welcome! Log In Create A New Profile

Advanced

Getting ‘404 Not Found Nginx’ while trying to visit WordPress post

Posted by zakero 
Getting ‘404 Not Found Nginx’ while trying to visit WordPress post
August 31, 2022 01:11PM
Hello all. Hope you are doing well today. Badly and desperately need help. So I have set up my WordPress website in a subdirectory of my domain (/blog) and is hosted in a VPS with Nginx as reverse proxy. Everything is working fine except while I’m trying to access a post. Right now when I try to access posts I get Nginx’s 404 Not Found page.

I have looked for solutions online and each and every solution says to put

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

in the location block but it’s not working after doing so.

I even tried without /blog above.

What could be the reason for this? Am I doing something wrong? It’s been 2 days that I have been trying solutions and nothing seems to be working.

NOTES:

1. I have removed SSL configurations from the server blocks to keep it simple. 2. And I am using tagDiv’s Newspaper theme if that’s relevant in this situation.
3. .htaccess file only has one item uncommented which is ErrorDocument 404 /blog/index.php?error=404.
4. I can access the posts if I change Permalinks to Plain.

CONFIGS:

Example.com.conf:
=========================================
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;
}

}
}

=========================================

NGINX.conf:

=========================================

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 20M;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

proxy_hide_header X-Powered-By;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

=============================================
Re: Getting ‘404 Not Found Nginx’ while trying to visit WordPress post
September 01, 2022 05:45AM
Hello, I am scratching my head from last few hours but unable to get reliable source to solve below problem.

Actually, I am trying to setup WP Smush (or any webp plugin) with webp support on Nginx + apache reverse proxy stack installed my moss.sh.
But, while setup neither the rewrite rules for apache nor for nginx seems to be working. After going though some sources online, I got to know that actucually nginx is serving all static files and ignoring all redirection setup in htaccess.

WP Smush documentation says,

"If your server is running NGINX as a proxy for Apache, the Apache/Litespeed rules may not work in .htaccess, and you’ll need to add the NGINX rules manually instead.

If your server uses a hybrid setup running both NGINX and Apache (such as Cloudways or Nexcess), your images are most likely to be cached and served via NGINX. As such, in order to use Smush’s Local WebP feature, you will need to make the following changes:

Exclude image extensions from NGINX rules so those files are now served via Apache.
In Smush, go to the Local WebP page and follow the setup wizard’s guide to add Apache rules.
Clear all cache and refresh your page.
After WebP conversion rules have been configured, click Check Status. "

After searching online to "Exclude image extension to disable" I got below config.


location / {
location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|woff|ttf|svg|eot|sh|webp)$ {
root /home/superuser/public_html/notworkingexample.hu;
expires max;
try_files $uri $uri/ @backend;
}

And ask to exclude png, jpeg,jpg,webp extension from config to exclude them. But I am unable to find such entry in my nginx config file.

Below is my Nginx config file:

```server {
listen 80;
listen [::]:80;
server_name www.tricks.nayag.com;

access_log /usr/local/openresty/nginx/logs/tricks.access;
error_log /usr/local/openresty/nginx/logs/tricks.error error;

location /.well-known/acme-challenge/ {
content_by_lua_block {
auto_ssl:challenge_server()
}
}
location / {
return 301 http://tricks.nayag.com$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.tricks.nayag.com;

access_log /usr/local/openresty/nginx/logs/tricks.access;
error_log /usr/local/openresty/nginx/logs/tricks.error error;

ssl_certificate_by_lua_block {
auto_ssl:ssl_certificate()
}
ssl_certificate /etc/ssl/resty-auto-ssl-fallback.crt;
ssl_certificate_key /etc/ssl/resty-auto-ssl-fallback.key;


return 301 $scheme://tricks.nayag.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name tricks.nayag.com;

access_log /usr/local/openresty/nginx/logs/tricks.access;
error_log /usr/local/openresty/nginx/logs/tricks.error error;

location /.well-known/acme-challenge/ {
content_by_lua_block {
auto_ssl:challenge_server()
}
}

location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name tricks.nayag.com;

access_log /usr/local/openresty/nginx/logs/tricks.access;
error_log /usr/local/openresty/nginx/logs/tricks.error error;

ssl_certificate_by_lua_block {
auto_ssl:ssl_certificate()
}
ssl_certificate /etc/ssl/resty-auto-ssl-fallback.crt;
ssl_certificate_key /etc/ssl/resty-auto-ssl-fallback.key;



include server_params.tricks;

location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $original_scheme;
include root_params.tricks;
}
location ~ /\.ht {
deny all;
}
}
```
And other two included config file are:-

server_params.tricks

```# Extra nginx params to be included in this site managed by Moss
# all directives below will be included within 'server' blocks
# common configs that you might want to fine-tune are given


# Core module
# https://nginx.org/en/docs/http/ngx_http_core_module.html
#

client_max_body_size 128m;


# Gzip module
# https://nginx.org/en/docs/http/ngx_http_gzip_module.html
#

gzip on;
gzip_disable "msie6";
gzip_comp_level 5;
gzip_min_length 256;
gzip_types application/javascript application/rss+xml application/x-javascript application/xhtml+xml application/xml text/css text/javascript text/plain text/xml;
```
root_params.tricks
```# Extra nginx params to be included in this site managed by Moss
# all directives below will be included within 'location /' blocks

# rewrite ^/users/(.*)$ /show?user=$1? last;
# rewrite_log off;
```

Any help is appreciated.

PS: In case it matter, I am trying to setup this on my wordpress based website [NAYAG Tricks][1].


[1]: https://tricks.nayag.com
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 178
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready