(13:Permission denied)在连接上游时出现:[nginx]

408

我正在配置Django项目与Nginx和Gunicorn一起使用。

当我在Nginx服务器上访问端口gunicorn mysite.wsgi:application --bind=127.0.0.1:8001时,我的错误日志文件中出现以下错误:

2014/05/30 11:59:42 [crit] 4075#0: *6 connect() to 127.0.0.1:8001 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "localhost:8080"

以下是我的nginx.conf文件的内容;

server {
    listen 8080;
    server_name localhost;
    access_log  /var/log/nginx/example.log;
    error_log /var/log/nginx/example.error.log;

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
    }
}

我的 HTML 页面显示 502 Bad Gateway 错误。

我做错了什么?

17个回答

980

免责声明

在运行此程序之前,请确保您的使用情况没有安全影响。

答案

我遇到了类似的问题,尝试让 Fedora 20、Nginx、Node.js 和 Ghost(博客)一起工作。结果发现我的问题是由于SELinux引起的。

这应该可以解决问题:

setsebool -P httpd_can_network_connect 1

详情

我检查了SELinux日志中的错误:

sudo cat /var/log/audit/audit.log | grep nginx | grep denied

我发现运行以下命令可以解决我的问题:

sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
sudo semodule -i mynginx.pp

选项 #2(可能更安全)

setsebool -P httpd_can_network_relay 1

https://security.stackexchange.com/questions/152358/difference-between-selinux-booleans-httpd-can-network-relay-and-httpd-can-net

参考文献

http://blog.frag-gustav.de/2013/07/21/nginx-selinux-me-mad/
https://wiki.gentoo.org/wiki/SELinux/Tutorials/Where_to_find_SELinux_permission_denial_details
http://wiki.gentoo.org/wiki/SELinux/Tutorials/Managing_network_port_labels


24
谢谢。我需要先安装policycoreutils-python才能获得audit2allow。参考链接:https://www.centos.org/forums/viewtopic.php?t=5012 - gross.jonas
1
请参见此处。在我的情况下,我必须将nginx添加到用户组中,该用户组存储了wwwroot的主目录。 - user113397
2
在 Fedora 23 上安装 policycoreutils-python 并没有提供 audit2allow 命令。经过一些研究,我发现你应该安装 devel 包 yum install policycoreutils-devel。参考:http://danwalsh.livejournal.com/61710.html - Joseph N.
3
这应该在许多Unix操作系统的CherryPy和Nginx文档中提到,因为我按照所有文档的步骤尝试了8个小时才弄清楚! - Marc Maxmeister
3
请注意,如果打开httpd_can_network_connect选项,可能会在您的公共服务器上造成严重的安全风险。请参阅此链接和此链接。 - Kristianne Nerona
显示剩余8条评论

226

我也遇到过这个问题。另一个解决方案是切换 SELinux 布尔值 httpd 网络连接的状态为 on(Nginx 使用 httpd 标签)。

setsebool httpd_can_network_connect on

要使更改持久化,请使用-P标志。

setsebool httpd_can_network_connect on -P

您可以使用以下命令查看httpd可用SELinux布尔值列表:

getsebool -a | grep httpd

1
这个方法有效,谢谢。我从CentOS 6.5升级到6.7,可能在升级过程中默认将该值关闭了,因为在升级之前它一直正常工作。修复很简单。 - Mike Purcell
1
请注意,在公共服务器上启用httpd_can_network_connect可能会带来严重的安全风险。详情请参见此和此链接。 - Kristianne Nerona

29

我已经通过使用当前登录的用户mulagala来运行我的Nginx解决了我的问题。

默认情况下,用户nginxnginx.conf文件的顶部定义,如下所示;

user nginx; # Default Nginx user

nginx 更改为您当前用户的名称 - 在这里是 mulagala

user mulagala; # Custom Nginx user (as username of the current logged in user)

然而,这可能并没有解决实际问题,甚至可能会有偶发的副作用。

要想获得有效的解决方案,请参考Joseph Barbere的解决方案


17

在Centos 7上出现了类似的问题。当我尝试应用Sorin建议的解决方案时,我陷入了循环中。首先我遇到了一个权限被拒绝的 {write} 问题。然后当我解决了这个问题后,我又遇到了一个权限被拒绝的 {connectto} 问题。然后又回到了权限被拒绝的 {write} 问题。

根据@Sid上面的回答,使用getsebool -a | grep httpd检查标志并切换它们,我发现除了关闭httpd_can_network_connect之外,http_anon_write也关闭了,导致写入权限被拒绝和{connectto}被拒绝的权限问题。

type=AVC msg=audit(1501830505.174:799183): avc:  
denied  { write } for  pid=12144 comm="nginx" name="myroject.sock" 
dev="dm-2" ino=134718735 scontext=system_u:system_r:httpd_t:s0 
tcontext=system_u:object_r:default_t:s0 tclass=sock_file

如上所述,使用sudo cat /var/log/audit/audit.log | grep nginx | grep denied获取。

所以我逐个解决它们,逐个切换标志。

setsebool httpd_can_network_connect on -P

按照@sorin和@Joseph上面指定的命令运行

sudo cat /var/log/audit/audit.log | grep nginx | grep denied | 
audit2allow -M mynginx
sudo semodule -i mynginx.pp

基本上,您可以检查在setsebool上设置的权限,并将其与从grep'ing' audit.log nginx中获取的错误相关联,被拒绝的。


16

如果在CentOS上使用Nginx反向代理API网关时出现“502 Bad Gateway”错误,请运行以下命令解决问题。

sudo setsebool -P httpd_can_network_connect 1

8
  1. 检查/etc/nginx/nginx.conf中的用户。
  2. 将所有权更改为用户。
sudo chown -R nginx:nginx /var/lib/nginx

8
  1. 首先看看被拒绝的内容:
sudo cat /var/log/audit/audit.log | grep nginx | grep denied

type=AVC msg=audit(1618940614.934:38415): avc:  denied  { connectto } for
pid=18016 comm="nginx" path="/home/deployer/project/tmp/sockets/puma.sock" scontext=system_u:system_r:httpd_t:s0
tcontext=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
tclass=unix_stream_socket permissive=1

在我的情况下,它对CentOS7有帮助:
sudo setenforce 0

setsebool httpd_can_network_connect on -P
setsebool httpd_can_network_relay on -P

在您能看到启用的内容后:

getsebool -a | grep httpd

httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> on
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> on
httpd_can_network_memcache --> off
httpd_can_network_relay --> on
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> off
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_ipa --> off
httpd_run_preupgrade --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off

谢谢!setenforce 0 解决了我的问题。 - Kamal
@Kamal 好的,很好! - shilovk
请不要在安全环境中这样做,关闭安全策略只是一种权宜之计,会使系统变得不安全。 - Josh Correia

7

我遇到了同样的问题。我尝试了@joebarbere提出的运行以下命令的解决方案,这确实解决了问题,但我担心安全性。

setsebool -P httpd_can_network_relay 1

我随后找到了以下解决方案:
我使用了非标准端口,而 SELinux 阻止了该端口。使用以下命令检查端口是否被允许:

https://serverfault.com/questions/634294/nodejs-nginx-error-13-permission-denied-while-connecting-to-upstream

sudo semanage port --list | grep 4343

并将端口添加到允许列表中

sudo semanage port --add --type http_port_t --proto tcp 4343

我重新启动了nginx服务,然后url就可以访问了。我认为这样更安全。


谢谢!你用的是什么操作系统?我正在使用CentOS Stream 9版本,这解决了我的问题。 - undefined
我正在使用CentOS 7 - undefined

4

连接上游服务器时出现13-权限被拒绝的错误nginx on centos服务器 -

执行以下命令:setsebool -P httpd_can_network_connect 1


2
我也遇到过这个问题。我正在使用带有HHVM的Nginx,以下解决方案修复了我的问题:
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/etc/nginx/fastcgi_temp(/.*)?"

sudo restorecon -R -v /etc/nginx/fastcgi_temp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接