Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx does redirect, not proxy

I want to set up Nginx as a reverse proxy for a https service, because we have a special usecase where we need to "un-https" a connection:

http://nginx_server:8080/myserver ==> https://mysecureservice

But what happens is that the actual https service isn't proxied. Nginx does redirect me to the actual service, so the URL in the browser changes. I want to interact with Nginx as it was the actual service, just without https.

This is what I have:

server {     listen 0.0.0.0:8080 default_server;     location /myserver {         proxy_pass https://myserver/;         proxy_set_header X-Real-IP  $remote_addr;         proxy_set_header X-Forwarded-For $remote_addr;         proxy_set_header Host $host;     } } 
like image 744
mitchkman Avatar asked Jul 01 '14 23:07

mitchkman


People also ask

Can NGINX work as forward proxy?

Usually, Nginx is used to serve and cache static assets or as proxy or load balancer for incoming traffic to application servers. In this repository, it is used as forward proxy.

Why use NGINX reverse proxy?

Security and anonymity – By intercepting requests headed for your backend servers, a reverse proxy server protects their identities and acts as an additional defense against security attacks.

What does proxy_redirect do in NGINX?

The Nginx website provides documentation on configuring Nginx for HTTPS. The proxy_redirect used above will rewrite all HTTP redirects from upstream to the current scheme, ie HTTPS. The browser will then receive the correct scheme directly, avoiding unnecessary round-trips.


1 Answers

You have to use the proxy_redirect to handle the redirection.

 Sets the text that should be changed in the “Location” and “Refresh” header fields of a   proxied server response. Suppose a proxied server returned the header field   “Location:https://myserver/uri/”. The directive  will rewrite this string to “Location: http://nginx_server:8080/uri/”.  

Example:

 proxy_redirect https://myserver/ http://nginx_server:8080/; 

Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

like image 155
Tan Hong Tat Avatar answered Oct 04 '22 17:10

Tan Hong Tat