【问题标题】:nginx ignore location partnginx忽略位置部分
【发布时间】:2012-03-27 21:21:03
【问题描述】:

我花了一个小时寻找这个问题的答案,但除非你知道词汇,否则很难搜索。我想要的很简单。我有这样的服务器:

server {
  listen 80;
  server_name test.dev;
  root /srv/www;

  location / {
    index foo.html;
  }

  location /foo {
    index foo.html;
  }

  location /bar {
    index foo.html;
  }
}

我想要的是 /、/foo 和 /bar 都指向 完全相同的文件。换句话说,我希望完全忽略位置部分。只需提供我告诉您提供的根目录中的文件即可。

别名似乎不是答案,不知道它应该服务哪个文件。

【问题讨论】:

    标签: configuration nginx alias


    【解决方案1】:

    尝试将每个符合条件的位置重定向到将提供文件的命名位置。

    server {
        ...
      error_page 418 = @response;
    
      location /foo {
        return 418;
      }
      location /bar {
        return 418;
      }
      location @response {
        index response.html;
      }
    }
    

    【讨论】:

    • 感谢您的回复。虽然我完全相信这会奏效,但当这不是错误时,我对返回错误代码感到非常不舒服。
    • 我知道默认情况下 nginx 将位置解释为文件系统路径的一部分,但必须有一种方法来覆盖这种行为......
    【解决方案2】:

    我可能会使用重写。

    server {
      listen 80;
      server_name test.dev;
      root /srv/www;
    
      location / {
      index foo.html;
      rewrite  ^/foo(.*)$ /$1  last;
      rewrite  ^/bar(.*)$ /$1  last;
      break;
      }
    
    }
    

    类似的东西,我想你可以从这里弄清楚:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2013-07-19
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      相关资源
      最近更新 更多