【问题标题】:How to set up a vhost to autoindex the files using nginx如何设置虚拟主机以使用 nginx 自动索引文件
【发布时间】:2013-12-22 21:41:39
【问题描述】:

我只是在 nginx.conf 中添加一个虚拟主机,用于自动索引文件,如下所示:

server {
    listen       80;
    server_name res.rickyzhang.me;
    root  /home/wwwroot/default/res;
    location = / {
        autoindex      on;
    }
    access_log  /home/wwwlogs/access_res.log  access;
}

我认为这会起作用,但是当我访问 res.rickyzhang.me 时,它​​总是给我一个 400 响应代码,这意味着请求无效并且服务器无法理解,我很困惑。谁能帮忙我?谢谢。

【问题讨论】:

  • 你为什么不添加一个error_log并在那里寻找线索?
  • @Michael Tabolsky,实际上vhost没有收到请求,响应码400表明请求无效,所以没有错误日志可以看,只有访问日志记录在 access.log 中
  • hmhm,如果你在 /home/wwwlogs/access_res.log 中得到了一些东西,那么显然你确实在“vhost”中得到了请求,对吧?所以error_log应该有一个相对的指示。相反,如果您将它放在另一个文件中,那么您的问题似乎无关紧要,因为您没有将请求发送到正确的服务器部分。
  • @Michael Tabolsky 我也认为 nginx 进程没有将请求发送到正确的虚拟主机,也许我应该尝试找出原因,这真的让我很困惑,但感谢你的想法跨度>
  • @ricky_hust 我认为您应该为此虚拟主机使用单独的访问日志,以便您了解哪个虚拟主机匹配。

标签: linux web-services http nginx


【解决方案1】:

我不确定,但请尝试在其中添加 try_files

location = / {
    autoindex      on;
    try_files $uri $uri/;
}

【讨论】:

  • 没用,同样的错误结果,谢谢你的回答
【解决方案2】:

刚刚在我的一台服务器上尝试过:

# nginx -V
nginx version: nginx/1.4.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)

server {
    server_name  some_hostname;

    location = / {
        root   html;
        autoindex on;
#http://wiki.nginx.org/HttpAutoindexModule
#The request only reaches the ngx_http_autoindex_module when 
#the ngx_http_index_module did not find an index file.
        index  should_not_exist;
    }
}

按预期工作

【讨论】:

    【解决方案3】:

    我不知道自己回答是否合适。

    昨天,我仔细检查了我的nginx.conf,我真的找不到任何问题。400响应代码给了我一些线索,所以我只是将主机名从res更改为dw,其他没有更改,它工作!

    真的让我吃惊,我猜res可能是nginx中的保留字,但是我在google上搜索,没有任何相关的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2012-01-02
      • 1970-01-01
      • 2016-01-03
      • 2018-06-27
      • 2018-05-20
      • 2014-11-12
      相关资源
      最近更新 更多