通过调整 WordPress 永久链接解决 Nginx ERR_TOO_MANY_REDIRECTS | 301 错误 Ubuntu 18.04

通过调整 WordPress 永久链接解决 Nginx ERR_TOO_MANY_REDIRECTS | 301 错误 Ubuntu 18.04
  • 操作系统:Ubuntu 18.04.5 LTS(有我自己的 VPS)
  • Web服务器:Nginx/1.18.0
  • MySQL:适用于 x86_64 上 Linux 的 mysql Ver 8.0.22(MySQL 社区服务器 - GPL)
  • WordPress:5.6

当我改变固定链接WordPress 中的选项(截屏:/wp-admin/options-permalink.php)从默认(?p=123) 到/%帖子名称%/(页面上的最后一个选项)我收到以下错误:“错误太多”带有状态代码301 仅适用于主页 (因此直接使用 example.com),所有其他页面都运行正常,甚至新的永久链接选项也正常工作,因此可以通过以下方式访问默认博客:/你好世界/直接地。

知道为什么吗?这是最后要做的事情,我的服务器已经完工了,花在这个小错误上的时间比其他任何事情都多,哈哈。注意:我已禁用所有插件,除了 nginx 缓存,我使用它来清除服务器端缓存。我还以隐身模式访问网站。

我遵循了本教程:SpinUp WP(我做了包括 CH6 在内的所有事情)。以下是我的 NGINX 设置:

NGINX 配置文件(出于隐私原因,我已将用户名更改为 omar):

user omar;
worker_processes 6;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 1024;
        multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        keepalive_timeout 15;
        types_hash_max_size 2048;
        server_tokens off;
        client_max_body_size 64m;

        # 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;
        add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;


        ##
        # 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 5;
        # 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;

        ##
        # Cache Settings
        ##
        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        add_header Fastcgi-Cache $upstream_cache_status;


        ##
        # Security
        ##
        add_header Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval';" always;
        add_header X-Xss-Protection "1; mode=block" always;
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "origin-when-cross-origin" always;



        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        server {
            listen 80 default_server;
            listen [::]:80 default_server;
            server_name _;
            return 444;
        }
}

example.com 的配置文件(出于隐私原因,我已将用户名更改为 omar,将域名更改为 example.com):

fastcgi_cache_path /home/omar/example.com/cache levels=1:2 keys_zone=example.com:100m inactive=60m;

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

access_log /home/omar/example.com/logs/access.log;
error_log /home/omar/example.com/logs/error.log;

root /home/omar/example.com/public/;
index index.php;

set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}

if ($request_uri ~* "/(cart|checkout|my-account)/*$")
{
set $skip_cache 1;
}

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

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache example.com;
fastcgi_cache_valid 60m;
}
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name www.example.com;;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

return 301 https://example.com$request_uri;
}

server {
listen 80;
listen [::]:80;

server_name example.com www.example.com;;

return 301 https://example.com$request_uri;
}

如果我需要展示更多的东西,只要告诉我,我会展示更多。

答案1

感谢真主,我找到了答案。这一切都是因为我想成为完美主义的穆斯林,我在 URL 中使用了大写字母:/wp-admin/options-general.php。感谢真主(Alhamdulillah),通过将所有字母都变成小写,解决了这个问题。按照真主的意愿(如果真主愿意的话),它将帮助你们,兄弟和非兄弟。

相关内容