WordPress blog posts permalinks giving 404 on nginx

I have the following nginx configuration to serve blog on /blog/ URL.

server {
        listen 80;
        server_name example.in www.example.in;
        root /var/www/website;

        index index.html index.htm index.php;

        # Serve blog
        location /blog {
                return 301 /blog/;
        }

        location /blog/ {
                autoindex on;
                alias /var/www/blog/;
                index index.php index.html index.htm;
                try_files $uri $uri/ /index.php$args;

                location ~ .php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_param  SCRIPT_FILENAME    $request_filename;
                        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                }
        }

        # Serve other files
        location / {
                try_files $uri $uri/ =404;
        }

        location ~ .php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /.ht {
                deny all;
        }
}

The homepage is working fine at https://example.in/blog and also the admin panel is working perfectly https://example.in/blog/wp-admin/

When the blog posts permalink is set to plain, the blogs posts are opening fine with the URL

https://example.in/blog/?p=123

But on changing the permalink to another format blog/blog/2021/04/16/sample-post/, It is giving 404

https://example.in/blog/blog/2021/04/16/sample-post/

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You should use the ^~ prefix on the location statement, change the alias statement to root /var/www;, and change the last parameter of the try_files statement.

For example:

location ^~ /blog/ {
    autoindex on;
    root /var/www;
    index index.php;
    try_files $uri $uri/ /blog/index.php?$args;

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param  SCRIPT_FILENAME    $request_filename;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x