10月 16

用Nginx为uwsgi后端图片服务器做缓存服务

  很早处理的问题,现在拿出来做个记录。
  需求:后端图片服务处理各种格式、尺寸的图片。使用python处理,系统为Django+uwsgi(其实直接用tornado更好,也更方便)。前端需要做缓存,直接使用nginx。当前问题nginx和uwsgi连接后就不能使用cache服务了,nginx的proxy cache服务是在upstream的流转发过程中实现。所以要做个内部转发。nginx部分配置如下:

日志部分,添加缓存记录方将来统计击中率,参数为upstream_cache_status。
日志的中表示说明
MISS 未命中,请求被传送到后端
HIT 缓存命中
EXPIRED 缓存已经过期请求被传送到后端
UPDATING 正在更新缓存,将使用旧的应答
STALE 后端将得到过期的应答

log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for” “$request_time” ‘
‘”$upstream_cache_status”‘;

转发的主要部分

upstream  local_img {
   server localhost:81;
   }
server{
       listen       81;
       server_name 127.0.0.1;
       location / {
                uwsgi_pass   127.0.0.1:9000;
                client_max_body_size   10m;
                include       uwsgi_params;
                access_log  off;
                autoindex off;
                }
    }

server {
       listen       80;
       server_name imgtest.simonzhang.net ;
       location / {
                #proxy cache stat
                proxy_redirect          off;
                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_connect_timeout   15;
                proxy_send_timeout      45;
                proxy_read_timeout      45;
                proxy_buffer_size       128k;
                proxy_buffers           4 128k;
                proxy_busy_buffers_size 128k;
                proxy_temp_file_write_size 128k;
                proxy_cache cache_one;
                # 成功浏览过的图片5天过期
                proxy_cache_valid 200 5d;
                proxy_cache_valid 404 304 1m;
                proxy_cache_key $host$uri$is_args$args;
                #add_header X-Cache $upstream_cache_status;
                proxy_pass http://local_img;
                #proxy cache end
                }
    }

  重启nginx服务后查看日志,日志中已经有了HIT的日志,日志显示使用处理耗时0.000ms。收工。

4月 17

python 使用资源不断收集中

安装使用:
easy_install安装升级工具,根据自己需要版本获取。有了这个很多东西都不需要了。
http://pypi.python.org/pypi/setuptools#downloads
wget -q http://peak.telecommunity.com/dist/ez_setup.py

开发环境搭建:
eclipse 环境集成安装
http://pydev.org/updates

开发框架:
py2exe
http://starship.python.net/crew/theller/py2e

windows下的摄像头获取
http://videocapture.sourceforge.net/

python转C并编译
http://cython.org/

学习:
watchdog
watchdog 用来监控文件系统事件的Python API和shell实用工具。

pattern
Pattern Web数据挖掘模块。可用于数据挖掘、自然语言处理、机器学习和网络分析

django-sentry
实时Django的异常记录,Django的异常记录到数据库处理程序。

excel操作
http://www.python-excel.org/

9月 26

centos安装node.js简单测试

操作系统

Centos 5.4 Linux 2.6.18-164.el5 4G内存、Xeon(TM) CPU 3.20GHz

node-v0.5.7

node的官方网站http://nodejs.org/ 使用官方网站示例。

再安装前请将开发工具(tcl-developenssl-develglibc-devel bzip2*等)安装,可以使用yum工具。

安装

# tar zxvf node-v0.5.7.tar.gz

# cd node-v0.5.7

# ./configure

# make

有报make: [all] Error 1 (ignored) 但是没有找到问题,忽略了直接做下一步。

# make install

收到安装成功讯息。

建立一个node.js档案。

vim node.js

var http = require(‘http’);

http.createServer(function (req, res) {

res.writeHead(200, {‘Content-Type’: ‘text/plain’});

res.end(‘Hello Node.jsn‘);

}).listen(8124, “192.168.1.200”);

console.log(‘Server running at http://192.168.1.200:8124/’);

执行node.js

# node node.js

打开浏览器使用连接http:// 192.168.1.200:8124看到Hello Node.js,就表示node.js已经正在运作中。

进行个简单测试,使用apacheab工具

命令# ./ab -c10000 -n 40000 http:// 192.168.1.200:8124/

Complete requests: 40000

并发数: 3498.75 [#/sec]

平均返回相应时间: 0.286 [ms]

传输量: 266.51 [Kbytes/sec]

无请求失败。

在同一台机器上使用django写个,测试一下。

环境Python 2.6.6 django 1.3.0 关闭debug状态

代码为:

#!/bin/evn python

from django.http import HttpResponse

def index(request):

html = “Hello Djangon”

return HttpResponse(html)

直接使用python自带的调试服务器

# python manage.py runserver 192.168.1.200:9000

因为4000030000时将服务器跑死了(报错apr_socket_recv: Connection timed out (110)),所以修改为25000进行测试,结果也死了,改20000也死了,直接改15000也死了。算了我不测了。

总结:初步看node js效率确实很高,python+django自带的服务器的效

4月 20

nginx连接python+django,测试uWSGI和fastcgi性能

【编写测试:张子萌 2011-4-19

当前状况是nginx0.9pcrepython2.6Mysql-pythonDjango已经安装完毕。

nginx已经安装在/program/nginx目录中。项目位置:/program/www/mysite

首先验证uWSGI

1、开始安装uWSGIuwsgi地址。

http://projects.unbit.it/uwsgi/

下载

# wget http://projects.unbit.it/downloads/uwsgi-0.9.7.2.tar.gz

# tar zxvf uwsgi-0.9.7.2.tar.gz

# cd uwsgi-0.9.7.2

# make

# cp uwsgi /usr/bin

2Nginx配置,只列出主要部分部分。nginx默认已经支持uWSGIfastcgi

server部分如下

server {

listen 80;

server_name localhost 172.27.224.235;

location / {

uwsgi_pass 127.0.0.1:9000;

include uwsgi_params;

access_log off;

}

}

3UWSGI配置,参数部分按照实际修改。

# mkdir -p /program/uwsgi

# mkdir -p /program/mysite/logs

# cd /program/uwsgi

# vi uwsgi.xml

内容如下:

127.0.0.1:9000 #设定监听ip和端口

4 #worker模式支持多线程和多进程混合模型的MPM

#如果对workprefork工作模式不清楚自行查资料

4 #使用进程数

100 #监听满后排队数量,超过排队数量服务将会拒绝连接

true #主线程enable

/program/uwsgi/uwsgi.pid #产生主进程号的位置

/program/mysite #项目的目录位置

django_uwsgi #模块信息,下一步中会编辑这个文件

true

true #打印内存请求日志信息

true #开启多线程

true #
每行都用string格式打印时间

512 #uwsgi需要使用内存的量,如当前配置为512M

/program/mysite/logs/django.log #此进程打印日志的位置

4、创建应用模块

# cd /program/mysite

# vi django_uwsgi.py

import os,sys

import django.core.handlers.wsgi

os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘testsite.settings’

path = ‘/program ###此处是防止找不到模块

if path not in sys.path:

sys.path.append(path)

print sys.path

application = django.core.handlers.wsgi.WSGIHandler()

5、启动服务

#/usr/bin/uwsgi -x /program/uwsgi/uwsgi.xml

启动nginx

6、查看进程

#[liuts@webserver nginx]# ps -ef|grep uwsgi|grep -v grep

root 25867 1 0 19:41 ? 00:00:00 /usr/bin/uwsgi -x /program/uwsgi/uwsgi.xml

root 25868 25867 0 19:41 ? 00:00:00 /usr/bin/uwsgi -x /program/uwsgi/uwsgi.xml

root 25869 25867 0 19:41 ? 00:00:00 /usr/bin/uwsgi -x /program/uwsgi/uwsgi.xml

root 25870 25867 0 19:41 ? 00:00:00 /usr/bin/uwsgi -x /program/uwsgi/uwsgi.xml

root 25871 25867 0 19:41 ? 00:00:00 /usr/bin/uwsgi -x /program/uwsgi/uwsgi.xml

7、查看监听端口

#[liuts@webserver nginx]# netstat -an|grep 9000

Tcp 0 127.0.0.1:9000 0.0.0.0:* LISTEN

8、访问测试

输入http://yourserverip/

注:附件中有启停uwsgi脚本,方便启停使用。

再次验证fastcgi

使用fastcgi需要安装flupflup一个用python写的web server

1、安装flup

下载地址

# wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz

#tar zxvf flup-1.0.2.tar.gz

#cd flup-1.0.2

#python setup.py install

2、修改nginx配置

http部分,参数自己优化

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

location部分

fastcgi_pass 127.0.0.1:8000;

include fastcgi_params;

3、启动fastcgi

# /p

4、查看端口进程

#[liuts@webserver nginx]# netstat -an|grep 8000

Tcp 0 127.0.0.1:8000 0.0.0.0:* LISTEN

5、访问测试

输入http://yourserverip/

两个模块调试正常做个测试看看。

我用的是笔记本的虚拟机做的,虚拟机参数如下:

Cpu参数

processor : 0

vendor_id : GenuineIntel

cpu family : 6

model : 15

model name : Intel(R) Core(TM)2 Duo CPU T5870 @ 2.00GHz

内存参数:

MemTotal: 1034708 kB

Cached: 318512 kB

测试工具使用apache自带的ab压力测试工具。使用并发1000,连续60秒进行测试。

测试页面脚本如下,服务接到请求后首先获取系统时间,再做一个610的随机数累加,然后将信息返回给客户。

from django.http import HttpResponse

import datetime

import random

import MySQLdb

def current_datetime(request):

now = datetime.datetime.now()

html = “now %s. n” % now

########

r=random.randint(6,10)

html2=0

for i in range(r):

html2 = html2 + i

html=html+”==”+str(html2)+”==”

########

return HttpResponse(html)

uWSGI测试结果

测试时长: 60.312 seconds

Complete requests: 42055

并发数: 697.29 [#/sec]

平均返回相应时间: 1.434 [ms]

传输量: 135.18 [Kbytes/sec]

Cpu系统使用量平均:84%

Cpu用户使用量平均:16%

内存使用量:512M

Fastcgi测试结果

测试时长: 60.004 seconds

Complete requests: 32270

并发数: 537.80 [#/sec]

平均返回相应时间: 1.859 [ms]

传输量: 385.16 [Kbytes/sec]

Cpu系统使用量平均:51%

Cpu用户使用量平均:49%

内存使用量:500M

结论:

各项测试uWSGI明显优于Fastcgi。具体参数优化需要根据业务编写,然后再做测试。