Nginx 配置wss与https

 

因为wss协议和https协议的端口号都是 443,故需要写在一个站点里,代码如下

upstream websocket {
server 127.0.0.1:8383;# 远程websocket服务器地址
}
#upstream web{
# server test.com:8080;# 远程http接口,这里是该https转发到的接口,不需要也可以不转发,直接在下面写入
#}
# 通过下面的反向代理到上面的接口去
server {
listen 443;#默认https和wss协议端口
server_name test.com;

ssl on;
ssl_certificate /证书目录/***.pem;#你的上传到服务器的证书位置
ssl_certificate_key /证书目录/****.key;#你的上传到服务器的证书位置
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
underscores_in_headers on;#开启自定义头信息的下划线
#wss协议转发 小程序里面要访问的链接
location /wss {
access_log /var/log/nginx/come-websocket.log;
proxy_pass http://websocket/; 
# 代理到上面的地址去,最后使用websocket访问的url会是 wss://test.com/wss/websocket
proxy_read_timeout 60s;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
}

#https协议转发 小程序里面要访问的链接
location /{
access_log /var/log/nginx/https-reverse.log;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_pass http://test.com:8080;#代理到原有的http的地址去,这里就可转发到上面写的地址
# 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 *;#跨域访问设置

//以下的也可写在一个新的站点里,然后将上面的proxy_pass写入这个新站点的访问地址就好,
且上面的upsteam web{}就不能注释掉了。

root 项目目录;

client_max_body_size 50M;
fastcgi_connect_timeout 75;

fastcgi_read_timeout 600;

fastcgi_send_timeout 600;
index index.php index.html index.htm index.nginx-debian.html;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string; //laravel框架的url美化
}

location ~ \.php$ {
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
include fastcgi_params;

}
}
}

这样就能实现wss与https共存了,自己亲测有效哦。

发表评论

电子邮件地址不会被公开。 必填项已用*标注