首页 > 解决方案 > NGINX 反向代理 - ERR_TOO_MANY_REDIRECTS - 如何修复?

问题描述

到目前为止,我在主服务器 B 93.151.75.75 上使用了一个 PHP 脚本,它位于 NGINX 反向代理服务器 A 173.176.183.183 后面。关于 website2.com 域名。它奏效了。现在我在 website2.com 域名上安装了一个新的 PHP 脚本并且崩溃了(((如果我在脚本设置中设置了 HTTP 协议,我得到混合内容错误。如果我打开 HTTPS 协议,那么我得到一个错误 - ERR_TOO_MANY_REDIRECTS。我尝试将此脚本放在 website3.com 域名上并且效果很好。当然,我的主服务器 B 93.151.75.75 在 website3.com 域名上具有来自 Cloudflare 的 SSL 证书。我该如何制作这个 PHP 脚本在 website2.com 域上正常工作?我在服务器 A 173.176.183.183 反向代理设置中做错了什么?

我附在 NGINX 反向代理 A 173.176.183.183 配置和来自 Redirect Checker 的信息下方。我将非常感谢您的建议!

查看服务器配置

nginx.conf (NGINX 反向代理 173.176.183.183)

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

client_header_timeout 5s;
client_body_timeout 5s;
send_timeout 10s;
reset_timedout_connection on;

client_header_buffer_size 2k;
client_body_buffer_size 20k;
large_client_header_buffers 4 8k;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
# include /etc/nginx/conf.d/*.conf;


#proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=static_cache:8m inactive=10m max_size=1024M;
#proxy_cache_min_uses 1;


server {
listen 80;
server_name website2.com www.website2.com;
access_log /var/log/nginx/website2.com-access.log;
error_log /var/log/nginx/website2.com-error.log;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl;
ssl_certificate /etc/ssl/www.website2.com.pem;
ssl_certificate_key /etc/ssl/www.website2.com.key;

server_name website2.com;
access_log /var/log/nginx/website2.com-ssl-access.log;
error_log /var/log/nginx/website2.com-ssl-error.log;

client_max_body_size 100m;

location / {
proxy_pass http://93.151.75.75;
proxy_set_header Host $host;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
#proxy_request_buffering off;
#proxy_cache static_cache;
}
}
}

重定向检查器:

https://website2.com/
302 Found
https://website2.com
302 Found
https://website2.com
302 Found
https://website2.com
302 Found
https://website2.com
302 Found
https://website2.com
302 Found
https://website2.com
302 Found
https://website2.com
.........

>>> https://website2.com/

> --------------------------------------------
> 302 Found
> --------------------------------------------

Status: 302 Found
Code: 302
Server: nginx/1.16.1
Date: Fri, 30 Apr 2021 14:05:18 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/7.3.24
Location: https://website2.com
Set-Cookie: PHPSESSID=016eaf866d782aad092458f7ecf65f9c; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Vary: Accept-Encoding




>>> https://website2.com

> --------------------------------------------
> 302 Found
> --------------------------------------------

Status: 302 Found
Code: 302
Server: nginx/1.16.1
Date: Fri, 30 Apr 2021 14:05:18 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/7.3.24
Location: https://website2.com
Set-Cookie: PHPSESSID=f5bb7fb2f39189d662f3357ff61927f3; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Vary: Accept-Encoding




>>> https://website2.com

> --------------------------------------------
> 302 Found
> --------------------------------------------

Status: 302 Found
Code: 302
Server: nginx/1.16.1
Date: Fri, 30 Apr 2021 14:05:18 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/7.3.24
Location: https://website2.com
Set-Cookie: PHPSESSID=c0cb7b8a0e23d2726f3c0f8070192b04; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Vary: Accept-Encoding

....................

标签: httpnginxhttpsreverse-proxy

解决方案


我需要将此行添加到常见的 Nginx 反向代理选项中-

proxy_set_header X-Forwarded-Proto $scheme;

问题就解决了。


推荐阅读