在配置nginx作为反向代理和负载均衡,同时配置python脚本接收文件上传参数的情况下,可以在nginx.conf文件新增一个server。具体操作和步骤如下
一,配置nginx作为反向代理和负载均衡
参考 nginx反向代理、负载均衡、限流配置详解;搭配apache或tomcat环境搭建详解-优快云博客
二,配置python flask 应用以接收上传文件参数
nginx配置示例
假设有多个需要运行Python Flask应用的后端服务器,可以在nginx这样配置
http {
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
location / {
proxy_pass http://backend;
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;
}
}
}
Python Flask 应用示例
Python Flask应用可以使用Flask
和requests
库来处理上传文件和转发请求:
from flask import Flask, request, redirect, url_for
from werkzeug.utils import secure_filename
import requests
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/path/to/the/uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Forward the file to the backend
url = "http://backend-service-url"
files = {'file': (filename, open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb'), 'application/octet-stream')}
response = requests.post(url, files=files)
return response.text
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
在这个Python Flask应用中,它会接收文件上传,保存到指定目录,并转发文件到后端服务。确保后端服务URL是可以处理文件上传的内部服务地址。
以上代码仅为示例,请根据实际环境和需求进行相应的调整。
三,配置同时使用如php,python,ruby等应用开发程序
nginx.conf文件
在其他应用upstream后添加
#代理转发地址 py
upstream py{
server 127.0.0.1:8080 weight=5;
#server 127.0.0.1:8080 weight=5; #若存在多个服务器使用py应用程序 自行填写
keepalive 16;
}
在文件末尾解开include server/* 注释
include servers/*;
在vhost.conf文件
添加
#py 模块
server {
listen 80;
root /root/www/py;
index index.py index.html;
server_name local.py.com;
location / {
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.py/$1 last;
}
}
location ~ /index\.py {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://py;
}
}
文件目录地址自行修改
四,修改hosts
一般位置在/etc 使用命令如下
sudo vim hosts
在文件中添加
127.0.0.1 local.py.com
命令刷新生效
sudo killall -HUP mDNSResponder
五,在apache配置文件中添加
httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so
<VirtualHost *:8080>
ServerName example.com
DocumentRoot /path/to/your/python/app
WSGIDaemonProcess myapp python-path=/path/to/your/python/app
WSGIProcessGroup myapp
WSGIScriptAlias / /path/to/your/python/app/wsgi.py
<Directory /path/to/your/python/app>
Require all granted
</Directory>
# 静态文件目录
Alias /static/ /path/to/your/python/app/static/
<Directory /path/to/your/python/app/static>
Require all granted
</Directory>
</VirtualHost>
注,可以根据实际情况选择使用wsgi 或者 uwsgi
两者区别和用法可参考
nginx反向代理、负载均衡、限流配置详解;搭配apache或tomcat环境搭建详解-优快云博客
fcgi 、 wsgi 、 uwsgi 区别用法
httpd-vhost.conf
#py
<VirtualHost *:8080>
ServerName local.py.com:8080
DocumentRoot "/Users/your_addr/www/py"
<Directory "/Users/your_addr/www/py">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
重启nginx和apache 后即可
六、java同上原理
http {
upstream java_api {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
location / {
proxy_pass http://java_api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
步骤同python
其他参数配置参考