Centos 7 上使用nginx爲Node.js配置反向代理時錯誤:(13: Permission denied) while connecting to upstream

錯誤來源:Centos 7 上使用nginx爲Node.js配置反向代理時產生(13: Permission denied) while connecting to upstream的錯誤php

nginx配置文件內容以下:html

upstream node_server {
   server 127.0.0.1:5000 max_fails=3 fail_timeout=4s;
   server 127.0.0.1:5001 max_fails=3 fail_timeout=4s;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    index index.html index.htm;

    server_name _;


    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_redirect off;
        proxy_buffer_size 16k;
        proxy_buffers 4 32k;
        proxy_pass http://node_server;
    }

    location /public/ {
        root /opt/app;
    }
}

使用Node.js靜態文件交由nginx管理,而動態的請求有Node.js處理(app.js須要從新配置),配置成功後分別啓動nginx服務和Node.js服務,訪問http:127.0.0.1出現 502 服務器端錯誤,node

查閱各類資料發雖然有nginx在反向代理配置時雖然有出現此類錯誤,但並無解決問題。linux

查看nginx錯誤日誌:nginx

cat /var/log/nginx/error.log

發現錯誤:failed (13: Permission denied) while connecting to upstream服務器

緣由及解決方案:問題是由SElinux致使,查看audit日誌:app

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

 

錯誤信息:socket

type=AVC msg=audit(1437452895.972:639): avc: denied { name_connect } for pid=5884 comm="nginx" dest=5000 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:commplex_main_port_t:s0 tclass=tcp_socket permissive=0tcp

修復此錯誤:spa

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

問題最終獲得解決。(附上最終成功界面截圖)

解決方法在stackoverflow中的連接爲:http://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx

相關知識連接:1.SElinux http://vbird.dic.ksu.edu.tw/linux_basic/0440processcontrol_5.php

                    2.audit2allow https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Fixing_Problems-Allowing_Access_audit2allow.html

相關文章
相關標籤/搜索