我有一个nginx 1.6.2服务器作为一个负责平衡器后端运行的SSL终止。 所有与后端服务器的通信都通过HTTP进行。
发生了什么事情的图表:
/--http---> frontend:80 --\ client --+ +--http---> backend:8000 \--https--> frontend:443 --/ LOAD BALANCER BACKENDS
出于testing目的,我目前只有一个后端。 负载平衡器运行HAProxy 1.5,我有一些控制权。
我在后端nginxconfiguration的server块中有一个非常典型的try_files指令:
server { server_name frontend; ... try_files $uri $uri/ =404; ... }
现在,默认情况下,当我访问一个没有结尾斜杠的目录时,例如https://frontend/somedir ,nginx想要发送一个HTTP 301redirect到一个绝对URL,如http://frontend:8000/somedir/ 。
我可以让nginx使用port_in_redirect off 8000端口号。
但是,我似乎无法更正redirectnginx正在生成的http://scheme的开始。 我能得到nginx的最好的办法是从https://frontend/somedir到http://frontend/somedir/ ,有效地剥离SSL!
负载平衡器正在发送一个X-Forwarded-Proto头文件,但是我没有看到nginx在制作redirect的时候可以参考它。 事实上有一个2012年的答案,说nginx彻底不能做到这一点,解决scheme是用nginxreplace负载平衡器。 恕我直言,这是太重要的事情,以保证这样一个急剧的堆栈变化。
自2012年以来有什么变化吗? 我真的不想在HAProxy级别重写这些redirect:如果我只是总是重写Location: response头的scheme与Web应用程序的HTTPredirect实际有意的HTTPS到HTTPredirect可能会“重新HTTPSed”该计划的要求是与。
编辑:
这是一个最小化的configuration,显示nginx生成绝对Location: URL。 请注意,没有重写。
user nobody nobody; worker_processes auto; worker_rlimit_nofile 4096; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; multi_accept on; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; # TODO: Tune fastcgi_buffers/other buffers # Configure keepalive connections keepalive_timeout 15; keepalive_requests 1000; # Hide server version. server_tokens off; # Do not allow any directory indexes anywhere. # This is the default, but it is here for extra paranoia. autoindex off; # gzip text content. gzip on; gzip_vary on; gzip_disable "msie6"; gzip_comp_level 2; gzip_min_length 1024; gzip_types text/css text/plain text/xml application/json application/javascript; server { listen 8000 default_server; root /usr/share/nginx/html; index index.html index.htm; server_name localhost; location / { try_files $uri $uri/ =404; } } }
如果你使用curl查看头文件 – 注意我在/usr/share/nginx/html下创build了一个目录testdir :
[myuser@dev nginx]$ curl -i http://localhost:8000/testdir HTTP/1.1 301 Moved Permanently Server: nginx Date: Thu, 26 Mar 2015 14:35:49 GMT Content-Type: text/html Content-Length: 178 Location: http://localhost:8000/testdir/ Connection: keep-alive <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>
您需要告诉负载平衡器在请求中重写http https :
proxy_redirect http:// $scheme://;