使用快取伺服器時發生重新導向錯誤的解決方法

4 min

language: ja bn en es hi pt ru zh-cn zh-tw

error.png

您好,我是無能。
※照片無關。是其他錯誤時的圖片。

當我將其作為快取伺服器運行時,不知為何會不時出現重新導向錯誤,而且在清除快取之前都會一直顯示,這讓我非常困擾,但現在這個問題已經解決了。

有問題的語法。※部分已修改。

{
    listen 443 ssl;
    server_name xxx.xxx;

    location / {
        proxy_pass /xxxxxx/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        add_header Content-Security-Policy upgrade-insecure-requests;

       ssl_certificate /xxx/xxx.pem; 
       ssl_certificate_key /xxx/xxx.pem; 

		set $do_not_cache 0;
		if ($http_cookie ~ ^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$) {
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-admin/.*"){
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-content/.*"){
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-includes/.*"){
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-cron.php.*"){
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-login.php.*"){
			set $do_not_cache 1;
		}
                if ($uri ~* "\.(jpg|jpeg|png|webp|gif|mp4|css|js|ico|woff2)$")  {
    set $do_not_cache 0;
}
                

                proxy_no_cache          $do_not_cache;
                proxy_cache_bypass      $do_not_cache;

                proxy_cache             zone;
                proxy_cache_key         $scheme$proxy_host$uri$is_args$args;
                proxy_cache_valid       200 201 6h;
                proxy_cache_valid       302 3h;
                proxy_cache_valid       301 1d;
                proxy_cache_valid       404 5m;
                proxy_cache_lock        on;
    }

}

在多次重新建構時,我發現SSL認證經常會導致重新導向迴圈,因此我終於意識到語法上可能在SSL認證金鑰本身中包含了反向代理,當我將.pem檔案移到後面時,問題就解決了。以下是正確的語法。

另外,我也覺得在 `add_header Content-Security-Policy upgrade-insecure-requests;` 之後立即放置金鑰可能不太好。

{
    listen 443 ssl;
    server_name xxx.xxx;

    location / {
        proxy_pass /xxxxxx/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        add_header Content-Security-Policy upgrade-insecure-requests;

		set $do_not_cache 0;
		if ($http_cookie ~ ^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$) {
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-admin/.*"){
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-content/.*"){
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-includes/.*"){
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-cron.php.*"){
			set $do_not_cache 1;
		}
		if ($request_uri ~ "^/wp-login.php.*"){
			set $do_not_cache 1;
		}
                if ($uri ~* "\.(jpg|jpeg|png|webp|gif|mp4|css|js|ico|woff2)$")  {
    set $do_not_cache 0;
}
                

                proxy_no_cache          $do_not_cache;
                proxy_cache_bypass      $do_not_cache;

                proxy_cache             zone;
                proxy_cache_key         $scheme$proxy_host$uri$is_args$args;
                proxy_cache_valid       200 201 6h;
                proxy_cache_valid       302 3h;
                proxy_cache_valid       301 1d;
                proxy_cache_valid       404 5m;
                proxy_cache_lock        on;
    }
       ssl_certificate /xxx/xxx.pem; 
       ssl_certificate_key /xxx/xxx.pem; 
}

這樣就解決了。

那麼。

※尚未解決。已新增至②。

使用快取伺服器時發生重新導向錯誤的解決方法②

Related Posts