【问题标题】:Nginx - serving assets from user directoriesNginx - 从用户目录提供资产
【发布时间】:2015-06-23 05:15:37
【问题描述】:

我有 2 个问题。

我不想为 html 文件要求 .html 文件扩展名

/index => /index.html

我想从用户目录提供服务

/~username serves from /home/username/www/

我之前使用try_files来实现(1),我是用户nginx UserDir的建议:

location ~ /^/~(.+?)(/.*)?$ {
   alias /home/$1/www$2;
   index index.html index.htm;
   autoindex on;
}

以上内容适用于用户目录,但仍需要使用 .html ext。 我知道有一个已知的错误会阻止 alias 和 try_files 一起工作。

想法?抱歉,如果之前已经回答过这个问题,但找不到可行的解决方案。

【问题讨论】:

    标签: nginx


    【解决方案1】:

    您始终可以将alias 替换为root

    location ~ /^/~([^/]+)(/.*)?$ {
      root /home/$1/www;
      autoindex on;
      try_files $2 $2/ $2.html;
    }
    

    PS:将index 移动到服务器范围而不是位置

    【讨论】:

    • 谢谢。那行得通。再次使用 root 感觉很脏,但直到别名和 try_files 错误被修复,猜测这是唯一的方法。再次感谢:)
    【解决方案2】:

    它有点老了,但是由于我最近遇到了同样的问题,所以这是我的答案。感谢http://marc.info/?l=nginx&m=124533515814122&w=2,我发现更好的答案是:

    location ~ /^/~(.+?)(/.*)?$ {
      alias /home/$1/www$2;
      index index.html index.htm;
      autoindex on;
      try_files "" .html / =404; 
    }
    

    【讨论】:

      【解决方案3】:

      您可以将.html 扩展添加到位置正则表达式和别名:

      location ~ /^/~(.+?)(/.*)?.html$ {
          alias /home/$1/www$2.html
      

      请注意,在此配置中,只能提供 html 文件。您可以添加其他位置以支持其他文件扩展名。

      【讨论】:

      • 为了更加安全,请在位置说明符的正则表达式中转义 html 左侧的点。即指定位置为/^/~(.+?)(/.*)?\.html$
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 2017-03-28
      • 2016-07-08
      • 2011-11-25
      • 2014-04-21
      • 2016-09-29
      相关资源
      最近更新 更多