Nginx  前后端部署文件配置
文章摘要
Nginx配置单页应用时,History模式需通过try_files和重写处理路由以避免404;Hash模式则直接服务静态文件,无需额外路由配置。两种模式均支持后端API代理。
# History 模式配置 ```nginx 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 模式配置 ```nginx 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; } } ```
作者头像
admin
分享技术与生活
打赏作者

评论

暂无评论,快来抢沙发吧~