jetty 常用配置

    xiaoxiao2021-04-19  84

    1.识别 X-Forwarded- style headers: 若要让jetty识别 X-Forwarded- 格式的header,如X-Forwarded-Proto,则需要在jetty.xml文件中 httpConfig设置节点打开配置addCustomizer: 如: <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration"> <Set name="secureScheme">https</Set> <Set name="securePort"><Property name="jetty.secure.port" default="8443" /></Set> ........ <!-- Uncomment to enable handling of X-Forwarded- style headers --> <Call name="addCustomizer"> <Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/></Arg> </Call> </New> 作用:可以识别X-Forwarded- 形式的header,比如X-Forwarded-Proto。场景: nginx 做反向代理访问jetty,外部访问nginx采用https协议,而nginx代理jetty是采用http, nginx 通过 proxy_pass http://bossms 反向代理bossms服务,且设置X-Forwarded-Proto: proxy_set_header X-Forwarded-Proto $scheme。nginx 配置如下: upstream bossms { server 127.0.0.1:8010 weight=1; } server { listen 443 ssl; listen 1002 ssl; #server_name localhost; ssl_certificate ssl/server.crt; ssl_certificate_key ssl/server.key; #ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; #ssl_ciphers HIGH:!aNULL:!MD5; #ssl_prefer_server_ciphers on; location / { #proxy_redirect ~^http://([^:]+)(:\d+)?(.*)$ https://$1$2$3; proxy_pass http://bossms; #proxy_set_header Host $http_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; } } 现在假设jetty要返回一个重定向登陆页面,如果没有开启addCustomizer,则因为jetty无法识别X-Forwarded-Proto,所以jetty 识别出的网络协议是http (因为nginx访问jetty时路径:http://bossms),因此302返回的重定向location是http://bossms/login.html。如果开启了addCustomizer,则jetty识别的协议是https,因此返回的重定向location是https://bossms/login.html。 也可以通过proxy_redirect ~^http://([^:]+)(:\d+)?(.*)$ https://$1$2$3; 方式将被代理服务器返回的重定向的http协议的location重定向为https协议的location。 备注:nginx 会处理被代理服务器返回的url,如果返回的url域名是nginx配置的upstream,则会将该域名替换为访问该nginx时的域名,如:访问nginx 的url是:https://192.168.11.24:1002, jetty重定向返回的locaiton是https://bossms/login.html,因为bossms是nginx中配置的upstream,nginx将locaton替换为:https://192.168.11.24:1002/login.html。 如果nginx中设置proxy_set_header Host $http_host; 则jetty中会识别Host 这个header 的值是访问nginx是的域名和端口号,即此时jetty识别的Host 是 :192.168.11.24:1002, 因此此时返回的重定向的location = http://192.168.11.24:1002/login.html (未开启addCustomizer时) 或者 location = https://192.168.11.24:1002/login.html (开启addCustomizer时)。
    转载请注明原文地址: https://ju.6miu.com/read-675760.html

    最新回复(0)