NGINX Reverse Proxy - ERR_TOO_MANY_REDIRECTS - how to fix?

Until now, I used a PHP script on the main server B 93.151.75.75, which was behind the NGINX reverse proxy server A 173.176.183.183. On the website2.com domain name. And it worked. Now I have installed a new PHP script on the website2.com domain name and am crashing ((( If I set the HTTP protocol in the script settings, I get mixed content errors. If I turn on the HTTPS protocol, then I get an error - ERR_TOO_MANY_REDIRECTS. I tried placing this script on the website3.com domain name and it worked fine. Of course my main server B 93.151.75.75 has SSL certificates from Cloudflare on the website3.com domain name. How can I make this PHP script work correctly on the website2.com domain? What am I doing wrong in server A 173.176.183.183 reverse proxy settings?

I am attaching below NGINX reverse proxy A 173.176.183.183 config and information from Redirect Checker. I would be very grateful for advice!

View server configuration

nginx.conf (NGINX reverse proxy 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
# 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
access_log /var/log/nginx/website2.com-access.log;
error_log /var/log/nginx/website2.com-error.log;
return 301
}
server {
listen 443 ssl;
ssl_certificate /etc/ssl/
ssl_certificate_key /etc/ssl/
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
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;
}
}
}

Redirect Checker:

302 Found
302 Found
302 Found
302 Found
302 Found
302 Found
302 Found
.........
>>>
> --------------------------------------------
> 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:
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
>>>
> --------------------------------------------
> 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:
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
>>>
> --------------------------------------------
> 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:
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
....................
3

1 Answer

I needed to add this line to the common Nginx reverse proxy options -

proxy_set_header X-Forwarded-Proto $scheme;

and the problem was resolved.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like