描述

在部署 Python Web 应用的时候,几乎不会有人在生产中是直接运行 Flask 或者 Django 应用的,常见套路都是加一层 WSGI 并且用常规的 Web Server 做一个反向代理,今天我进行反向代理的时候就遇到了一个错误,这里介绍一下问题。

配置

我的 Nginx 配置大概是这样个样子:

    upstream bigbang {
        server 127.0.0.1:5000;
    }

    server {
        location /bigbang {
            proxy_pass http://bigbang;
        }

        ... ...
    }

然而用浏览器打开 http://liqiang.io/bigbang 的时候,居然是一个 404,但是我确认我的 Web Server 一切都是正常的,因为我使用 http://localhost:5000/bigbang 访问是毫无问题的。

所以就查看了一下 Nginx 的日志,发现居然是 Permission Deny,有点奇怪,所以就简单查了一下。

[[email protected]]# tailf /var/log/nginx/error.log
2019/10/20 17:03:20 [crit] 15768#0: *31 connect() to 127.0.0.1:5000 failed (13: Permission denied) while connecting to upstream, client: ::1, server: _, request: "GET /bigbang/threadsList?links HTTP/1.1", upstream: "http://127.0.0.1:5000/bigbang/threadsList", host: "localhost", referrer: "http://localhost/"

解决方式

简单查了一下之后发现是 SELinux 的问题,把它关闭就可以了:

[[email protected]]# setenforce 0

Ref