我的网站上PHP写的,但是我的爬虫和人工智能项目使用python写的,那么如何将两种项目同时运行,文章介绍了如何通过Nginx配置实现PHP网站对Python(Flask+WSGI)项目的流量转发。首先确保Python项目正常运行,然后配置Nginx以新的端口转发请求到Python服务的内网地址,通过域名访问即指向Python应用,简化了不同语言项目间的交互。
搭建Python web flask +wsgi 需要使用wsgi模式 nginx才支持
该模式可以可以参考上一篇文章:python 对接阿里通义万象文生图
开启已经写好的python项目,检查项目是否可以正常运行
/www/wwwroot/Python/myenv/bin/python3 -m flask --app tongyiwxiang run --host=0.0.0.0

浏览器输入http://ip地址:5000/tongyiwanxiang,检查项目是否正常运行

方法一:可以通过宝塔自带的ngnix反向代理,将通过代理域名访问python项目
首先添加一个站点

事先准备一个解析到自己的服务器的域名

创建




浏览器输入http://pywstx.zlhdsg.com/tongyiwanxiang测试

如果想用https,可以通过宝塔面板申请免费证书



浏览器输入https://pywstx.zlhdsg.com/tongyiwanxiang测试

nginx设置反向代理接口超时时间
proxy_read_timeout 600s;


以上为宝塔自带反向代理方式
方式二:如果不使用宝塔可以自己配置ngnix反向代理也是可以的,将上边配置网站关闭
查看ngnix 配置

配置文件拉倒底就是我们创建php网站的配置,记录下路径
include /www/wwwroot/ngnix/zhuanfa.conf;
zhuanfa.conf文件放到上方图片箭头文件上方,不然遗留配置会覆盖这个文件
server {
listen 80;
server_name pywstx.zlhdsg.com;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# HTTP反向代理相关配置结束 <<<
access_log /www/wwwlogs/wstx.log;
error_log /www/wwwlogs/wstx.error.log;
}
浏览器输入http://pywstx.zlhdsg.com/tongyiwanxiang测试

如果想用https可以通过配置文件
server {
listen 80;
listen 443 ssl http2 ;
server_name pywstx.zlhdsg.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/Python/wstx;
#CERT-APPLY-CHECK--START
# 用于SSL证书申请时的文件验证相关配置 -- 请勿删除
include /www/server/panel/vhost/nginx/well-known/pywstx.zlhdsg.com.conf;
#CERT-APPLY-CHECK--END
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
ssl_certificate /www/server/panel/vhost/cert/pywstx.zlhdsg.com/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/pywstx.zlhdsg.com/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_tickets on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# HTTP反向代理相关配置结束 <<<
access_log /www/wwwlogs/wstx.log;
error_log /www/wwwlogs/wstx.error.log;
}
ssl_certificate和 ssl_certificate_key 处使用的是宝塔申请的ssl证书文件,也可以用其他证书
浏览器输入https://pywstx.zlhdsg.com/tongyiwanxiang测试

以上为nginx反向代理目标URL的全部操作