502 Bad Gateway when redirecting to nginx

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 { ### server port and name ### 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 { ### server port and name ### 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.

+5
source share
1 answer

The problem may be in SELinux. Check if it works with sestatus . Since some kind of redirection works for you, this command may be redundant, but others may require it:

 sudo setsebool -P httpd_can_network_connect 1 

To enable forwaring for specific ports, which may be your problem, run the following command:

 sudo semanage port -a -t http_port_t -p tcp 8088 

Replace 8088 with the appropriate port.

Unable to find semanage . How you install it depends on the distribution, but you can most likely use Google to solve this problem.

+1
source

All Articles