python-如何部署Flask+;Gunicorn&x2B;Nginx+;云服务器上的管理员?
从昨天开始,我已经阅读了很多关于这个问题的说明,但它们都有类似的步骤。但是我一步一步地跟着,但还是不能把一切都做好 实际上,我可以让Flask+Gunicorn+supervisor工作,但Nginx工作不好 我用SSH连接我的远程云服务器,并且我没有在我的计算机上部署该站点 Nginx安装正确,因为当我通过域名(aka.python-如何部署Flask+;Gunicorn&x2B;Nginx+;云服务器上的管理员?,python,nginx,web,flask,server,Python,Nginx,Web,Flask,Server,从昨天开始,我已经阅读了很多关于这个问题的说明,但它们都有类似的步骤。但是我一步一步地跟着,但还是不能把一切都做好 实际上,我可以让Flask+Gunicorn+supervisor工作,但Nginx工作不好 我用SSH连接我的远程云服务器,并且我没有在我的计算机上部署该站点 Nginx安装正确,因为当我通过域名(aka.example.com)访问站点时,它会显示Nginx欢迎页面 我使用supervisor启动Gunicorn,配置为 [program:myapp] command=/hom
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代表什么,但我认为这并不意味着本地主机,因为我可以通过访问站点,而且它运行良好。
然后我尝试使用Nginx作为代理服务器
我删除了/etc/nginx/sites available/default'和'/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\u passhttp://127.0.0.1:8000
在这里,因为我不知道Nginx是否应该将请求传递给localhost,但我已尝试将其更改为0.0.0.0:8000
,但仍然不起作用。
有人能帮忙吗?
0.0.0.0
表示服务器将接受来自所有IP地址的连接。有关更多详细信息,请参阅
如果gunicorn服务器侦听127.0.0.1
,则只有您(或与gunicorn服务器在同一台机器上的其他人)可以通过本地循环访问它
但是,由于您使用Nginx接受来自internet的连接,因此您只需proxy\u pass即可http://127.0.0.1:8000;
并将命令更改为命令=/home/fh/test/venv/bin/gunicorn-w4-b 127.0.0.1:8000 myapp:app
。在这种情况下,gunicorn本身只需要接受来自Nginx的连接,Nginx与gunicorn在同一台机器上运行
整个过程是这样的
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)