Why is my Nginx reverse proxy doing a 301 redirect instead of proxying? Why is my Nginx reverse proxy doing a 301 redirect instead of proxying? docker docker

Why is my Nginx reverse proxy doing a 301 redirect instead of proxying?


Although the questioner's config doesn't have this particular issue, a redirect instead of proxying can also be caused by trailing slash issues, as described in the docs:

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:

location /user/ {   proxy_pass http://user.example.com;}location = /user {    proxy_pass http://login.example.com;}


The problem was my Host header in the cloud upstream, I had

proxy_set_header Host $http_host;

But it needed to be

proxy_set_header Host my-domain.com;


here is an example config for nginx as a reverse proxy which works for me,I simplified it and removed unnecessary parts.I hope it helps.

upstream OAUTH {    server remote_oauth;}server {    listen  80;    server_name example.com;    client_header_timeout       300;    location = /servies/oauth {      return 301 /services/oauth/;    }    location /services/oauth/ {        proxy_pass_request_headers on;        proxy_set_header Host $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_pass http://OAUTH/;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_set_header X-Forwarded-Proto $scheme;        proxy_set_header X-ROOT-URI /services/oauth;        proxy_set_header Accept-Encoding "gzip";        proxy_buffering off;        proxy_request_buffering off;        proxy_http_version 1.1;        proxy_intercept_errors on;        proxy_redirect default;        client_max_body_size 4M;    }}

I thinks you missed this part :proxy_pass_request_headers on