使用缓存服务器时发生重定向错误的解决方法

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