简体   繁体   English

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

[英]Nginx: stat() failed (13: permission denied)

I am using the default config while adding the specific directory with nginx installed on my ubuntu 12.04 machine.我在使用安装在 ubuntu 12.04 机器上的 nginx 添加特定目录时使用默认配置。

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                root /username/test/static;
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
...

...
}

I just want a simple static nginx server to serve files out of that directory.我只想要一个简单的静态 nginx 服务器来从该目录中提供文件。 However, checking the error.log I see但是,检查error.log我看到

2014/09/10 16:55:16 [crit] 10808#0: *2 stat() "/username/test/static/index.html" failed (13: Permission denied), client:, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "domain"
2014/09/10 16:55:16 [error] 10808#0: *2 rewrite or internal redirection cycle while internally redirecting to "/index.html

I've already done chown -R www-data:www-data on /username/test/static , I've set them to chmod 755 .我已经在/username/test/static上完成了chown -R www-data:www-data ,我已经将它们设置为chmod 755 I don't know what else needs to be set.我不知道还需要设置什么。

Nginx operates within the directory, so if you can't cd to that directory from the nginx user then it will fail (as does the stat command in your log). Nginx 在该目录中运行,因此如果您无法从 nginx 用户cd到该目录,那么它将失败(就像您日志中的stat命令一样)。 Make sure the www-user can cd all the way to the /username/test/static .确保www-user可以一直cd/username/test/static You can confirm that the stat will fail or succeed by running您可以通过运行来确认stat将失败还是成功

sudo -u www-data stat /username/test/static

In your case probably the /username directory is the issue here.在您的情况下, /username目录可能是这里的问题。 Usually www-data does not have permissions to cd to other users home directories.通常www-data没有权限cd到其他用户的主目录。

The best solution in that case would be to add www-data to username group:在这种情况下,最好的解决方案是将www-data添加到username组:

gpasswd -a www-data username

and make sure that username group can enter all directories along the path:并确保username组可以进入路径中的所有目录:

chmod g+x /username && chmod g+x /username/test && chmod g+x /username/test/static

For your changes to work, restart nginx为了使您的更改生效,请重新启动 nginx

nginx -s reload

I've just had the same problem on a CentOS 7 box.我刚刚在 CentOS 7 机器上遇到了同样的问题。

Seems I'd hit selinux.似乎我打了 selinux。 Putting selinux into permissive mode ( setenforce permissive ) has worked round the problem for now.将 selinux 置于许可模式( setenforce permissive )目前已经解决了这个问题。 I'll try and get back with a proper fix.我会尝试以适当的方式修复。

Nginx need to have +x access on all directories leading to the site's root directory. Nginx 需要对通向站点根目录的所有目录具有 +x 访问权限。

Ensure you have +x on all of the directories in the path leading to the site's root.确保在通向站点根目录的路径中的所有目录上都有 +x。 For example, if the site root is /home/username/siteroot:例如,如果站点根目录是 /home/username/siteroot:

chmod +x /home/
chmod +x /home/username
chmod +x /home/username/siteroot

On CentOS 7.0 I had this Access Deined problem caused by SELinux and these steps resolved the issue:在 CentOS 7.0 上,我遇到了由 SELinux 引起的Access Deined问题,这些步骤解决了该问题:

yum install -y policycoreutils-devel
grep nginx /var/log/audit/audit.log | audit2allow -M nginx
semodule -i nginx.pp

Update: Just a side-note from what I've learned while using digitalocean's virtual Linux servers, or as they call them Droplets .更新:只是我在使用 digitalocean 的虚拟 Linux 服务器时学到的知识的一个旁注,或者他们称之为Droplets Using SELinux requires a decent amount of RAM.使用 SELinux 需要相当数量的 RAM。 It's most probably like you won't be able to run and manage SELinux on a droplet with less than 2GB of RAM.这很可能就像您将无法在内存不足 2GB 的 Droplet 上运行和管理SELinux。

You may have Security-Enhanced Linux running, so add rule for that.您可能正在运行 Security-Enhanced Linux,因此请为此添加规则。 I had permission 13 errors, even though permissions were set and user existed..我有权限 13 个错误,即使设置了权限并且用户存在..

chcon -Rt httpd_sys_content_t /username/test/static

Symptom:症状:

Could not upload images to WordPress Media Library.无法将图像上传到 WordPress 媒体库。

Cause:原因:

(CentOS) yum update (CentOS) yum update

Error:错误:

2014/10/22 18:08:50 [crit] 23286#0: *5332 open() "/var/lib/nginx/tmp/client_body/0000000003" failed (13: Permission denied), client: 1.2.3.4, server: _, request: "POST /wp-admin/media-new.php HTTP/1.1", host: "example.com", referrer: "http://example/wp-admin/media-new.php"

Solution:解决方案:

chown -R www-data:www-data /var/lib/nginx

By default the static data, when you install the nginx, will be in /var/www/html.默认情况下,当您安装 nginx 时,静态数据将位于 /var/www/html 中。 So you can just copy your static folder into /var/html/ and set the所以你可以将你的静态文件夹复制到 /var/html/ 并设置

root /var/www/<your static folder>

in ngix.conf (or /etc/nginx/sites-available/default)在 ngix.conf (或 /etc/nginx/sites-available/default)

This worked for me on ubuntu but I guess it should not be much different for other distros.这在 ubuntu 上对我有用,但我想对于其他发行版应该没有太大不同。

Hope it helps.希望能帮助到你。

我遇到了这个问题,我解决了它以授予 nginx 用户和组的权限,如下所示:

chown -R nginx:nginx /username/test/static

Change your nginx.conf user property to www-static files owener.将您的nginx.conf user属性更改为www-static files owner。

#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user your_user_name;

# same other config

I finally found my way through.我终于找到了自己的出路。 In short, let's say your username is joe and you hold a website under your personal filesystem /home/joe/path/to/website .简而言之,假设您的用户名是joe并且您在您的个人文件系统/home/joe/path/to/website下拥有一个/home/joe/path/to/website

You literally have to tell the system that nginx is your pal.您实际上必须告诉系统nginx是您的伙伴。
Place nginx in joe group :nginx放入joe组:

sudo gpasswd -a nginx joe

After that if it still doesn't work, check right access of /home/joe directory.之后,如果它仍然不起作用,请检查/home/joe目录的正确访问权限。 That's probably the reason why nginx can't reach the file because even if he is your friend now you have to open him the door to your house :这可能是 nginx 无法访问文件的原因,因为即使他现在是你的朋友,你也必须打开他到你家的门:

sudo chmod g+x /home/joe

That's it.就是这样。 That's literally all you have to do to give nginx access to your local files :)这就是让 nginx 访问本地文件所需要做的一切:)

I don't think there are security concerns with this method because nginx is the high authority and only an admin can change the group.我认为这种方法没有安全问题,因为nginx是高权限,只有管理员可以更改组。 nginx can now read what's in joe directories. nginx现在可以读取joe目录中的内容。 It's only a security breach if the holder of the nginx account is different with the user you open directory access from, but in my case I'm the holder of both parties, that is in a local context.如果nginx帐户的持有人与您打开目录访问的用户不同,这只是一个安全漏洞,但在我的情况下,我是双方的持有人,这是在本地上下文中。

In my case, the folder which served the files was a symbolic link to another folder, made with就我而言,提供文件的文件夹是到另一个文件夹的符号链接,由

ln -sf /origin /var/www/destination

Even though the permissions (user and group) where correct on the destination folder (the symbolic link), I still had the error because Nginx needed to have permissions to the origin folder whole's hierarchy as well.即使权限(用户和组)在目标文件夹(符号链接)上正确,我仍然遇到错误,因为 Nginx 也需要对原始文件夹的整个层次结构具有权限。

I had the same issue, I am using Plesk Onyx 17 with Centos7.我遇到了同样的问题,我正在使用带有 Centos7 的 Plesk Onyx 17。 I could see this error in proxy_error_log under the affected domain's logs.我可以在受影响域的日志下的 proxy_error_log 中看到此错误。 All the dirs/files in /var/www/vhosts/ are owned by respective users (domain owners) and you can see that all of them are in psacln group. /var/www/vhosts/ 中的所有目录/文件都归各自的用户(域所有者)所有,您可以看到它们都在 psacln 组中。 So solution was to add nginx also to this group, so he can see what he needs:所以解决方案是将nginx也添加到这个组中,这样他就可以看到他需要什么:

usermod -aG psacln nginx

And indeed, restart nginx and reload page with Ctrl+F5.实际上,重新启动 nginx 并使用 Ctrl+F5 重新加载页面。

I found a work around: Moved the folder to nginx configuration folder, in my case "/etc/nginx/my-web-app".我找到了一个解决方法:将文件夹移动到 nginx 配置文件夹,在我的例子中是“/etc/nginx/my-web-app”。 And then changed the permissions to root user "sudo chown -R root:root "my-web-app".然后将权限更改为 root 用户“sudo chown -R root:root“my-web-app”。

This is usually the privilege problem... For me, its because i use the /root/** as the nginx root, it need higher privilege.这通常是权限问题...对我来说,因为我使用 /root/** 作为 nginx root,它需要更高的权限。 An easy way is just move the project into a directory created by yourself.一种简单的方法是将项目移动到您自己创建的目录中。

这就是我解决这个问题的方法

sudo chmod o+x /home/ec2-user

To check the default Nginx users:要检查默认的 Nginx 用户:

sudo ps aux| grep nginx

You will get an output like this:你会得到这样的输出:

root       69558  0.0  0.0  66276  1708 ?        Ss   10:14   0:00 
nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   69559  0.0  0.1  66516  5540 ?        S    10:14   0:00 nginx: worker process
www-data   69560  0.0  0.1  66516  6944 ?        S    10:14   0:00 nginx: worker process
root       69794  0.0  0.0   8168   672 pts/1    S+   10:19   0:00 grep --color=auto nginx

Also, check the nginx.conf file using any text editor of your choice: I will be using vim:另外,使用您选择的任何文本编辑器检查 nginx.conf 文件:我将使用 vim:

vim /etc/nginx/nginx.conf

在此处输入图片说明

Solution:解决方案:

  1. Change the www-data user in the nginx.conf file to root, in case you are on root user.如果您是 root 用户,请将 nginx.conf 文件中的 www-data 用户更改为 root。 在此处输入图片说明
  1. The Second solution is to add the user www-data to root group.第二种解决方案是将用户 www-data 添加到根组。

You can also add which user will run the nginx.您还可以添加将运行 nginx 的用户。 In the nginx.conf file, make the following changes:在 nginx.conf 文件中,进行以下更改:

user root;

You can add the above line as the first line in your nginx conf.您可以将上述行添加为 nginx conf 中的第一行。 You can write the name of any user who has the permission to write in that directory.您可以在该目录中写入具有写入权限的任何用户的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 open()“ /root/project/static/*.css”失败(13:权限被拒绝)nginx - open() “/root/project/static/*.css” failed (13: Permission denied) nginx SQLSTATE [HY000]:常规错误:13无法获取“ ./pics”的统计信息(错误代码:13-权限被拒绝) - SQLSTATE[HY000]: General error: 13 Can't get stat of './pics' (Errcode: 13 - Permission denied) nginx:index.js“被禁止(13:权限被拒绝) - nginx: index.js" is forbidden (13: Permission denied) Django / NGINX / Gunicorn / Ubuntu:媒体文件-[Errno 13]权限被拒绝 - Django/NGINX/Gunicorn/Ubuntu: Media files - [Errno 13] Permission denied Nginx 错误:(13:权限被拒绝)在 /var/cache/nginx/proxy_temp/ 上读取上游时 - Nginx error: (13: Permission denied) while reading upstream on /var/cache/nginx/proxy_temp/ 在Ubuntu上拒绝nginx权限 - nginx Permission denied on Ubuntu Nginx:Ubuntu 上的 nginx 权限被拒绝 - Nginx: Permission denied for nginx on Ubuntu PASSENGER / RVM许可被拒绝(13) - PASSENGER/RVM permission denied (13) Linux [Errno 13] 权限被拒绝 - Linux [Errno 13] Permission denied 连接到上游时 Nginx、django、gunicorn、ubuntu 14.04(13:权限被拒绝) - Nginx, django, gunicorn, ubuntu 14.04 (13: Permission denied) while connecting to upstream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM