摘要:在反向代理一個帶有功能的程序源代碼地址時,發(fā)現(xiàn)訪問接口時總是出現(xiàn)響應,的配置參考的是官方文檔相關配置唯一不同的是我們的配置了。
在Nginx反向代理一個帶有WebSocket功能的Spring Web程序(源代碼地址)時,發(fā)現(xiàn)訪問WebSocket接口時總是出現(xiàn)403響應,Nginx的配置參考的是官方文檔:
http { // ssl 相關配置 ... map $http_upgrade $connection_upgrade { default upgrade; "" close; } server { listen 8020; location /ws { proxy_pass http://some-ip:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } } }
唯一不同的是我們的Nginx配置了https。
于是打開Spring日志查看直接訪問和通過Nginx訪問的差別。
直接訪問的日志:
DEBUG ... o.s.web.servlet.DispatcherServlet : DispatcherServlet with name "dispatcherServlet" processing GET request for [/ws/gs-guide-websocket/786/kz0qai5l/websocket] DEBUG ... s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /gs-guide-websocket/786/kz0qai5l/websocket DEBUG ... s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/gs-guide-websocket/786/kz0qai5l/websocket] DEBUG ... o.s.w.s.s.s.WebSocketHandlerMapping : Matching patterns for request [/gs-guide-websocket/786/kz0qai5l/websocket] are [/gs-guide-websocket/**] DEBUG ... o.s.w.s.s.s.WebSocketHandlerMapping : URI Template variables for request [/gs-guide-websocket/786/kz0qai5l/websocket] are {} DEBUG ... o.s.w.s.s.s.WebSocketHandlerMapping : Mapping [/gs-guide-websocket/786/kz0qai5l/websocket] to HandlerExecutionChain with handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@307f6b8c] and 1 interceptor DEBUG ... o.s.web.servlet.DispatcherServlet : Last-Modified value for [/ws/gs-guide-websocket/786/kz0qai5l/websocket] is: -1 DEBUG ... o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from same origin DEBUG ... o.s.w.s.s.t.h.DefaultSockJsService : Processing transport request: GET http://localhost:8080/ws/gs-guide-websocket/786/kz0qai5l/websocket DEBUG ... o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name "dispatcherServlet": assuming HandlerAdapter completed request handling DEBUG ... o.s.web.servlet.DispatcherServlet : Successfully completed request
通過Nginx訪問的日志:
DEBUG ... o.s.web.servlet.DispatcherServlet : DispatcherServlet with name "dispatcherServlet" processing GET request for [/ws/gs-guide-websocket/297/jp1c3ab5/websocket] DEBUG ... s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /gs-guide-websocket/297/jp1c3ab5/websocket DEBUG ... s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/gs-guide-websocket/297/jp1c3ab5/websocket] DEBUG ... o.s.w.s.s.s.WebSocketHandlerMapping : Matching patterns for request [/gs-guide-websocket/297/jp1c3ab5/websocket] are [/gs-guide-websocket/**] DEBUG ... o.s.w.s.s.s.WebSocketHandlerMapping : URI Template variables for request [/gs-guide-websocket/297/jp1c3ab5/websocket] are {} DEBUG ... o.s.w.s.s.s.WebSocketHandlerMapping : Mapping [/gs-guide-websocket/297/jp1c3ab5/websocket] to HandlerExecutionChain with handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@307f6b8c] and 1 interceptor DEBUG ... o.s.web.servlet.DispatcherServlet : Last-Modified value for [/ws/gs-guide-websocket/297/jp1c3ab5/websocket] is: -1 DEBUG ... o.s.w.s.s.t.h.DefaultSockJsService : Processing transport request: GET http://localhost:8080/ws/gs-guide-websocket/297/jp1c3ab5/websocket DEBUG ... o.s.w.s.s.s.OriginHandshakeInterceptor : Handshake request rejected, Origin header value https://some-host.com not allowed DEBUG ... o.s.w.s.s.s.HandshakeInterceptorChain : org.springframework.web.socket.server.support.OriginHandshakeInterceptor@25ce6ad4 returns false from beforeHandshake - precluding handshake DEBUG ... o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name "dispatcherServlet": assuming HandlerAdapter completed request handling DEBUG ... o.s.web.servlet.DispatcherServlet : Successfully completed request
注意到直接訪問的日志里有這么一條:
DEBUG ... o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from same origin
通過Nginx訪問的日志里有這么一條:
DEBUG ... o.s.w.s.s.s.OriginHandshakeInterceptor : Handshake request rejected, Origin header value https://some-host.com not allowed
然后Google查詢相關解決辦法,找到github上的這個issue,所以只需要修改Nginx的配置,添加proxy_set_header Origin "";就行了:
http { // ssl 相關配置 ... map $http_upgrade $connection_upgrade { default upgrade; "" close; } server { listen 8020; location /ws { proxy_pass http://some-ip:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Origin ""; } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/39734.html
摘要:北美時間月日,爆出嚴重安全漏洞,該漏洞由聯(lián)合創(chuàng)始人及首席架構(gòu)師發(fā)現(xiàn)。反復深入研究后,我發(fā)現(xiàn)問題與不處理非響應和反向代理緩存連接有關。問題是,將僅在反向代理中執(zhí)行許多請求的授權(quán)。大多數(shù)負載均衡器在看到升級請求而非響應后不會重用連接。 北美時間11月26日,Kubernetes爆出嚴重安全漏洞,該漏洞由Rancher Labs聯(lián)合創(chuàng)始人及首席架構(gòu)師Darren Shepherd發(fā)現(xiàn)。該漏洞...
摘要:二跨域解決方案原理利用標簽沒有跨域限制的漏洞,網(wǎng)頁可以得到從其他來源動態(tài)產(chǎn)生的數(shù)據(jù)。使用反向代理實現(xiàn)跨域,是最簡單的跨域方式。 前言 前后端數(shù)據(jù)交互經(jīng)常會碰到請求跨域,什么是跨域,以及有哪幾種跨域方式,這是本文要探討的內(nèi)容。 本文完整的源代碼請猛戳github博客,紙上得來終覺淺,建議動手敲敲代碼 一、什么是跨域? 1.什么是同源策略及其限制內(nèi)容? 同源策略是一種約定,它是瀏覽器最核心...
摘要:二跨域解決方案原理利用標簽沒有跨域限制的漏洞,網(wǎng)頁可以得到從其他來源動態(tài)產(chǎn)生的數(shù)據(jù)。使用反向代理實現(xiàn)跨域,是最簡單的跨域方式。 前言 前后端數(shù)據(jù)交互經(jīng)常會碰到請求跨域,什么是跨域,以及有哪幾種跨域方式,這是本文要探討的內(nèi)容。 本文完整的源代碼請猛戳github博客,紙上得來終覺淺,建議動手敲敲代碼 一、什么是跨域? 1.什么是同源策略及其限制內(nèi)容? 同源策略是一種約定,它是瀏覽器最核心...
摘要:反向代理簡單解釋,用戶訪問頁面,由轉(zhuǎn)接,轉(zhuǎn)到服務器端的內(nèi)部開放端口不對外。 剛接觸的一個涉及實時通信的h5項目,前期開發(fā)沒遇到什么大問題,在pc端chrome調(diào)試都一切正常,用手機訪問頁面時,卻出現(xiàn)了一個問題,node啟動服務的命令行界面并沒有打印出用戶訪問頁面的信息,也就是說手機端的頁面沒有連接到websocket服務,且本地計算機和手機是連的是同一個wifi,也就是說網(wǎng)絡環(huán)境相同,...
閱讀 1232·2021-11-23 10:04
閱讀 2474·2021-11-22 15:29
閱讀 3138·2021-11-19 09:40
閱讀 791·2021-09-22 15:26
閱讀 2190·2019-08-29 16:27
閱讀 2563·2019-08-29 16:10
閱讀 1977·2019-08-29 15:43
閱讀 3384·2019-08-29 12:43