浏览时Nginx:stat()失败(13:权限被拒绝)

时间:2015-04-21 14:33:45

标签: nginx permissions alias

我想使用我的网络浏览器在我的服务器上列出目录。 目录是/ srv / downloads /

我安装了nginx服务器来做到这一点。这是配置文件:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

和conf.d目录中的.conf文件是:

server {
    listen       80 default_server;
    server_name  example.org;
    root     /srv;

    client_max_body_size 64M;

    location /downloads/ {
    autoindex on;
        alias /srv/downloads/;
    allow 192.168.0.0/24;
    allow 10.212.116.0/23;
    deny all;
    }
}

但是当我尝试访问http://192.168.0.2/downloads/时,我能够列出目录但不能列出所有内容。以下是发生的事情:

2015/04/21 17:08:54 [crit] 12839#0: *1 stat() "/srv/downloads/fix_kernel_doc.patch.done" failed (13: Permission denied), client: 192.168.0.26, server: example.org, request: "GET /downloads/ HTTP/1.1", host: "192.168.0.2"

如nginx.conf所示,nginx服务器由用户" nginx"运行。所以我做了以下事情:

[root@srv]# su -l nginx -s /bin/stat /srv/downloads/fix_kernel_doc.patch.done
Last login: mar. avril 21 17:30:19 CEST 2015 on pts/0
  File: '/srv/downloads/fix_kernel_doc.patch.done'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 85524762    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    is)   Gid: ( 1000/    devs)
Context: system_u:object_r:var_t:s0
Access: 2015-04-21 17:22:48.393259661 +0200
Modify: 2015-04-21 17:22:48.393259661 +0200
Change: 2015-04-21 17:22:48.393259661 +0200
 Birth: -

我的用户nginx位于" devs"组。我还尝试使用用户www-data来确定它,但它也没有用。

请问好吗? 感谢

2 个答案:

答案 0 :(得分:1)

根据Context: system_u:object_r:var_t:s0,我猜你正在使用SELinux。

SELinux可能会阻止访问。要验证这是否是SELinux问题,请尝试运行setenforce 0以暂时禁用SElinux,然后重试加载目录。或者,如果确实发生了拒绝,/var/log/audit/audit.log应该已经记录了拒绝。

如果可行,您将不得不更改目录的上下文以允许nginx读取它,或者尝试运行audit2allow以查看是否有SELinux布尔值您可以更改允许nginx访问。

编辑:由于它是SELinux和单个文件夹,解决此问题的最简单方法应该是运行semanage fcontext -a -t httpd_sys_content_t "/srv(/.*)?",然后运行restorecon -R -v /srv

第一个告诉SELinux / srv目录包含HTTP服务器的只读内容,第二个目录将通过并正确地重新标记所有文件。

最后,使用setenforce 1重新启用SELinux。

来自http://www.serverlab.ca/tutorials/linux/web-servers-linux/configuring-selinux-policies-for-apache-web-servers/https://drupalwatchdog.com/volume-2/issue-2/using-apache-and-selinux-togetherhttp://www.techrepublic.com/blog/linux-and-open-source/practical-selinux-for-the-beginner-contexts-and-labels/

的说明

Configure SELinux access so that Apache can access mounted directories是一个相关的问题。

答案 1 :(得分:0)

我遇到了这个问题,并且正在评论此文档用于文档目的。

示例:

nginx用户:www-data

    location /sub {
            alias /home/dir1/dir2/dir3;
            autoindex on;
    }

为www-data设置r + x作为dir3的组所有者返回权限被拒绝

为www-data设置r + x作为dir2的组所有者返回想要的行为

使用递归选项

设置权限
相关问题