当前位置: 首页 > news >正文

【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服务了


http://www.mrgr.cn/news/35932.html

相关文章:

  • FlinkPipelineComposer 详解
  • C++中的栈(Stack)和堆(Heap)
  • 应用jar包使用skywalking8(Tongweb7嵌入式p11版本 by lqw)
  • Visual Studio 2022 安装
  • react-redux useSelector钩子 学习样例 + 详细解析
  • 【FreeRL】MAPPO的简单复现
  • 引用reference作为函数返回
  • 细说机房安装带孔的通风防静电地板的原因
  • 【C++进阶】2024年了set、map还搞不懂底层细节?
  • 接口中心四大闭环:如何确保接口生命周期的完美呈现
  • C语言中的转义字符
  • 如何恢复被删除的 GitLab 项目?
  • 基于丹摩智算的`YoloV8-训练与测试
  • Python面向对象编程:类和对象①
  • ant design vue组件中table组件设置分组头部和固定总结栏
  • _RET_IP_ 和_THIS_IP_ 作用
  • 通信工程高级职称评审条件详细解读
  • Databend 为什么能帮用户降低 90% 成本?
  • 直播平台美颜功能开发方案:基于视频美颜SDK的集成详解
  • mybatis-plus公共字段自动填充fillStrategy()方法和strictFill()方法
  • FTP服务搭建
  • 博科测试IPO上市关注:汽车测试试验业务发展迅速
  • Oracle 相关的工具使用 SQL Developer , sqlplus
  • JAVA输入输出处理技术
  • 2024 Redis 全部
  • Redis结合Caffeine实现二级缓存:提高应用程序性能