(13: Permiso denegado) mientras se conecta a upstream: [nginx]


Estoy trabajando con la configuración del proyecto django con nginx y gunicorn. Mientras estoy accediendo a mi puerto gunicorn mysite.wsgi:application --bind=127.0.0.1:8001 en el servidor nginx, recibo el siguiente error en mi archivo de registro de errores.

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"

Mi nginx.conf archivo

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;
    }
}

En la página html obtengo 502 Bad Gateway.

¿Qué error estoy haciendo?

Author: jfunez, 2014-05-30

6 answers

Tuve un problema similar al obtener Fedora 20, Nginx, Node.js, y Ghost (blog) para trabajar. Resulta que mi problema se debió a SELinux .

Esto debería resolver el problema:

setsebool -P httpd_can_network_connect 1

Detalles

He comprobado si hay errores en los registros de SELinux:

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

Y encontró que ejecutar los siguientes comandos arregló mi cuestión:

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

Referencias:

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

Http://www.linuxproblems.org/wiki/Selinux

 403
Author: joebarbere,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-07 04:19:13

Me he encontrado con este problema también. Otra solución es cambiar el valor booleano de SELinux para httpd network connect a on (Nginx usa la etiqueta httpd).

setsebool httpd_can_network_connect on

Para hacer que el cambio persista use el indicador-P.

setsebool httpd_can_network_connect on -P

Puede ver una lista de todos los booleanos de SELinux disponibles para httpd usando

getsebool -a | grep httpd
 145
Author: Sid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-14 10:18:00

He resuelto mi problema ejecutando mi nginx como mi usuario actual que es mulagala. Por defecto el usuario como nginx en mi archivo nginx.conf.Podemos encontrar esa línea en la parte superior del archivo nginx.conf.

user nginx;

Cambie esto a su nombre de usuario actual como

user  mulagala;
 11
Author: Mulagala,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-04 10:52:32

Tuvo un problema similar en Centos 7. Cuando intenté aplicar la solución prescrita por Sorin, empecé a moverme en ciclos. Primero tuve un permiso {write} denegado. Entonces cuando resolví que tenía un permiso { connectto } denegado. Luego de nuevo a permission {write} denied.

Siguiendo la respuesta de @Sid anterior de verificar las banderas usando getsebool -a | grep httpd y alternarlas, encontré que además de que httpd_can_network_connect está desactivado. http_anon_write también estaba desactivado, lo que resultó en un permiso denegado escritura y permiso denegado {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

Obtenido utilizando sudo cat / var/log/audit / audit.log | grep nginx | grep denegado como se explicó anteriormente.

Así que los resolví uno a la vez, alternando las banderas en uno a la vez.

setsebool httpd_can_network_connect on -P

Luego ejecutar los comandos especificados por @ sorin y @ Joseph arriba

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

Básicamente puedes comprobar los permisos establecidos en setsebool y correlacionarlos con el error obtenido de la auditoría grepp' ing'.log nginx, denied

 9
Author: unlockme,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-04 07:45:22

Me he encontrado con este problema también. Estoy usando Nginx con HHVM, debajo de la solución solucionada mi problema:

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

sudo restorecon -R -v /etc/nginx/fastcgi_temp
 1
Author: sule,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-15 08:45:03
sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx

sudo semodule -i mynginx.pp
 -2
Author: TitaniuM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-06 20:20:56