我有一个设置,HTTP(S)stream量从HAProxy到nginx。
HAProxy nginx HTTP -----> :80 ----> :9080 HTTPS ----> :443 ----> :9443
我遇到了从https到http的尾随斜杠导致的隐式redirect问题,如下所示:
$ curl -k -I https://www.example.com/subdir HTTP/1.1 301 Moved Permanently Server: nginx/1.2.4 Date: Thu, 04 Oct 2012 12:52:39 GMT Content-Type: text/html Content-Length: 184 Location: http://www.example.com/subdir/
显然,HAProxy是作为SSL解包程序工作的,而nginx只能看到http请求。 我已经尝试在HAProxyconfiguration上设置X-Forwarded-Proto来https,但是它什么都不做。
我的nginx设置如下:
server { listen 127.0.0.1:9443; server_name www.example.com; port_in_redirect off; root /var/www/example; index index.html index.htm; }
和HAProxyconfiguration中的相关部分:
frontend https-in bind *:443 ssl crt /etc/example.pem prefer-server-ciphers default_backend nginxssl backend nginxssl balance roundrobin option forwardfor reqadd X-Forwarded-Proto:\ https server nginxssl1 127.0.0.1:9443
我有同样的问题。 在这种情况下,Nginx正在执行某种autoredirect,但是您可以使用两个位置块来捕获和修改默认行为:
# This location will catch the redirect nginx will do automatically # because we are using an exact match ("=") location = /subdir { return 301 https://yourdomainname/subdir/; } # This location will be used for the other requests location /subdir { }
希望这可以帮助
你的后端nginxstream量应该是SSL(基于端口是9443)? 如果是这样,那么看起来好像你的nginxssl后台的服务器指令缺less一个ssl关键字,所以;
server nginxssl1 127.0.0.1:9443 ssl
这将导致haproxy使用SSL进行到nginx服务器的出站通信。 或者也许你正在故意做这个testing呢?
在任何情况下,在前端和后端也可能值得指定模式HTTP,因为默认模式是TCP。
这不是一个真正的http vs https的问题,而是你希望没有结尾的斜杠,但是如果subdir的名字暗示了一个子目录,那么你的请求中的语法是错误的,nginx会为你修复它,这样你就可以重新发布一个有效的请求这将构成对在该目录中find的相对对象的所有后续请求的正确基础。
你可能已经有了http到http或https到https我想。