You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker部署Nginx:如何将根路径重定向至/site路径

解决Nginx根路径自动跳转至/site的问题

你只需要在现有Nginx配置中添加一个匹配根路径的location /块,实现永久重定向即可,修改后的完整配置如下:

location / {
    # 永久重定向根路径到/site,保留协议、主机和端口
    rewrite ^/$ $scheme://$host:$server_port/site permanent;
}

location /site {
    alias /app/static/angular;
    try_files   $uri $uri/ /index.html =404;
}

location /node {
    proxy_pass http://127.0.0.1:8081;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
}

说明:

  • rewrite ^/$ ... permanent:匹配精确的根路径请求,permanent表示返回301永久重定向,浏览器会记住这个跳转规则,后续访问根路径会直接跳转。
  • $scheme、$host、$server_port是Nginx内置变量,分别获取当前请求的协议(如https)、主机名、端口,确保跳转后的URL和当前访问的协议、端口一致。

修改配置后,重启Docker中的Nginx容器使配置生效:

docker restart <你的Nginx容器名称或ID>

内容的提问来源于stack exchange,提问作者subhra_user

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 12:10:35