nginx 代理https后,应用redirect https变成http --转
2017-05-05 11:11
507 查看
原文地址:http://blog.sina.com.cn/s/blog_56d8ea900101hlhv.html
情况说明
nginx配置https,tomcat正常http接受nginx转发。
nginx 代理https后,(java代码redirect地址)应用redirect https变成http
情况类似
http://2hei.net/mt/2010/02/request-getscheme-cannt-get-https.html http://yywudi.info/nginx-https-400-bad-request-solution/
原因分析:
经过nginx代理后用的spring mvc的redirect,
其中: request.getScheme() return http but not https.
浏览器调整的地址变成http
解决办法:http://han.guokai.blog.163.com/blog/static/136718271201211631456811/
在代理模式下,Tomcat 如何识别用户的直接请求(URL、IP、https还是http )?
在透明代理下,如果不做任何配置Tomcat 认为所有的请求都是 Nginx 发出来的,这样会导致如下的错误结果:
request.getScheme() //总是 http,而不是实际的http或https
request.isSecure() //总是false(因为总是http)
request.getRemoteAddr() //总是 nginx 请求的 IP,而不是用户的IP
request.getRequestURL() //总是 nginx 请求的URL 而不是用户实际请求的 URL
response.sendRedirect( 相对url ) //总是重定向到 http 上 (因为认为当前是 http 请求)
如果程序中把这些当实际用户请求做处理就有问题了。解决方法很简单,只需要分别配置一下 Nginx 和 Tomcat 就好了,而不用改程序。
配置 Nginx 的转发选项:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
配置Tomcat server.xml 的 Engine 模块下配置一个 Value:
Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/
配置双方的 X-Forwarded-Proto 就是为了正确地识别实际用户发出的协议是 http 还是 https。X-Forwarded-For 是为了获得实际用户的 IP。
这样以上5项测试就都变为正确的结果了,就像用户在直接访问 Tomcat 一样。
后记:
上面https到http解决办法对context引导还是会出问题,
比如输入https://xxxx/signet 会转向到http://xxx/signet/ ,
http情况下:输入http://xxxx/signet会返回一个302到http://xxxx/signet/然后正常访问。
解决办法:
# re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;
http://serverfault.com/questions/145383/proxy-https-requests-to-a-http-backend-with-nginx https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-with-ssl-as-a-reverse-proxy-for-jenkins http://www.cyberciti.biz/faq/linux-unix-nginx-redirect-all-http-to-https/
情况说明
nginx配置https,tomcat正常http接受nginx转发。
nginx 代理https后,(java代码redirect地址)应用redirect https变成http
情况类似
http://2hei.net/mt/2010/02/request-getscheme-cannt-get-https.html http://yywudi.info/nginx-https-400-bad-request-solution/
原因分析:
经过nginx代理后用的spring mvc的redirect,
其中: request.getScheme() return http but not https.
浏览器调整的地址变成http
解决办法:http://han.guokai.blog.163.com/blog/static/136718271201211631456811/
在代理模式下,Tomcat 如何识别用户的直接请求(URL、IP、https还是http )?
在透明代理下,如果不做任何配置Tomcat 认为所有的请求都是 Nginx 发出来的,这样会导致如下的错误结果:
request.getScheme() //总是 http,而不是实际的http或https
request.isSecure() //总是false(因为总是http)
request.getRemoteAddr() //总是 nginx 请求的 IP,而不是用户的IP
request.getRequestURL() //总是 nginx 请求的URL 而不是用户实际请求的 URL
response.sendRedirect( 相对url ) //总是重定向到 http 上 (因为认为当前是 http 请求)
如果程序中把这些当实际用户请求做处理就有问题了。解决方法很简单,只需要分别配置一下 Nginx 和 Tomcat 就好了,而不用改程序。
配置 Nginx 的转发选项:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
配置Tomcat server.xml 的 Engine 模块下配置一个 Value:
Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/
配置双方的 X-Forwarded-Proto 就是为了正确地识别实际用户发出的协议是 http 还是 https。X-Forwarded-For 是为了获得实际用户的 IP。
这样以上5项测试就都变为正确的结果了,就像用户在直接访问 Tomcat 一样。
后记:
上面https到http解决办法对context引导还是会出问题,
比如输入https://xxxx/signet 会转向到http://xxx/signet/ ,
http情况下:输入http://xxxx/signet会返回一个302到http://xxxx/signet/然后正常访问。
解决办法:
# re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;
http://serverfault.com/questions/145383/proxy-https-requests-to-a-http-backend-with-nginx https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-with-ssl-as-a-reverse-proxy-for-jenkins http://www.cyberciti.biz/faq/linux-unix-nginx-redirect-all-http-to-https/
相关文章推荐
- nginx 代理https后,应用redirect https变成http
- nginx 代理https后,spring mvc web应用redirect https变成http
- nginx 代理https后,spring mvc web应用redirect https变成http
- nginx 反向代理http和https配置
- nginx(https)反向代理tomcat(http)的实现
- nginx 代理http转https
- Redirect all HTTP requests to HTTPS with Nginx
- nginx 反向代理web应用将https请求转成http请求时,必须注意事项
- Passenger,nginx and SSL(http redirect to https)
- 解决 nginx https反向代理http协议 302重定向localtion到http问题
- 反向代理使用https协议,后台tomcat使用http,redirect时使用错误协议的解决办法
- nginx 反向代理http/https
- Nginx同时反向代理http和https
- CentOS云主机中Nginx反向代理http、https服务
- nginx反向代理tomcat应用,struts2网站程序redirect时导致请求地址错误的解决方法
- C++实现搜狗浏览器加速代理客户端(HTTP/HTTPS)的核心代码
- 告别LVS:使用keepalived+nginx实现负载均衡代理多个https
- Nginx 反向代理https
- nginx强制使用https访问(http跳转到https)
- urllib2中使用http、https代理