【问题标题】:Nginx: How to rewrite url for a particular directory onlyNginx:如何仅重写特定目录的 url
【发布时间】:2019-06-12 22:06:08
【问题描述】:

我正在使用 nginx 进行反向代理。我正在使用别名重写目录。在我的别名目录中,我有使用 html5 路由的 Angular 2 构建文件。因此,当用户刷新页面时,它应该只重写该目录中存在的 index.html。

控制面板被别名覆盖”

我已经尝试过“try_files $uri $uri/ /index.html =404;” 但这是指主目录中的 index.html。

我也尝试过尝试提供绝对路径,但仍然无法正常工作。

server {
    listen 80;
    server_name xx.xxx.xx.xxx;
    location / {
        proxy_pass http://localhost:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    location /control-panel {
        alias /var/www/html/soundoff-admin-dev/dist;
        # here rewrite should override with index.html inside "dist" directory
     }
}

【问题讨论】:

  • 我脑海中的try_files $uri $uri/ /index.html =404; 将转到您的/ 位置以获取index.html,然后它会调用您的代理。那么为什么不更改 index.html 路径以使用您的位置
  • @ShawnC。已经试过了。但不工作。在页面刷新时显示 404 错误

标签: nginx nginx-location nginx-reverse-proxy nginx-config


【解决方案1】:

借自https://stackoverflow.com/a/50863128/1264360

试试,

 location /control-panel {
    root /var/www/html/soundoff-admin-dev/dist;
    try_files $uri $uri/ /control-panel/index.html;
 }

【讨论】:

  • 我使用了这种方法,但这不起作用。 css & js 文件被 index.html 覆盖
猜你喜欢
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多