相关文章推荐
乐观的甘蔗  ·  Alpine Docker ...·  1 月前    · 
温暖的汤圆  ·  Docker Remote ...·  1 周前    · 
仗义的冲锋衣  ·  VisualStudio2019- ...·  9 月前    · 
害羞的海龟  ·  ORACLE基础知识2 - 知乎·  1 年前    · 
打盹的板栗  ·  Spring data ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have setup two nginx instances as follows:

nginx (https) → docker:[nginx (http) → uwsgi]

The front facing nginx process exposes the https service, which passes down all requests via proxy_pass to the docker nginx process. It also redirects all requests to httphttps.

The problem is that the docker nginx process has the following line in a location block in its default server instance:

server {
    location = / {
        return 301 $scheme://$http_host${request_uri}login/;

With the intention of redirecting / to the login page. This works fine except that the redirection always points to an http://... url. E.g. A request to http://myserver.com/, gets redirected to https://myserver.com/, then it gets passed down to the docker nginx which returns a 301 with the following url: http://myserver.com/login/. I want it to be https://myserver.com/login/ or whatever schema the front-facing server may offer.

This is how I setup the front-facing nginx process:

server {
    listen       443 ssl http2 default_server;
    location / {
        proxy_pass http://localhost:81;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass    $http_upgrade;
        proxy_redirect     https:// http://;
        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-Host $server_name;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   X-Scheme $scheme;
server {
    listen       80 default_server;
    location = / {
        return 301 https://$http_host${request_uri};

Is this kind of redirection even possible?

Also, in case you wonder, I also tried all possible combinations of X-Forwarded-Proto, X-Scheme and proxy_redirect as other answers suggest, namely:

Nginx does redirect, not proxy

how to handle nginx reverse proxy https to http scheme redirect

One trick that I've found is that you can disable absolute redirects (e.g. instead of redirecting to http://localhost/your_url_with_trailing_slash_now it will redirect to /your_url_with_trailing_slash_now).

add the following to any point within the server block (of the nginx instance that does this 301 redirect, in your case the nginx-docker instance):

server {
  absolute_redirect off;

More info can be found here: https://serverfault.com/questions/227742/prevent-port-change-on-redirect-in-nginx

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.