【问题标题】:prevent redirecting to https and URI hacks nginx reverse proxy防止重定向到 https 和 URI 黑客 nginx 反向代理
【发布时间】:2017-10-13 02:15:01
【问题描述】:

我已经在我的服务器上使用 docker 内的 nginx 设置了一个反向代理,从那时起所有请求都重定向到 https,尽管我没有将所有位置都设置为 https 重定向。

基本上我想要的是能够使用反向代理同时提供 https 和 http。 此外,我希望能够动态重定向到不同的 URI,例如,我想将 /bla2/foo/bar 的所有路由设置为仅重定向到 /bla2 之后的内容 我试图在这里得到的是,每当访问 example.com/bla2/foo/bar 它应该将其重定向到 example.com/foo/bar 而没有 bla2 部分...

  1. 可以在同一个配置文件上吗?
  2. 什么会导致我的服务器将所有请求重定向到 https

这是我的服务器 nginx.conf

 server {
        listen   443;
        ssl    on;
        ssl_certificate    /etc/ssl/example.com.crt;
        ssl_certificate_key    /etc/ssl/example.com.key;


        listen     80;
        server_name  example.com;

        location /bla/bla {
                proxy_pass http://example.com:3980/foo/bar1;
        }

        **location /bla2/**{
               # proxy_pass http://example.com:3004;
                return 301 http://example.com:3004$request_uri;

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                proxy_set_header X-Forwarded-Proto $scheme;
        }

        location /app3 {
                rewrite ^/app3(.*) /$1 break;
                proxy_pass http://example.com:1236/app3;
        }
}

我希望能够直接获取 http://example.com:3004 的内容,如果我将其放入浏览器而不重定向到 https。 只有当我尝试访问 example.com/bla2 时,我才希望它是必需的 https 而不是 http,并被重定向到不同的路径。

【问题讨论】:

  • 现在有什么问题?还有什么在3004上运行? NodeJS?
  • 我有 2 个不同的问题,一个是无论我是否指向应用程序都将所有 http 重定向到 http,另一个是我上面提到的路由问题。我刚刚编辑了内容,希望现在更清楚。是的,这些应用程序是节点 js 应用程序
  • 如果您直接在浏览器中访问 http://example.com:3004/ 并重定向到 https,那么这可能只是因为您的应用正在发送该重定向。还要确保清除浏览器缓存
  • 我的配置是否符合我的要求?
  • 从外观上看,我没有看到任何问题,它应该做你想做的事

标签: redirect docker nginx reverse-proxy


【解决方案1】:

您需要为此使用正则表达式并捕获组

location ~* /bla2/(.*) {
    return 301 http://example.com:3004$1$is_args$args;
}

另一种方法就是使用重写

rewrite ^/bla2/(.*)$ http://example.com:3004/$1 redirect;

如果你想将它透明地代理到example.com:3004 删除/bla2 那么你应该使用

location /bla2/ {
   proxy_pass http://example.com:3004/;
}

/bla2/http://example.com:3004/ 中的尾部斜杠 / 在这里非常重要

【讨论】:

  • 如果您不介意在不使用重定向的情况下添加第三个选项,那么我可以接受答案,因为最终我选择了一个不使用重定向的选项,我想您知道我的意思
  • @user4860092,立即查看
猜你喜欢
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 2016-04-11
  • 2018-02-22
相关资源
最近更新 更多