前后端分离项目
通过nginx指向前端dist目录
server {
listen 8080; #监听的端口
server_name localhost; #域名
location / {
root /root/mhsp/dist; #前端dist资源目录
index index.html index.html
}
location ~ (^/api/v|^/cas/) {
proxy_set_header Host $host;
proxy_set_header REMOTE-PORT $remote_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for;
add_header 'Origin' 'http://xxxx'; #怎么访问就怎么配置,ip/域名
add_header 'Access-Control-Allow-Origin' '*'; #跨域
proxy_pass http://xxxx:8081; #后端访问地址
}
......
}
HTTP转HTTPS,websocket支持https配置
在默认的nginx.conf中打开https支持配置,修改配置
#HTTPS server HTPS配置
server {
listen 443 ssl; #监听的端口,https默认443
server_name localhost;
ssl_certificate cert/xx.pem;
ssl_certificate_key cert/xx.key; #证书和key路径
......
location / {
#大部分和上面一致
......
add_header 'Access_Control_Allow-Methods' 'GET,POST,OPTIONS';
add_header 'Access_Control_Allow-Headers' '*';
proxy_pass http://xxxxx:8082; #需要开启https的地址端口
}
# 定义/imserver的接口为websocket接口,开启websocket支持https的配置
location /imserver {
proxy_pass http://xxxxx:8082; #需要支持https的地址
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
重启nginx,进入sbin下
./nginx -t 检查配置是否正确
./nginx -s reload 重启nginx
|