Nginx

Nginx ERR_TOO_MANY_REDIRECTS 通過調整 WordPress 永久連結 |301 錯誤 Ubuntu 18.04

  • December 10, 2020
  • 作業系統:Ubuntu 18.04.5 LTS(擁有我自己的 VPS)
  • 網路伺服器:Nginx/1.18.0
  • MySQL: mysql Ver 8.0.22 for Linux on x86_64 (MySQL Community Server - GPL)
  • WordPress:5.6

當我將 WordPress 中的永久連結選項(ScreenShot: /wp-admin/options-permalink.php)從預設值(?p=123)更改為*/%postname%/(頁面上的最後一個選項)時,我收到以下錯誤:“ERR_TOO_MANY_REDIRECTS”,狀態碼為301* ,僅適用於首頁 (直接使用 example.com),所有其他頁面都執行良好,即使是新的永久連結選項也可以正常工作,因此預設部落格可以通過以下方式訪問:*/hello-world/*直接。

知道為什麼嗎?這是我的伺服器完成後要做的最後一件事,在這個小錯誤上花費的時間比其他所有事情都多,哈哈。注意:我已經禁用了所有外掛,除了用於清除伺服器端記憶體的 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;
}

如果我需要展示更多的東西,請告訴我,我會展示更多。

Alhamdulillah,我找到了答案。這完全是由於試圖成為完美主義的穆斯林,我在裡面的 URL 中有大寫字母:/wp-admin/options-general.php。所有對上帝的讚美(Alhamdulillah),通過將所有字母變為小寫來解決。憑藉上帝的旨意(真主),它將幫助您的兄弟和非兄弟。

引用自:https://serverfault.com/questions/1045711