• 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/*;
    }
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey!

    Seems like the problem only appears if you try to use a custom permalink structure.

    Have you tried disabling all or at least non-critical plugins to check for plugin related issues, or perhaps tried to clone the website on a more standard config using WAMP as an example to, rule out any server config problems?

    Thread Starter zakero

    (@zakero)

    Hey there. Thank you so much for replying. Finally someone replied.

    No I haven’t tried disabling the plugins. But I’ll try now.

    However, just to let you now this is the error in my website’s error log:

    2022/09/02 15:43:43 [error] 86838#86838: *6 "/var/www/html/example.com/blog/advantages-of-social-media-2/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-media-2/ HTTP/1.1", host: "www.example.com", referrer: "https://www.example.com/blog/"

    Does it mean anything to you?

    Thread Starter zakero

    (@zakero)

    @gytisp And no disabling plugins doesn’t fix the issue.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting ‘404 Not Found Nginx’ while trying to visit WordPress post’ is closed to new replies.