您的位置:首页 > 运维架构 > Nginx

nginx和uwsgi安装配置

2013-05-20 10:00 323 查看

一、安装nginx

下载nginx源码包,官网是http://wiki.nginx.org/Main ,为了支持gzip以及正则表达式,还需要下载pcre和zlib源码包。pcre网址http://www.pcre.org/,zlib网址http://www.zlib.net/
我下载的nginx版本为1.4.1, pcre版本为8.32,zlib版本为1.2.8。

下载完成后,对三个源码包分别解压。进入nginx目录配置。(注:openssl如果系统没有请安装先)
./configure --prefix=$HOME/nginx --with-openssl=/usr/include --with-pcre=../pcre-8.32 --with-http_stub_status_module  --with-zlib=../zlib-1.2.8
关于nginx的配置选项含义,可以参照官方文档。这里主要是配置了gzip压缩,正则表达式以及启用ssl。--with-http_stub_status_module是安装可以查看nginx状态的模块。

然后
make && make install

安装完成后,就可以设定配置文件了。我的配置文件比较简单,设定如下:
#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 {
use epoll;
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;  #开启gzip压缩
#gzip_min_length 1k;  #最小的压缩长度,小于该长度不压缩
gzip_buffers 4 16k; #gzip压缩的缓存大小,配置为16k的4倍
gzip_http_version 1.1;
gzip_comp_level 2;   #压缩比例
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

server {
listen       8000;  #监听端口
server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
root   html;
index  index.html index.htm;
}

  location /py {
include uwsgi_params;
uwsgi_pass localhost:8081;
}

#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;
}
}
}


配置完成后,就可以运行$NGINX_PATH/sbin/nginx启动nginx了。注意,由于该程序启动需要root权限,因此可用sudo命令,或者将nginx程序的所有者改为root,然后设定s标记。
sudo chown root:root nginx
sudo chmod +s nginx
关闭命令:nginx -s stop
重载配置文件命令:nginx -s reload

上面配置文件中加粗的部分是我配置的uwsgi部分,即针对/py的路径,全部由在localhost:8081处监听的uwsgi程序处理。uwsgi的安装在第二部分说明。

gzip详细参数可参见:http://blog.csdn.net/jessonlv/article/details/8016284。查看服务器是否开启gzip,可使用curl工具。
curl -I -H "Accept-Encoding: gzip, deflate" "http://localhost"
如果gzip开启,则服务器的响应应该类似于下面这样:
HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Mon, 20 May 2013 01:49:47 GMT
Content-Type: text/html
Last-Modified: Thu, 19 Jul 2012 03:16:40 GMT
Connection: keep-alive
Content-Encoding: gzip


二、uwsgi安装

下载uwsgi的源码包,http://uwsgi-docs.readthedocs.org/en/latest/。 
编译并安装
make && make install
在nginx中配置uwsgi,在第一部分安装nginx的配置文件中加粗的部分即是配置的部分。然后使用下面的命令启动即可。
uwsgi --socket localhost:8081  --wsgi-file wsgi.py --master --processes 4 --threads 2 --stats localhost:9191
其中wsgi.py是我自己的python程序,如下所示:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
现在使用localhost:8080/py就可以访问该程序并输出Hello World。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: