ws訪問正常,現(xiàn)在做小程序,需要 wss,按照文檔中第二種方法,修改 nginx 代理,結(jié)果一直報錯。
nginx 中 conf的配置如下 :
server {
listen 443;
server_name api.***.city;
ssl on;
ssl_certificate /etc/nginx/conf.d/wss/scs166028******_api.***.city_server.crt;
ssl_certificate_key /etc/nginx/conf.d/wss/scs166028******_api.***.city_server.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
location /wss
{
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite /wss/(.*) /$1 break;
proxy_redirect off;
}
}
前端訪問:
uni.connectSocket({
url: "wss://api.***.city/wss", //加不加端口號都無效,域名后加不加 wss 也無效
success: (res) => {
console.log('連接成功了!')
},
fail: (res) => {
console.log('連接失敗信息:' + res.data)
// reconnect();
}
})
報以下錯誤:
WebSocket connection to 'wss://api.***.city/wss' failed: Error during WebSocket handshake: Unexpected response code: 404
WebSocket connection to 'wss://api.***.city:8282/wss' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
WebSocket connection to 'wss://api.***.city:8282/' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
WebSocket connection to 'wss://api.***.city/' failed: Error during WebSocket handshake: Unexpected response code: 200
百度了很多答案,都試過了,無效。
查詢云服務(wù)器,安全組顯示443,8282這些端口均已放行。
改完nginx配置要重啟nginx才能生效,
rewrite /wss/(.*) /$1 break;
proxy_redirect off;
這2個配置手冊沒有,感覺多余。
地址用 wss://api.***.city/wss
是正確的
location /wss/ {
proxy_redirect off;
proxy_pass http://127.0.0.1:8282;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
break;
}
謝謝評論,已經(jīng)完全按照這個改過了,重啟后仍然報錯:
WebSocket connection to 'wss://api.***.city:8282/wss' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
不加端口訪問,報404錯誤:
WebSocket connection to 'wss://api.***.city/wss' failed: Error during WebSocket handshake: Unexpected response code: 404
覺得你有點懵,什么加不加端口的。
wss://就不需要端口了,默認(rèn)走的是443,再轉(zhuǎn)發(fā)到8282。
要么:ws://api..city:8282 (可Ip+端口 或 域名+端口)
要么:wss://api..city:/wss/(只可域名,通過路徑/wss/轉(zhuǎn)發(fā))
此外,結(jié)尾的:/wss 與 /wss/ 好像有區(qū)別的,把后面的/加上試試:wss://api.***.city/wss/
和nginx里面的配置有關(guān)。
我的配置類似:
location /wss/
{
}
那連接的時候就用:wss://api..city/wss/
網(wǎng)上其他教程沒有/結(jié)尾
location /wss
{
}
那連接的時候就用:wss://api..city:/wss
而且這個地址可以直接使用瀏覽器打開的: https://api.***.city/wss/
完整的 nginx 配置如下:
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server{
listen 80;
server_name api.***.city;
root /Apps/***_app/public;
index index.html index.php;
if ($http_origin = ""){
set $http_origin $http_host;
}
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Methods $http_access_control_request_method;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
add_header Access-Control-Max-Age 1728000;
add_header Access-Control-Allow-Origin *;
if ($request_method = 'OPTIONS') {
return 204;
}
location / {
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
root /Apps/***_app/public;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /Apps/***_app/public/index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_split_path_info ^(.+.php)(.*)$;
include fastcgi_params;
}
}
server {
listen 80;
listen 443 ssl;
server_name api.***.city;
root /Apps/***_app/public;
index index.html index.php;
ssl_certificate /etc/nginx/cert/api/scs1660289054299_api.***.city_server.crt;
ssl_certificate_key /etc/nginx/cert/api/scs1660289054299_api.***.city_server.key;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
add_header Access-Control-Allow-Origin '*';
add_header Access-Control-Allow-Methods $http_access_control_request_method;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
if ($request_method = 'OPTIONS') {
return 204;
}
location / {
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
root /Apps/***_app/public;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /Apps/***_app/public/index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_split_path_info ^(.+.php)(.*)$;
include fastcgi_params;
}
}
server {
listen 443;
server_name api.***.city;
ssl on;
ssl_certificate /etc/nginx/cert/api/scs1660289054299_api.***.city_server.crt;
ssl_certificate_key /etc/nginx/cert/api/scs1660289054299_api.***.city_server.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
location /wss/
{
proxy_redirect off;
proxy_pass http://127.0.0.1:8282;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
break;
}
}
}
location /wss/{
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
}
wss://www.xxx.com/wss/
這個完全OK
nginx大于1.10