request.getScheme cann’t get https in nginx proxy with apache
程序中为了不写死url,使用了动态获得的方式:
basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
其中: request.getScheme() return http but not https.
之前单独使用apache(https)+resin的方式正常,现在前面增加了一层nginx,发现问题来了,协议部分(Scheme)无法传过去,后台的resin无法获取到正确的值。
尝试了下面的配置,结果还是一无所获。
proxy_redirect off;
proxy_set_header HTTPS on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Nginx-Scheme $scheme;
proxy_set_header Scheme $scheme;
proxy_set_header X-FORWARDED_PROTO $scheme;
proxy_set_header X-FORWARDED_PROTO “https”;
proxy_set_header X-Forwarded-Scheme “https”;
proxy_set_header X-Forwarded-Proto $scheme;
real-scheme-header X-Forwarded-Proto;
今天下午好好google了一番,所有的帖子几乎翻了个遍,终于有所收获啦,重点是nginx配置完毕后,apache也要配置环境变量哦!
http://www.ruby-forum.com/topic/183450
nginx config:
=============
proxy_set_header X-Nginx-Scheme $scheme;
# nginx variable $scheme will be ‘http’ or ‘https’.
apache config:
==============
SetEnvIf X-Nginx-Scheme “^https$” HTTPS=on
# Apache environment variable HTTPS will be ‘on’ or not defined.
测试代码如下:
<%
out.println(“Protocol: ” + request.getProtocol() + “<br>”);
out.println(“Scheme: ” + request.getScheme() + “<br>”);
out.println(“Server Name: ” + request.getServerName() + “<br>” );
out.println(“Server Port: ” + request.getServerPort() + “<br>”);
%>
页面打印结果为,正常了!
Protocol: HTTP/1.0
Scheme: https
Server Name: 2hei.net
Server Port: 443
本文固定链接: https://www.2hei.net/2010/02/21/request_getscheme_cannt_get_https/ | 2hei.net
最活跃的读者