HTTPS 是一种加密文本的超链接,所以需要证书,证书可以 使用域名,在腾讯云等服务商申请 https 证书,证书有 收费的和免费的,免费的有使用期限。
利用域名申请证书后,一般会有4中证书文件,
.csr .key .crt .pem , 正常NG用 .key 和 .pem就行,放入nginx 目录 conf/ 目录下的ssl/ 目录里。
nginx.conf 的配置
server {
listen 443 ssl;
ssl_certificate D:/phpstudy_pro/Extensions/Nginx1.15.11/conf/ssl/sdsfbtest.h5sdk.com_bundle.pem;
ssl_certificate_key D:/phpstudy_pro/Extensions/Nginx1.15.11/conf/ssl/sdsfbtest.h5sdk.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
server_name sdsfbtest.h5sdk.com;
location /wss/ {
proxy_pass http://10.8.6.23:9502/; #通过配置端口指向部署websocker的项目
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 X-Forwarded-For $remote_addr;
}
}
默认https 端口 443 , http 端口默认 是80
ssl_certificate :.pem 的路径
ssl_certificate_key : .key 的路径
server_name : 域名
proxy_pass http://10.8.6.23:9502/; 指向 要跳转的地址
---
如果要让默认http跳转到https,可以增加个 80端口的配置
server {
listen 80;
server_name www.my.local;
root "D:/phpstudy_pro/WWW/www.my.local";
location / {
include D:/phpstudy_pro/WWW/www.my.local/nginx.htaccess;
autoindex off;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
rewrite ^(.*)$ https://$host$1 permanent; # 加https跳转
}
配置好后,重启nginx
--------------------------------------------------------
下面是WebSocket访问
使用debug -> Console
复制进去访问
var wsServer = 'wss://sdsfbtest.h5sdk.com/wss/';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
console.log("Connected to WebSocket server.");
};
websocket.onclose = function (evt) {
console.log("Disconnected");
};
websocket.onmessage = function (evt) {
console.log('Retrieved data from server: ' + evt.data);
};
websocket.onerror = function (evt, e) {
console.log('Error occured: ' + evt.data);
};