【问题标题】:How does one map many URLs to a single file using nginx?如何使用 nginx 将多个 URL 映射到单个文件?
【发布时间】:2017-01-28 05:03:12
【问题描述】:

我有一个静态文件 index.html。我将如何配置 nginx 以从域上的每个路径提供它?

    URL  | file
    -----------------
    /    | index.html
    /foo | index.html
    /bar | index.html
    /baz | index.html

基本上,我想要一个外卡匹配。

(我意识到这将是一个不寻常的设置。)

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    前段时间我遇到了同样的问题,似乎记得做了以下事情:

    server {
       server_name example.com www.example.com;
       root /var/www/vhosts/example.com;
       location / {
          try_files index.html =404;
       }
    }
    

    如果您不介意返回错误代码(例如,您正在停机进行维护),您还可以执行以下操作:

    server {
       server_name example.com www.example.com;
       root /var/www/vhosts/example.com;
       location / {
          error 503 index.html;
          return 503;
       }
    }
    

    【讨论】:

      【解决方案2】:

      这就是你要找的吗?

      rewrite ^(.*)$ index.html

      【讨论】:

      • 你能提供上下文吗?这应该插入到配置文件的什么位置?
      • 我最终使用了与您的建议类似的内容:rewrite ^.*$ /index.html last;(在server { ... } 块内)。
      猜你喜欢
      • 2012-12-31
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2013-01-20
      • 2012-04-21
      • 1970-01-01
      相关资源
      最近更新 更多