Explain in detail the solution of Nginx reverse proxy WebSocket response
When Nginx reverse proxies a Spring Web program with WebSocket function (source code address), it is found that there is always a 403 response when accessing the WebSocket interface. The configuration of Nginx refers to the official documentation:
Http {/ / ssl related configuration. 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;}
The only difference is that our Nginx is configured with https.
So open the Spring log to see the difference between direct access and Nginx access.
Logs accessed directly:
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/websocketDEBUG... 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 interceptorDEBUG... O.s.web.servlet.DispatcherServlet: Last-Modified value for [/ ws/gs-guide-websocket/786/kz0qai5l/websocket] is:-1DEBUG. O.s.web.cors.DefaultCorsProcessor: Skip CORS processing: request is from same originDEBUG... O.s.w.s.s.t.h.DefaultSockJsService: Processing transport request: GET http://localhost:8080/ws/gs-guide-websocket/786/kz0qai5l/websocketDEBUG... O.s.web.servlet.DispatcherServlet: Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handlingDEBUG... O.s.web.servlet.DispatcherServlet: Successfully completed request
Logs accessed through 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/websocketDEBUG... 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 interceptorDEBUG... O.s.web.servlet.DispatcherServlet: Last-Modified value for [/ ws/gs-guide-websocket/297/jp1c3ab5/websocket] is:-1DEBUG. O.s.w.s.s.t.h.DefaultSockJsService: Processing transport request: GET http://localhost:8080/ws/gs-guide-websocket/297/jp1c3ab5/websocketDEBUG... O.s.w.s.s.s.OriginHandshakeInterceptor: Handshake request rejected, Origin header value https://some-host.com not allowedDEBUG... O.s.w.s.s.s.HandshakeInterceptorChain: org.springframework.web.socket.server.support.OriginHandshakeInterceptor@25ce6ad4 returns false from beforeHandshake-precluding handshakeDEBUG... O.s.web.servlet.DispatcherServlet: Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handlingDEBUG... O.s.web.servlet.DispatcherServlet: Successfully completed request
Notice that there is this entry in the log of direct access:
The copy code is as follows:
DEBUG... O.s.web.cors.DefaultCorsProcessor: Skip CORS processing: request is from same origin
There is this entry in the log accessed through Nginx:
The copy code is as follows:
DEBUG... O.s.w.s.s.s.OriginHandshakeInterceptor: Handshake request rejected, Origin header value https://some-host.com not allowed
Then Google queries the relevant solution and finds the issue on github, so you just need to modify the configuration of Nginx and add proxy_set_header Origin ";":
Http {/ / ssl related configuration. 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 ";}
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.