关于前端:如何使用进行flasknginx构建

28次阅读

共计 4506 个字符,预计需要花费 12 分钟才能阅读完成。

查看端口号是不是被占用

netstat -anp | grep 8080
点击并拖拽以挪动
查看利用的过程

ps aux|grep nginx
点击并拖拽以挪动
flask 局部
首先 flask 写服务的局部略去(和业务相干)

gunicorn 局部
首先咱们的 flask 我的项目的启动形式是:

app.run(host=”0.0.0.0″,use_reloader=True, port=5000)
点击并拖拽以挪动
咱们应用 gunicorn 进行启动的话,不必写端口号,能够应用 gunicorn 设置启动的端口号

flask 服务的启动形式变成

app.run(debug=False)
点击并拖拽以挪动
应用 gunicorn 进行启动

原始命令 worker 4 线程是 4 个

gunicorn -w 4 manage:app
点击并拖拽以挪动

当咱们的 app 是应用工厂函数构建的时候

gunicorn -w 4 “my_project:create_app()”
点击并拖拽以挪动
实现下面的步骤,即可应用 gunicorn 实现 flask 的启动

supervisor 局部
次要是实现 gunicorn 的配置

咱们应用四个过程 (每个过程四个线程) 通过 gunicorn 对 flask 进行启动

[program:gunicorn]
command=gunicorn -w 4 “manage:create_app()” -b :8091
directory=/home/a21036/waws
priority=1
numprocs=1
autostart=true
autorestart=true
startretries=10
exitcodes=0
stopsignal=KILL
stopwaitsecs=10
redirect_stderr=true
[program:gunicorn_1]
command=gunicorn -w 4 “manage:create_app()” -b :8092
directory=/home/a21036/waws/
priority=1
numprocs=1
autostart=true
autorestart=true
startretries=10
exitcodes=0
stopsignal=KILL
stopwaitsecs=10
redirect_stderr=true
[program:gunicorn_2]
command=gunicorn -w 4 “manage:create_app()” -b :8093
directory=/home/a21036/waws/
priority=1
numprocs=1
autostart=true
autorestart=true
startretries=10
exitcodes=0
stopsignal=KILL
stopwaitsecs=10
redirect_stderr=true
[program:gunicorn_3]
command=gunicorn -w 4 “manage:create_app()” -b :8094
directory=/home/a21036/waws/
numprocs=1
autostart=true
autorestart=true
startretries=10
exitcodes=0
stopsignal=KILL
stopwaitsecs=10
redirect_stderr=true
点击并拖拽以挪动
咱们的 supervisor 没有启动就应用上面命令启动

supervisord -c /usr/supervisor/supervisor.conf
点击并拖拽以挪动
若咱们曾经启动了 supervisor, 咱们应用上面的命令来启动

supervisorctl
start gunicorn
start gunicorn_1
start gunicorn_2
start gunicorn_3
点击并拖拽以挪动
supervisorctl
start all
点击并拖拽以挪动
nginx 局部
次要是思考程序的并发性,对启动的四个服务,进行负载平衡

在 /etc/nginx/cond.d 中新建页游的两个文件,upstream.conf 管制散发的文件 backend.conf 理论的监听端口实现操作的配置

upstream.conf

upstream backends {

 server localhost:9001;
 server localhost:9002;
 server localhost:9003;
 server localhost:9004;

}
server {

 listen 80;
 location / {proxy_pass 

} }
点击并拖拽以挪动
backend.conf

server {

 listen localhost:9001;
 location ^~ /static/ {
    alias /home/a21036/static/;
    autoindex on;
 }
 location = /upload {
    proxy_pass 
    proxy_redirect off;
    proxy_set_header Host $http_post;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 location =/ {
     proxy_pass 
     proxy_redirect off;
     proxy_set_header Host $http_post;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

}
server {

 listen localhost:9002;
 location ^~ /static/ {
    alias /home/a21036/static/;
    autoindex on;
 }
 location = /upload {
    proxy_pass 
    proxy_redirect off;
    proxy_set_header Host $http_post;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 location =/ {
     proxy_pass 
     proxy_redirect off;
     proxy_set_header Host $http_post;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

}
server {

 listen localhost:9003;
 location ^~ /static/ {
    alias /home/a21036/static/;
    autoindex on;
 }
 location = /upload {
    proxy_pass 
    proxy_redirect off;
    proxy_set_header Host $http_post;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 location =/ {
     proxy_pass 
     proxy_redirect off;
     proxy_set_header Host $http_post;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

}
server {

 listen localhost:9004;
 location ^~ /static/ {
    alias /home/a21036/static/;
    autoindex on;
 }
 location = /upload {
    proxy_pass 
    proxy_redirect off;
    proxy_set_header Host $http_post;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 location =/ {
     proxy_pass 
     proxy_redirect off;
     proxy_set_header Host $http_post;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

}
点击并拖拽以挪动
在这个中央犯了一个重大的谬误:

server {

 listen localhost:8094;   # 谬误本源
 location ^~ /static/ {
    alias /home/a21036/static/;
    autoindex on;
 }
 location = /upload {
    proxy_pass 
    proxy_redirect off;
    proxy_set_header Host $http_post;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 location =/ {
     proxy_pass 
     proxy_redirect off;
     proxy_set_header Host www.sangpi.com$http_post;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

}
点击并拖拽以挪动
理论的谬误起因:咱们应用 supervisor 进行将 gunicorn 进行启动,启动了 8094 的服务,而后在应用 nginx 进行 listen 监听 8094,会呈现端口被占用的问题,我已咱们须要用其余端口对 8094 进行监听,这里应用 9004 对 8094 进行监控

咱们的 nginx 没有启动就应用上面命令启动

systemctl start nginx
点击并拖拽以挪动
若咱们曾经启动了 nginx, 咱们应用上面的命令来 reload 配置文件

检测咱们的 nginx 的配置语法是不是有问题

/usr/sbin/nginx -t

重载配置文件

/usr/sbin/nginx -s reload
点击并拖拽以挪动
其余局部
当咱们对配置呈现问题的时候,咱们能够通过配置文件中指定的 log 的地位,找到 log,查看 log 就能找到问题所在了,以下以 nginx 的日志为例

查看形式 1

tail -F xxxx.log # 新生成的日志以滚动的形式进行查看

查看形式 2

vim xxxx.log # 全量查看日志
点击并拖拽以挪动
nginx 又两个次要日志放在 /var/log/nginx

正文完
 0