测试下nginx+多uwsgi的配置.
一个flask项目:
wangxh@mac : ~/myapp/myhome$ tree
.
├── __init__.py
├── s.ini
├── s2.ini
├── templates
│ └── index.html
├── ui.py
└── ui.pyc
wangxh@mac : ~/myapp/myhome$ cat s.ini
[uwsgi]
socket = /tmp/s.sock
master = true
module = ui
callable = app
uid = 4294967294
gid = 4294967294
buffer-size = 25000
wangxh@mac : ~/myapp/myhome$ cat s2.ini
[uwsgi]
socket = /tmp/s2.sock
master = true
module = ui
callable = app
uid = nobody
gid = nobody
buffer-size = 25000
wangxh@mac : ~/myapp/myhome$ cat ui.py
#!/usr/bin/python
#coding=utf-8
import sys
sys.path.append('/Library/Python/2.7/site-packages')
from flask import Flask, request, render_template, redirect, url_for, session, flash
import flask
import jinja2
import datetime
import urlparse
import random
import hashlib
import re
app = Flask(__name__)
app.secret_key = '\x9a\xf8pJp\xbf\xbdBY\xb0\xfd\xa68\xac\x809j\x0c\xd6\x89\xc5,\xcf\xc2'
@app.route('/')
def index():
return render_template("index.html")
if __name__ == "__main__":
#app.debug = True
app.run()
配置nginx:
wangxh@mac : ~/myapp/myhome$ cat /usr/local/nginx-1.3.6/conf/nginx.conf|grep -v '#'|grep -v '^$'
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream ui {
server unix:///tmp/s.sock;
server unix:///tmp/s2.sock;
}
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass ui;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
运行,在两个终端中分别运行:
uwsgi --ini s.ini
uwsgi --ini s2.ini
重启nginx,然后再开一个终端执行:
wangxh@mac : ~$ while true;do curl -vv http://localhost/;sleep 1;done
再uwsgi终端即可观察到请求被均匀分布到两个uwsgi