1

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/
0

1 Answer 1

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;
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.