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

devtools/2024/9/29 9:02:09/

一、前端

  • 将编译好的前端文件放入服务器中/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服务了


http://www.ppmy.cn/devtools/118615.html

相关文章

Linux应用开发实验班——JSON-RPC

目录 前言 1.是什么JSON-RPC 2.常用的JSON函数 1.创建JSON 2.根据名字获取JSON 3.获取JSON的值 4.删除JSON 3.如何进行远程调用 服务器 客户端 4.基于JSON-RPC进行硬件操作 课程链接 前言 学习的课程是百问网韦东山老师的课程,对更详细步骤感兴趣的同学…

Oracle的归档操作

1、查询数据库归档模式 两种方式:查询v$database和命令查看。 NOARCHIVELOG和No Archive Mode都是未开启归档 SQL> select name,log_mode from v$database;NAME LOG_MODE --------- ------------ ORCL NOARCHIVELOGSQL> archive log list;Databa…

笔记整理—linux进程部分(2)使用fork创建进程

为什么要创建进程,首先每个程序的运行都需要一个进程;多进程实现宏观上的并行。 fork的原理,是进程的分裂生长模式。如果操作系统需要一个新的进程,那么就会以cp的方法得到一个新的进程,此时老的进程是父进程&#xff…

【电商搜索】现代工业级电商搜索技术-Facebook语义搜索技术QueSearch

【电商搜索】现代工业级电商搜索技术-Facebook语义搜索技术Que2Search 目录 文章目录 【电商搜索】现代工业级电商搜索技术-Facebook语义搜索技术Que2Search目录0. 论文信息1. 研究背景:2. 技术背景和发展历史:3. 算法建模3.1 模型架构3.1.1 双塔与分类 …

【春秋云境】CVE-2024-23897-Jenkins 2.441之前版本存在任意文件读取漏洞

一、靶场介绍 Jenkins 2.441及更早版本,以及LTS 2.426.2及更早版本没有禁用其CLI命令解析器的一个功能,该功能会将参数中’字符后跟的文件路径替换为该文件的内容,允许未经身份验证的攻击者读取Jenkins控制器文件系统上的任意文件。 二、P…

深刻理解Redis集群(上):RDB快照和AOF日志

RDB快照 save同步阻塞 客户端 服务端 .conf配置文件 # The filename where to dump the DB dbfilename dump.rdb# rdb-del-sync-files是Redis配置文件中的一个选项,它的作用是在主节点上执行BGSAVE或AOF持久化操作时,删除同步锁文件,以释放磁…

linux环境下使用sqlplus访问远程oracle数据库

1.前提 由于sqlplus rpm包安装,需要其它rpm包依赖,建议linux服务器需要提前配置好yum源 2.下载rpm安装包 下载地址(需要oracle account):Instant Client for Linux x86-64 (64-bit) E.g: oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm oracle-instantclie…

上交所服务器崩溃:金融交易背后的技术隐患暴露杭州BGP高防服务器43.228.71.X

一、上交所宕机事件始末 2024 年 9 月 27 日,上交所交易系统突发崩溃,这一事件犹如一颗巨石投入平静的湖面,引起了轩然大波。当天上午,众多投资者反馈券商交易出现延迟问题,随后上交所发布了《关于股票竞价交易出现异常…