我正在使用 Nginx 和 Gunicorn 配置 Django 项目。
当我在 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 连接到上游时失败(13:权限被拒绝),客户端:127.0.0.1,服务器:localhost,请求:“GET / HTTP/1.1”,上游:“http://127.0.0.1:8001/”,主机:“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
。
我在做什么错?
免责声明
在运行此之前,请确保您的用例没有安全隐患。
回答
我在让 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
参考
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
我也遇到过这个问题。另一种解决方案是将 httpd 网络连接的 SELinux 布尔值切换为 on
(Nginx 使用 httpd 标签)。
setsebool httpd_can_network_connect on
要使更改持续使用,请使用 -P 标志。
setsebool httpd_can_network_connect on -P
您可以使用 httpd 查看所有可用 SELinux 布尔值的列表
getsebool -a | grep httpd
我已经通过以我当前登录的用户 mulagala 身份运行我的 Nginx 解决了我的问题。
默认情况下,用户为 nginx 定义在 nginx.conf
文件的最顶部,如下所示;
user nginx; # Default Nginx user
将 nginx 更改为您当前用户的名称 - 这里是 mulagala。
user mulagala; # Custom Nginx user (as username of the current logged in user)
但是,这可能无法解决实际问题,并且实际上可能会产生偶然的副作用。
如需有效的解决方案,请参阅Joseph Barbere's solution。
在 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 如上所述被拒绝。
所以我一次解决一个,一次切换一个标志。
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 上设置的权限并将其与从 grepp'ing'audit.log nginx 获得的错误相关联,拒绝
如果 centos api url for api gateway proxy pass on nginx 抛出“502 Bad Gateway”错误,请运行以下命令解决问题
sudo setsebool -P httpd_can_network_connect 1
检查 /etc/nginx/nginx.conf 中的用户 将所有权更改为用户。
sudo chown -R nginx:nginx /var/lib/nginx
现在看看魔术。
首先查看被拒绝的内容:
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
13-permission-denied-while-connecting-to-upstreamnginx 在 centos 服务器上 -
setsebool -P httpd_can_network_connect 1
我也遇到过这个问题。我将 Nginx 与 HHVM 一起使用,以下解决方案解决了我的问题:
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/etc/nginx/fastcgi_temp(/.*)?"
sudo restorecon -R -v /etc/nginx/fastcgi_temp
另一个原因可能是;您正在使用代理通过 nginx 访问您的应用程序,但您没有使用 gunicorn 为代理添加 gunicorn.sock
文件。
您需要在 nginx 配置中添加代理文件路径。
location / {
include proxy_params;
proxy_pass http://unix:/home/username/myproject/gunicorn.sock;
}
这是一个很好的教程,逐步实现
注意:如果您没有创建 anyname.sock
文件,则必须先创建,请使用上述方法或任何其他方法或教程来创建它。
如果您使用的是 oracle linux,禁用强制可以解决问题
sudo setenforce 0
yum install policycoreutils-python
才能首先获得audit2allow
。参考:centos.org/forums/viewtopic.php?t=5012policycoreutils-python
没有提供命令audit2allow
。经过一番研究,我发现您应该安装开发包yum install policycoreutils-devel
。参考:danwalsh.livejournal.com/61710.html