一、证书Https
(1)先获取https证书(nginx用的)
(2)配置nginx(如有模块化配置,那么在模块化配置文件中新增xxxx.conf 文件)
教程:nginx模块化配置
xxxx.conf 文件内容:
server {
listen 80;
server_name 你要代理的域名;
# 强制https跳转
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
# 代理域名
server_name 你要代理的域名;
# 证书配置
ssl_certificate xxxxxx.crt; #证书路径
ssl_certificate_key xxxxxx.key; #证书密钥路径
ssl_session_timeout 5m;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#请按照以下协议配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
location / {
# 转发的本地地址
proxy_pass http://localhost:web项目端口;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
(3) 重启nginx
nginx.exe -s reload
安装win服务重启方式:service.exe restart
二、解决访问跨域问题
例子:前端接口:8001,后端接口8002(前后端都没配置跨域)
(1)修改nginx配置文件(代理给6666端口,注意,6666端口不可以被占用)
server {
# 监听端口
listen 6666;
# 监听地址
server_name localhost;
# 转发的本地地址(前端)
location / {
proxy_pass http://localhost:8001;
}
# 转发的本地地址(后端)
location /api {
proxy_pass http://localhost:8002;
}
}
(2)重启nginx
(3)测试前端和后端地址
(访问地址端口已经变更,项目运行服务端口不变,直接代理给6666端口了)
原来:
前端地址:http://localhost:8001
后端地址:http://localhost:8002/api/getData
更改后:
前端地址:http://localhost:6666
后端地址:http://localhost:6666/api/接口地址