Nginx 支持webSocket 响应403

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

web项目为了解决并发连接数,并发现在数,下载流量问题,我在我们项目引入了nginx。最近项目加入websocket协议,集成推送功能。突然发现,nginx代理不了了,响应403.

网上找了些列子,包括官网http://nginx.org/en/docs/http/websocket.html  给出的配置内容:

location /chat/ {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

然而并没有什么卵用。配置完后错误还是没有解决。最终费了好大力气。找到解决方法:

 # Pass the csrf token (see https://de.wikipedia.org/wiki/Cross-Site-Request-Forgery)
    # Default in Spring Boot and required. Without it nginx suppresses the value
    proxy_pass_header X-XSRF-TOKEN;

    # Set origin to the real instance, otherwise a of Spring security check will fail
    # Same value as defined in proxy_pass
    proxy_set_header Origin "http://testsysten:8080"; 

添加以上两句就ok了。原因(自行翻译):

Nginx needs to pass some additional header values if you want to use Websocket and Spring Security. The following lines need to be added to locationsection in your Nginx config。

 

转载于:https://my.oschina.net/wrs/blog/727313

你可能感兴趣的:(Nginx 支持webSocket 响应403)