启用S​​ELinux时,proxy_pass不起作用,为什么?

时间:2014-12-12 01:57:38

标签: nginx reverse-proxy selinux

我有一个应用程序正在侦听端口8080和Nginx在端口8080上运行。代理传递语句如下所示:

$ cat /var/etc/opt/lj/output/services/abc.servicemanager.conf

location /api/abc.servicemanager/1.0 { proxy_pass     http://localhost:8081;}

nginx.conf中,我将此文件包含为:

include /etc/nginx/conf.d/services/*.conf;

/etc/nginx/conf.d/service是一个符号链接:

# ll /etc/nginx/conf.d/

lrwxrwxrwx. 1 root root   39 Dec 10 00:19 services -> ../../../var/etc/opt/lj/output/services

这是一个CentOS 7.0 SELinux Enabled系统。如果我setenforce 0,并让它宽容,我就不会发现任何问题。所以文件在正确的位置,路径没有问题。如果SELinux正在执行,我在审计日志中看到以下内容:

type=AVC msg=audit(1418348761.372:100930): avc:  denied  { getattr } for  pid=3936 comm="nginx" path="/var/etc/opt/lj/output/services/abc.servicemanager.conf" dev="xvda1" ino=11063393 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:var_t:s0 tclass=file

我想知道如何在不必禁用SELinux的情况下启用Nginx来查找conf文件。

4 个答案:

答案 0 :(得分:33)

值得注意的是SELinux中的初学者,如果您的代理服务在8080上运行,您可以使用以下命令而不编译策略。

$ sudo setsebool httpd_can_network_connect 1 -P

答案 1 :(得分:23)

了解audit2allow并使用它来创建一个策略,允许访问Nginx的被拒绝请求。

第1步涉及运行audit2allow定位nginxlocalconf:

$ sudo grep nginx /var/log/audit/audit.log | \
     grep denied | audit2allow -m nginxlocalconf > nginxlocalconf.te

第2步,审核结果:

$ cat nginxlocalconf.te 

module nginxlocalconf 1.0;

require {
    type httpd_t;
    type var_t;
    type transproxy_port_t;
    class tcp_socket name_connect;
    class file { read getattr open };
}

#============= httpd_t ==============

#!!!! This avc can be allowed using the boolean 'httpd_can_network_connect'
allow httpd_t transproxy_port_t:tcp_socket name_connect;
allow httpd_t var_t:file { read getattr open };

查看要激活的步骤:

$ sudo grep nginx /var/log/audit/audit.log | grep denied | \
   audit2allow -M nginxlocalconf
******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i nginxlocalconf.pp

第3步,激活:

$ sudo semodule -i nginxlocalconf.pp

答案 2 :(得分:3)

始终更喜欢将类型更改为创建自定义策略。在这种情况下,Nginx将提供httpd_sys_content_t类型的文件。假设您的文件位于/ var / www:

semanage fcontext -a -t httpd_sys_content_t /var/www/*
restorecon -R -v /var/www

答案 3 :(得分:2)

如果您有其他端口或自定义端口允许它:

在http:

中显示允许端口
semanage port -l | grep http

这是在我的localhost中输出的:

http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989

并允许8081:

semanage port -a -t http_port_t -p tcp 8081