Question

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

Hello. I 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. .htaccess file only has one item uncommented which is ErrorDocument 404 /blog/index.php?error=404.

  3. 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/*;
}

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 16, 2023

Hi there,

As you mentioned that the posts are accessible when permalinks are set to plain, the problem is likely with the try_files directive in your Nginx configuration.

You can try updating your location /blog block in your example.com.conf file with the following:

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;

        # try_files directive
        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;
        }
}

Also, I could suggest removing the following lines since they might be causing some issues as well:

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

Once you have made the changes, test the Nginx configuration syntax:

nginx -t

And if you get Syntax OK restart Nginx:

sudo service nginx restart

After restarting Nginx, check if the issue with the WordPress posts is resolved. If it persists, try setting the WordPress permalinks to “Post name” and save the settings and try again.

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel