我正在使用Nginx和Gunicorn配置Django项目。
当我进入gunicorn端口时。在Nginx服务器中,我在错误日志文件中得到以下错误;
2014/05/30 11:59:42 [crit] 4075#0: *6 connect()到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坏网关。
我犯了什么错误?
另一个原因可能是;您正在通过nginx使用代理访问您的应用程序,但您没有添加gunicorn。袜子文件代理gunicorn。
需要在nginx配置中添加代理文件路径。
location / {
include proxy_params;
proxy_pass http://unix:/home/username/myproject/gunicorn.sock;
}
这是一个很好的教程,一步一步地实现这个功能
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04#configure-nginx-to-proxy-pass-to-gunicorn
注意:如果你没有创建anyname。袜子文件你必须创建如果首先,要么使用上述或任何其他方法或教程来创建它。
免责声明
在运行此操作之前,请确保您的用例没有安全隐患。
回答
我有类似的问题,让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
首先看看被否定的是什么:
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
我也遇到过这个问题。另一个解决方案是将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