第一步:准备工作。
- 服务器绑定的域名: www.xxxxxx.com
- SSL证书文件:
1_www.xxxxxx.com_bundle.crt; (证书文件名称)
2_www.xxxxxx.com.key; (私钥文件名称)
第二步:下载nginx,解压、安装。
(1)官网地址:http://nginx.org/en/download.html 下载nginx ,如图:
(2)下载得到压缩包 :nginx-1.16.0(我下载的是这个版本),解压后目录如图:
(3)点击nginx.exe,发现一闪而过的窗口,在任务管理器里查看是否有两个进程,如果有,说明运行成功。如图:
第三步:配置nginx代理转发
(1)前提条件:
- 已在 SSL 证书管理控制台 中下载并解压缩 www.domain.com 证书文件包到本地目录。
- 解压缩后,可获得 Nginx 文件夹和 CSR 文件: 文件夹名称:Nginx 文件夹内容:
1_www.domain.com_bundle.crt 证书文件
2_www.domain.com.key 私钥文件
CSR 文件内容:www.domain.com.csr 文件。 - 已在当前服务器中安装配置 Nginx 服务器。
(2)把证书文件(1_www.domain.com_bundle.crt 证书文件 ,2_www.domain.com.key 私钥文件 )放入nginx里的conf文件夹里,如图:
(3)打开 nginx.conf 配置文件,配置代理内容,我这里配置的是监听443端口,转发到Tomcat的8080端口和转发到websocket的4909端口。如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/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 75;
#gzip on;
server {
listen 443 ssl;
server_name www.xxxxxxxx.com;
ssl_certificate 1_www.xxxxxxxx.com_bundle.crt;
ssl_certificate_key 2_www.xxxxxxxx.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location /{
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
}
location /wss{
proxy_pass http://localhost:4909;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
}
#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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
主要修改内容如图:
(4)重启nginx。
运行cmd,进入命令行界面,进入nginx的文件里,运行重启命令(.\nginx -s reload)。
开启命令:.\nginx
重启命令:.\nginx -s reload
判断配置文件是否正确命令:.\nginx -t
第四步:验证是否配置成功
(1)https访问项目:https://www.xxxxxxxxx.com/项目名。
(2)wss访问websocket服务:
wx.connectSocket({
url: 'wss://www.xxxxxxxxxxx.com/wss',
})
第五步:报错解决
(1)nginx报错:bind() to 0.0.0.0:443 failed (10013: An attempt was made to access a socket in a way forbidden by it。
原因:其他进程占用了nginx 的端口
解决:运行 cmd, 输入netstat -aon|findstr “443”,找到 0.0.0.0:443,找到 PID,在任务管理器结束进程。
第六步:HTTP 自动跳转 HTTPS 的安全配置(补充)
server {
listen 80;
server_name www.domain.com; #填写绑定证书的域名
rewrite ^(.*)$ https://$host$1 permanent; #把http的域名请求转成https
}