欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 【CentOS7】nginx部署前端 gunicorn部署flask后端并使用nginx反向代理

【CentOS7】nginx部署前端 gunicorn部署flask后端并使用nginx反向代理

2024/11/30 18:53:59 来源:https://blog.csdn.net/BY_HEY/article/details/142489572  浏览:    关键词:【CentOS7】nginx部署前端 gunicorn部署flask后端并使用nginx反向代理

一、前端

  • 将编译好的前端文件放入服务器中/usr/local/project/test_case/dist
  • 编辑/usr/local/webserver/nginx/conf/nginx.conf,此为nginx的配置文件
user nginx;
worker_processes  2;error_log  /logs/nginx/error.log crit;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /logs/nginx/access.log  main;sendfile        on;tcp_nopush      on;tcp_nodelay     on;#keepalive_timeout  0;keepalive_timeout  65;gzip  on;# HTTP 服务server {listen       80;server_name  localhost; #charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}error_page  404 /404.html;# redirect server error pages to the static page /50x.htmlerror_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}server {listen       84;server_name  localhost;location / {root   /usr/local/project/test_case/dist;index  index.html index.htm;}}# HTTPS 服务server {listen       443 ssl;server_name  localhost;ssl_certificate      cert.pem;ssl_certificate_key  cert.key;ssl_session_cache    shared:SSL:1m;ssl_session_timeout  10m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;# ssl_ciphers PROFILE=SYSTEM;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_prefer_server_ciphers on;location / {root   html;index  index.html index.htm;}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}}
  • 这里记得要给防火墙开放端口84并重启(看之前文章)这里就不赘述

问题

  • 这里成功访问网站之后再次刷新可能会出现404的问题
  • 解决方法:在 location / 中加入try_files $uri $uri/ /index.html;配置
 location / {root       /root/xxx;index      index.html;try_files $uri $uri/ /index.html;}

二、后端(Flask)

  • 下载 Python3
# 下载源码
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
# 解压
tar -xvJf Python-3.6.2.tar.xz
# 进入解压后目录
cd Python-3.6.2
# 配置
./configure prefix=/usr/local/python3
# 编译 && 安装
make && make install
# 创建软链接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
# 查看是否安装成功
python3 -V
  • 更新 pip3
# 这里要先用国内源更新pip3,不然后面下载包的时候会报错
pip3 install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
  • 下载flask && gunicorn
# 安装 flask
pip3 install flask
# 安装 gunicorn
pip3 install gunicorn
  • 接下来我们把用flask写好的run.py文件放到/usr/local/flask-project/run.py
from flask import Flaskapp = Flask(__name__)@app.route("/")
def hello():return "Hello, World!"if __name__ == "__main__":app.run()
  • 注意这里还有一个坑,我们还不能直接运行gunicorn,还需要将gunicorn加入到环境变量中或者创建软链接,这里选择创建软链接
ln -s /usr/local/python3/bin/gunicorn /usr/bin/gunicorn
  • 然后我们运行gunicorn来启动我们的flask服务
gunicorn -D -w 4 -b 127.0.0.1:5000 run:app
  • 接下来我们用nginx来进行反向代理(防火墙放开端口就不赘述)

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}server{listen   84;server_name localhost;location /{root   /usr/local/webserver/nginx/html/testcase;index  index.html;try_files $uri $uri/ /index.html;}}server{listen   5001;server_name localhost;location /{proxy_pass http://127.0.0.1:5000;}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

最后我们就可以成功访问我们的flask服务了

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com