I have a problem with nginx redirection. I am working on nginx 1.4.4, and I have two separate redirects. It should work in two ways: First redirect: address1.com redirects to address2.com → address2.com redirects to addres2.com:1234, where the application is located.
The second redirect comes directly from ddress2.com: - address2.com redirects the address address2.com:1234
Now the problem: - Redirecting from address1.com to address2.com works, but address2.com to address2.com: port is not. It ends with a 502 Bad Gateway error. Configs and
Errors from the log are presented below: Information from error.log:
[error] : *386 connect() failed (111: Connection refused) while connecting to upstream, client: {client ip addr}, server:{server name}, request:
"GET / HTTP / 1.1", upstream: " https://127.0.0.1 : {port}", host: "{server name}"
Nginx uses many .conf files stored in the conf.d directory.
address1.conf (This works):
server { listen {ip_addr}:443; ssl on; server_name address1.com; access_log /var/log/nginx/address1.log; error_log /var/log/nginx/address1-error.log; ssl_certificate /etc/httpd/ssl/servercert.crt; ssl_certificate_key /etc/httpd/ssl/private/serverkey.key; location / { rewrite ^ $scheme://address2.com redirect; }}
address2.com conf file (This is not the case):
server { listen {ip_addr}:443; ssl on; server_name address2.com; access_log /var/log/nginx/address2.log; error_log /var/log/nginx/address2-error.log; ssl_certificate /etc/httpd/ssl/servercert.crt; ssl_certificate_key /etc/httpd/ssl/private/serverkey.key; proxy_read_timeout 180; location / { proxy_pass https://127.0.0.1:{port}; proxy_redirect off; 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-Ssl on; proxy_set_header X-Forwarded-Protocol $scheme; proxy_set_header X-Forwarded-HTTPS on; }}
It's funny that I have another application working on the addr3.com → addr3.com:port scheme, and the redirect works just fine. Only
the difference between address2.conf and address3.conf is the port on which the applications are running. Each address uses https, port 443 is open on the firewall.
I hope that my description will be detailed enough, if not just let me know. I struggled with this problem for several days and did not find any tips or solutions suitable for me.
I would appreciate any help.