【问题标题】:Getting 404 when accessing Flask app behind nginx访问 nginx 后面的 Flask 应用程序时出现 404
【发布时间】:2017-01-08 23:23:19
【问题描述】:

我已经在 nginx 下配置了一个虚拟主机:

server {
        listen          80;
        server-tokens   off;
        access_log      /var/log/nginx/api.example.com.access.log;
        error_log       /var/log/nginx/api.example.com.error.log;
        index           index.html index.htm;
        server_name     api.example.com;
        autoindex       on;

        location / {
                include         uwsgi_params;
                uwsgi_pass      unix:/var/www/api.example.com/xxx.sock;
        }
}

此文件位于sites-availablesites-enabled 下。

我已经配置了uwsgi

[uwsgi]
chmod-socket = 666
socket = /var/www/api.example.com/xxx.sock
processes = 5
threads = 10
master = True
module = xxx 
callable = app 
chdir = /var/www/api.example.com
venv = /home/xxx/.virtualenvs/xxx
thunder-lock = True
uid = www-data
gid = www-data
plugins = http,python
vhost = True

我可以让 uwsgi 和 nginx 成功启动,但是当我转到 http://api.example.com 时,我总是得到一个 nginx 404。我假设他们都通过同一个套接字进行通信,因为没有发生与之相关的错误。我还可以看到在 /var/www/api.example.com//var/www/api.example.com/app 下生成的 .pyc 文件,我保留了我的视图和模型。

如何调试?

【问题讨论】:

    标签: python nginx flask virtualenv uwsgi


    【解决方案1】:

    不确定,但这个 nginx conf 可能有效。如果没有,请查看 nginx 和 uwsgi 日志。

    
    server {
        listen      80;
        server_name api.xxxx.com;
        charset     utf-8;
        client_max_body_size 250M;
    
    
    root /var/www/api.xxx.com/;
    index index.html index.htm;
    
    location / {
        try_files $uri $uri/ @proxy; 
    }
    
    location @proxy {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/api.xxx.com/xxx.sock
    
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }
    

    }

    【讨论】:

    • 现在我得到 403 - 这仍然比 404 好。
    • 403 表示认证错误,你是用flask-login还是其他什么做认证?
    • 不,只是带有 SQLAlchemy 和 Redis 的原始 Flask。会是这样吗?
    • 打开 nginx 上的目录索引显示该目录中所有文件的列表,单击它下载的 .py 文件 - 所以我想请求没有到达 uwsgi?
    • 只是好奇,如果你删除 uwsgi conf 中的 uid 和 gid 参数会怎样?
    【解决方案2】:

    我发现了问题:Flask 和 uwsgi 有不同的应用程序名称。当我回复他们与上面的名称相同时,我忘记重新启动服务器了。

    【讨论】:

      【解决方案3】:

      首先确保你的 nginx 有 HttpUwsgiModule,你可以通过运行命令“nginx -V”来检查。它将列出所有已配置的模块。

      这里有很好的链接http://wiki.nginx.org/HttpUwsgiModule

      【讨论】:

        猜你喜欢
        • 2021-05-04
        • 1970-01-01
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多