【问题标题】:python - How to deploy Flask+Gunicorn+Nginx+supervisor on a cloud server?python - 如何在云服务器上部署 Flask+Gunicorn+Nginx+supervisor?
【发布时间】:2017-02-14 16:01:07
【问题描述】:

从昨天开始,我已经阅读了很多关于这个问题的说明,但它们都有类似的步骤。但是我一步一步地跟着,但仍然无法得到一切。

其实我可以让 Flask+Gunicorn+supervisor 工作,但是 Nginx 不能正常工作。

我使用 SSH 连接我的远程云服务器,但我没有在我的计算机上部署站点。

Nginx 安装正确,因为当我通过域名(又名example.com)访问该站点时,它会显示 Nginx 欢迎页面。

我用supervisor启动Gunicorn,配置是

[program:myapp]
command=/home/fh/test/venv/bin/gunicorn -w4 -b 0.0.0.0:8000 myapp:app
directory=/home/fh/test
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/fh/test/log/gunicorn.log
stderr_logfile=/home/fh/test/log/gunicorn.err

这里我将服务器绑定到端口 8000我实际上并不知道 0.0.0.0 代表什么,但我认为这并不意味着本地主机,因为我可以访问通过 http://example.com:8000 的网站,它运行良好。

然后我尝试使用 Nginx 作为代理服务器。

我删除了 /etc/nginx/sites-available/default' and '/etc/nginx/sites-enabled/default/ 并创建了 /etc/nginx/sites-available/test.com/etc/nginx/sites-enabled/test.com 并将它们符号链接。

test.com

server {
        server_name www.penguin-penpen.com;
        rewrite ^ http://example/ permanent;
}

# Handle requests to example.com on port 80
server {
        listen 80;
        server_name example.com;

        # Handle all locations
        location / {
                # Pass the request to Gunicorn
                proxy_pass http://127.0.0.1:8000;

                # Set some HTTP headers so that our app knows where the request really came from
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

据我了解,Nginx 所做的是当我访问 http://example.com 时,它会将我的请求传递给 http://example.com:8000

我不太确定我应该在这里使用proxy_pass http://127.0.0.1:8000,因为我不知道Nginx是否应该将请求传递给本地主机但是我尝试将其更改为0.0.0.0:8000但它仍然没有不行。

谁能帮忙?

【问题讨论】:

    标签: python nginx web flask server


    【解决方案1】:

    0.0.0.0 表示服务器将接受来自所有 IP 地址的连接。详情请参阅https://en.wikipedia.org/wiki/0.0.0.0

    如果 gunicorn 服务器侦听 127.0.0.1,则只有您(或与 gunicorn 服务器在同一台机器上的其他人)可以通过本地循环 https://en.wikipedia.org/wiki/Local_loop 访问它。

    但由于您使用 Nginx 接受来自 Internet 的连接,您只需 proxy_pass http://127.0.0.1:8000; 并将命令更改为 command=/home/fh/test/venv/bin/gunicorn -w4 -b 127.0.0.1:8000 myapp:app。在这种情况下,gunicorn 本身只需要接受来自与 gunicorn 运行在同一台机器上的 Nginx 的连接。

    整个过程是这样的

    Connections from the Internet -> Nginx (reverse proxy, listen on 0.0.0.0:80) -> Gunicorn (which runs your Python code, listen on 127.0.0.1:8000)
    

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 2015-04-21
      • 2020-12-26
      • 2019-12-09
      • 2016-04-23
      • 2014-03-31
      • 2017-04-13
      • 2018-01-19
      • 2018-11-27
      相关资源
      最近更新 更多