通过 Ngnix 配置解决 WordPress 固定链接 404 问题

2024-09-29 20:09:35 浏览数 (1)

近期博客迁移到 racknerd vps,迁移后固定链接伪静态访问 404。

排查后原因是 Nginx 配置问题,期间还遇到跳转 https:// 无限死循环的问题,故记录正确完整 Nginx 配置。

代码语言:javascript复制
server {
        listen 80;
        listen [::]:80;
        root /var/www/html; #你的 wordpress 目录
        #server_name  xuanyuan.me; 
        #上面这个注释不能去掉,否则两个命名空间会导致https跳转无限死循环
        index index.php index.html index.htm;
        location / {
                try_files $uri $uri/ =404;
        }
        rewrite ^(.*) https://$host$1 permanent; #跳转 https
}

server {
        listen 443      ssl http2;
        listen [::]:443 ssl http2;
        root /var/www/html;
        server_name  xuanyuan.me;
        index index.php index.html index.htm;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_certificate     /etc/nginx/ssl/xxx.pem;     #你的证书目录
        ssl_certificate_key /etc/nginx/ssl/xxx.key;     #你的证书目录

        add_header Referrer-Policy           "no-referrer"   always;
        add_header X-Content-Type-Options    "nosniff"       always;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location ~ /. {
                deny all;
        }

        location ~* /(?:uploads|files)/.*.php$ {
                deny all;
        }

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }

        location /blog { #注意:此处/blog为wordpress目录
                try_files $uri $uri/ /blog/index.php?$args; #注意:此处/blog为wordpress目录
        }

        location ~ .php$ {
                include fastcgi.conf;
                fastcgi_pass   unix:/run/php/php7.4-fpm.sock; #修改为你的php版本
                fastcgi_index  index.php;
                fastcgi_split_path_info ^(. .php)(/. )$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        }

        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}

0 人点赞