编辑/etc/nginx/sites-enabled/default文件
server {
listen 80;
server_name www.abc.com # 域名信息
index index.html;
# vue项目地址,直接放在根路径
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
root /home/ubuntu/abc-web/dist; # vue项目的dist文件路径
try_files $uri $uri/ /index.html;
index index.html;
}
# springboot项目访问前缀,做服务器映射,同时要在springboot里面配置前缀,否则请求处理会出错
location /abc-api {
#是监听的端口默认访问的地址,这里如果没有做tomcat的转发则会进入nginx的html目录下的index.html
root html;
#这里是编写监听到的请求所转发的端口号,即tomcat端口
proxy_pass http://localhost:4008;
#Proxy Settings;
#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;
#设置nginx 的默认显示页
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
listen 443;
ssl on;
#证书,填写证书保存地址
ssl_certificate /etc/nginx/cert.pem;
#私钥,填写密钥保存地址
ssl_certificate_key /etc/nginx/key.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
}
springboot配置前缀(yml文件),与nginx代理的相对路径相同,否则拦截器会错误拦截
server:
servlet:
context-path: /abc-api
port: 4008
配置完成后,重启服务
service nginx force-reload # 重启nginx
systemctl status nginx.service # 查看服务