WebSocket connection to 'wss://ybt.snut.edu.cn/doc/58db7734deb54888884f9b67ed4102b8/c/702/xfwzwkga/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
nginx配置如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name ybt.snut.edu.cn;
#server_name 218.195.109.72
rewrite ^(.*) https://$server_name$1 permanent; #这句是代表 把http的域名请求转成https
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' https://ybt.snut.edu.cn;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass https://ybt.snut.edu.cn; #因为这里还是80端口,所以保持http就可以了
proxy_max_temp_file_size 0;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header Remote_addr $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' https://ybt.snut.edu.cn;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# HTTPS server
#
server {
listen 8180;
#listen 8000 ssl;
listen 443 ssl;
server_name ybt.snut.edu.cn;
#server_name http://218.195.109.72/;
#charset koi8-r;
#access_log logs/host.access.log main;
charset utf-8;
#add_header 'Access-Control-Allow-Origin' '*';
#add_header 'Access-Control-Allow-Methods' '*';
#add_header 'Access-Control-Allow-Headers' '*';
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' *;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
location /web-apps/ {
proxy_pass https://ybt.snut.edu.cn:8000/web-apps/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
#设置最大上传文件大小50M
client_max_body_size 50m;
#设置超时时间5min
keepalive_timeout 300s;
}
location /5.4.2-46/ {
proxy_pass https://ybt.snut.edu.cn:8000/5.4.2-46/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
#设置最大上传文件大小50M
client_max_body_size 50m;
#设置超时时间5min
keepalive_timeout 300s;
}
location /cache/ {
proxy_pass https://ybt.snut.edu.cn:8000/cache/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
#设置最大上传文件大小50M
client_max_body_size 50m;
#设置超时时间5min
keepalive_timeout 300s;
}
location /sdkjs/ {
proxy_pass https://ybt.snut.edu.cn:8000/sdkjs/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
#设置最大上传文件大小50M
client_max_body_size 50m;
#设置超时时间5min
keepalive_timeout 300s;
}
location /doc/ {
proxy_pass https://ybt.snut.edu.cn:8000/doc/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
#设置最大上传文件大小50M
client_max_body_size 50m;
#设置超时时间5min
keepalive_timeout 300s;
}
location /fonts/ {
proxy_pass https://ybt.snut.edu.cn:8000/fonts/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
#设置最大上传文件大小50M
client_max_body_size 50m;
#设置超时时间5min
keepalive_timeout 300s;
}
location ^~/#/ {
root /opt/ybt/front;
index /index.html;
}
location ^~/ybt/ {
proxy_pass http://218.195.109.72:8181;
#设置最大上传文件大小50M
client_max_body_size 200m;
#设置超时时间5min
keepalive_timeout 300s;
}
location ^~/tdc/ {
#此处为application.properties文件中配置的端口号和实际后端服务部署ip地址
proxy_pass http://218.195.109.72:8281;
#设置最大上传文件大小50M
client_max_body_size 200m;
#client_body_timeout 300s;
#client_header_timeout 300s;
#设置超时时间5min
keepalive_timeout 300s;
}
location ^~/api/ {
proxy_pass https://ybt.snut.edu.cn:8000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
}
location /wss/ {
proxy_pass http://ybt.snut.edu.cn:8000/wss;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header X-Client-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' *;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
}
#ssl on;
ssl_certificate /usr/local/nginx/snut.edu.cn.pem;
ssl_certificate_key /usr/local/nginx/snut.edu.cn.key;
#websocket报400错误在ssl模块加入以下代码
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' https://ybt.snut.edu.cn;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
}
#include /usr/local/nginx/conf.d/.conf;
#include /etc/nginx/conf.d/.conf;
}
WebSocket connection to 'wss://ybt.snut.edu.cn/doc/58db7734deb54888884f9b67ed4102b8/c/702/xfwzwkga/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
①添加
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
②设置跨域请求
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' https://ybt.snut.edu.cn;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
websocket能够正常使用,不报错