【问题标题】:(13: Permission denied) while connecting to upstream:[nginx](13:权限被拒绝)同时连接到上游:[nginx]
【发布时间】:2014-07-19 20:31:36
【问题描述】:

我正在使用 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() 到 127.0.0.1:8001 连接到上游时失败(13:权限被拒绝),客户端:127.0.0.1,服务器:本地主机,请求:“GET / HTTP/1.1”,上游:"http://127.0.0.1:8001/",主机:“本地主机: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

我做错了什么?

【问题讨论】:

    标签: django python-2.7 nginx gunicorn


    【解决方案1】:

    免责声明

    在运行此之前,请确保您的用例没有安全隐患。

    回答

    我在让 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

    【讨论】:

    • 谢谢。我需要yum install policycoreutils-python 才能首先获得audit2allow。参考:centos.org/forums/viewtopic.php?t=5012
    • 另见here。在我的情况下,我必须将 nginx 添加到存储 wwwroot 的主目录的用户组中。
    • 在 Fedora 23 上安装 policycoreutils-python 没有提供命令 audit2allow。经过一番研究,我发现你应该安装开发包yum install policycoreutils-devel。参考:danwalsh.livejournal.com/61710.html
    • 这应该在许多 unix 操作系统的 cherrpy 和 nginx 文档中,因为我在遵循所有文档后浪费了 8 个小时试图弄清楚!
    • 请注意,设置 httpd_can_network_connect 如果打开,可能会给您的面向公众的服务器带来严重的安全风险。见thisthis
    【解决方案2】:

    我也遇到过这个问题。另一种解决方案是将 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
    

    【讨论】:

    • 这行得通,谢谢。我从 CentOS 6.5 -> 6.7 更新,它必须在更新期间默认值关闭,因为它在更新之前工作正常。简单的修复。
    • 请注意,设置 httpd_can_network_connect 如果打开,可能会给您的面向公众的服务器带来严重的安全风险。见thisthis
    【解决方案3】:

    我已经解决了我的问题,以我当前登录的用户身份运行我的 Nginx,mulagala

    默认情况下,用户为 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

    【讨论】:

      【解决方案4】:

      在 Centos 7 上遇到了类似的问题。当我尝试应用 Sorin 规定的解决方案时,我开始循环移动。首先,我有一个权限 {write} 被拒绝。然后,当我解决了我的权限 { connectto } 被拒绝时。然后再次返回权限 {write } denied。

      按照上面的@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, denied 获得的错误相关联

      【讨论】:

        【解决方案5】:

        如果 centos api url for api gateway proxy pass on nginx 抛出“502 Bad Gateway”错误,请运行以下命令解决问题

        sudo setsebool -P httpd_can_network_connect 1
        

        【讨论】:

          【解决方案6】:
          1. 检查/etc/nginx/nginx.conf中的用户
          2. 将所有权更改为用户。
          sudo chown -R nginx:nginx /var/lib/nginx
          

          现在看看魔法吧。

          【讨论】:

            【解决方案7】:
            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
            
            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 好的,很好!
            • 不要在安全的环境中这样做,关闭安全策略是一种创可贴的修复方法,会使系统不安全。
            【解决方案8】:

            我也遇到过这个问题。我将 Nginx 与 HHVM 一起使用,以下解决方案解决了我的问题:

            sudo semanage fcontext -a -t httpd_sys_rw_content_t "/etc/nginx/fastcgi_temp(/.*)?"
            
            sudo restorecon -R -v /etc/nginx/fastcgi_temp
            

            【讨论】:

              【解决方案9】:

              13-permission-denied-while-connecting-to-upstreamnginx 在 centos 服务器上 -

              setsebool -P httpd_can_network_connect 1

              【讨论】:

                【解决方案10】:

                另一个原因可能是;您正在使用代理通过 nginx 访问您的应用程序,但您没有使用 gunicorn 为代理添加 gunicorn.sock 文件。

                需要在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.sock 文件,则必须先创建,请使用上述方法或任何其他方法或教程来创建它。

                【讨论】:

                • 我认为您不需要使用 gunicorn.sock 文件。那是可选的。如果需要,您应该能够在没有 gunicorn.sock 的情况下设置 Nginx、Gunicorn 和 Django。
                猜你喜欢
                • 2022-01-03
                • 2016-05-12
                • 2014-03-16
                • 2018-09-13
                • 2015-07-04
                • 2017-11-03
                • 1970-01-01
                • 1970-01-01
                • 2016-05-25
                相关资源
                最近更新 更多