1151 字
3 分钟
Nginx 前后端部署文件配置
文章摘要
DeepSeek R1
Nginx配置单页应用时,History模式需通过try_files和重写处理路由以避免404;Hash模式则直接服务静态文件,无需额外路由配置。两种模式均支持后端API代理。
History 模式配置
server
{
listen 80;
server_name 域名/外网IP;
index index.html;
root /home/wwwroot/eladmin/dist; #dist上传的路径
# 避免访问出现 404 错误
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
## 后端接口配置
location ^~ /api/ {
proxy_pass http://你的ip:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Hash 模式配置
server {
listen 80;
server_name 域名/外网IP;
location / {
root /home/wwwroot/eladmin/dist; #dist上传的路径
index index.html;
}
## 后端接口配置
location ^~ /api/ {
proxy_pass http://你的ip:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
