admin管理员组

文章数量:1297028

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 and also the admin panel is working perfectly /

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

/?p=123

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

/

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/
Share Improve this question edited Apr 17, 2021 at 1:45 Anuj TBE asked Apr 16, 2021 at 13:56 Anuj TBEAnuj TBE 1135 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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;
    }
}

本文标签: Wordpress blog posts permalinks giving 404 on nginx