【问题标题】:Nginx server for static content用于静态内容的 Nginx 服务器
【发布时间】:2015-10-11 17:58:55
【问题描述】:

启动 Nginx 服务器以提供静态内容(js 等)所需的最低要求是多少?

我有:

http {
    server {
        listen 80;
        location / {
            root /Users/matt/Dev;
        }
    }
}

events {
    worker_connections  1024;
}

但我明白了:

禁止

You don't have permission to access / on this server.

我已经运行sudo nginx -s reload

【问题讨论】:

    标签: macos nginx


    【解决方案1】:

    通过您的配置,您可以访问单个文件。所以你可以得到example.com/example.js(试试看,你会发现它有效)。

    使用example.com,您想显示禁用的根目录 (/Users/matt/Dev) 的内容。要启用使用autoindex

    location / {
        root /Users/matt/Dev;
        autoindex on;
    }
    

    查看更多信息here

    ngx_http_autoindex_module 模块处理以 斜线字符 ('/') 并生成目录列表。

    【讨论】:

    • 我认为 apache 正在使用该地址但不知道如何检查
    • 你也运行apache吗?
    【解决方案2】:

    默认情况下,当我们以root 运行 Nginx 时,例如sudo nginx ...,Nginx 将由root 启动一个主进程并监听一个或多个给定的http 端口,但出于安全原因,它将由用户nobody 启动多个工作进程。这些工作进程将处理传入的请求。我们可以通过运行检查这一点

    更新:将ps -ef更改为ps aux,因为在macosx/freebsd上ps -ef不显示用户

    $ ps aux | grep nginx
    

    在您的情况下,因为用户 nobody 没有访问 /Users/matt/Dev 的权限,所以我们在访问相关位置时会收到 no-permission-error。

    要修复它,我们可以尝试任何一个

    1. 在 nginx.conf 中定义用户,例如user matt;
    2. 设置/Users/matt/Dev的访问权限。请注意,我们还需要确保用户nobody 具有搜索权限(即x-permission)来访问路径名路径前缀中的所有目录,例如/Users、/Users/matt、/Users/matt/Dev,并拥有/Users/matt/Dev及其子文件的读取权限。

    【讨论】:

    • 这还不够。正如我在回答中所写的那样,它将需要autoindex
    • @uzsolt 我不认为启用autoindex 是一个好的选择,除非我们只是设置一个下载站点,因为它通常是不安全的,并且可能会导致一些私有源或数据库配置泄漏。顺便说一句,如果/Users/matt/Dev 中没有 index.html 文件,请使用指令 index 定义默认索引文件,例如index my-index-file.html;更多细节可以找到nginx.org/en/docs/http/ngx_http_index_module.html#index
    • 是的,另一种选择是创建索引文件。但只有权限(如您的回答中所建议)是不够的。
    猜你喜欢
    • 2011-11-06
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 2020-05-18
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    相关资源
    最近更新 更多