【问题标题】:NGINX: Redirect specific http URL to internal port, everything else being uplifted to https / 443NGINX:将特定的 http URL 重定向到内部端口,其他所有内容都被提升到 https / 443
【发布时间】:2020-09-11 10:44:08
【问题描述】:

遗憾的是,nginx - Disable HTTPS redirection for specific URL 从未得到回答,但由于这个问题已经存在一年多了,我会尝试改写一下我的问题。

有一个 nginx 服务器,可以很好地提升所有传入端口 443 的内容。最近收到一个请求,要求将特定的 http(非 443)url/路径 proxy_passed 到内部服务器端口,该端口上有一些 C 代码侦听。

我目前的想法是:

   server {
     listen  80;

     location /controller {
       proxy_pass       http://127.0.0.1:8068;
     }
 
     if ($ssl_protocol = "") {
       rewrite ^ https://$host$request_uri? permanent;    # Redirect all non-SSL traffic to SSL.
     }
   }

   server {
     listen  443;
     ...etc
   

但是你可以想象,这只有在我注释掉 if() 块时才有效。 nginx 是否有可靠的 if-else 块或类似的东西来完成我需要的东西? (我听说在 nginx 配置中的 if 解析支持是...... iffy)

我在 https 服务器块下有位置重定向,例如 /api,被 proxy_passed 到侦听端口 3000 的节点应用程序,所以我知道它运作良好。

任何指针将不胜感激。

【问题讨论】:

  • 你在一个server 块中有if ($ssl_protocol = ""),它只监听端口80,所以条件总是为真。而不是 if 块,尝试:location / { return 301 https://$host$request_uri; }
  • ok,curl 完美运行:http://example.com/controller 访问端口 8068,我从另一端的软件得到响应。 https://example.com/controller 给了我一个 404 - 这是预期的,我可以捕获它,并且 http 其他任何东西都会重定向到 https 并去它应该去的地方。但是 - 在 Chrome 网络浏览器中进行相同的测试有 http://example.com/controller 将我路由回 https 404 错误。所有其他测试与上述相同。有什么想法吗?
  • 浏览器会这样做有两个原因。要么你没有清除它的缓存,它正在响应你网站的早期版本,要么你的安全网站上有 HSTS 标头,禁止浏览器通过 http 连接。
  • 愚蠢的缓存一直妨碍我。感谢您的帮助!

标签: nginx redirect https


【解决方案1】:

感谢上述 cmets 中的 Richard Smith。答案是(引用):

“你有if ($ssl_protocol = "")在一个只监听80端口的服务器块中,所以条件总是为真。

而不是 if 块,尝试:location / { return 301 https://$host$request_uri; }"

一旦浏览器缓存问题得到解决(即:关闭),它就可以完美运行。

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多