`
tcspecial
  • 浏览: 897367 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

nginx 使用

阅读更多

       nginx 以小巧,并发能力强而著称,其运行速度非常快,据说达到10倍以上, 国内京东,新浪等大型网站都以此作为web服务器

 

一. 安装

     wget http://nginx.org/download/nginx-1.9.2.tar.gz

     编译 nginx ,V1.9.0版本支持TCP负载均衡,需在编译时开启TCP负载选项。

    

./configure --with-stream

   编译过程中,容易报库缺失错误:

   ./configure: error: the HTTP rewrite module requires the PCRE library.

   

yum -y install pcre-devel

 

   ./configure: error: the HTTP gzip module requires the zlib library

 

yum install -y zlib-devel

 

 

二. 测试

     启动 nginx:  ./usr/local/nginx/sbin/nginx

     停止 nginx:  ./nginx -s stop 

     关闭 nginx:  ./nginx -s quit 

     重新加载配置:./nginx -s reload 

     http://localhost   显示nginx的欢迎页

     

三. 常用配置

     3.1 当没有索引页时,显示文件和目录列表

     /usr/local/nginx/conf/nginx.conf 

worker_processes 1;   #服务进程数量,一般等于CPU数量 

events {
    useepoll;            		#linux上使用epoll网络模型,可不配
    worker_connections 1024;    #一个worker_processe允许的最近并发连接数量
}

location / {
	root   html;
	index  index.html index.htm;
	
	autoindex on;     			# 显示文件列表
	autoindex_exact_size on;
	autoindex_localtime on; 
}

#配置/ios目录下后缀.plist结尾文件以文本方式显示,避免下载
location ~ .*/ios/.*\.plist$ {                 
    add_header Content-Type text/plain;
}

 

  

   3.2 配置虚拟目录

    nginx 好像没有虚拟目录这个概念,但是可以通过指定请求路径时的访问路径实现, 具体通过 alias 和 root 这两个参数设置,这两个参数非常容易混淆,导致出现404错误

    现在要访问: /home/scada/www/test 下 hello.html 这个文件, 如下配置:

# alias 实现
location ^~/test/ {
    alias /home/scada/www/test/;
}

# root 实现
location ^~/test/ {
    root /home/scada/www; 
}

    浏览器访问: http://localhost/test/hello.html 

 

    3.3 配置负载均衡

    Tomcat 可使用Apache mod_jk  组件方式实现负载均衡,但是配置起来比较麻烦。 现在可以直接使用nginx实现负载均衡,只添加少许配置即可实现。

    现有 192.168.5.154  192.168.5.155  192.168.5.158  三台服务器,以192.168.5.154 作为 nginx 前端主服务器,192.168.5.155,192.168.5.158 作为Tomcat服务器。

    

http {
	#负载均衡服务器列表,可设置不同的权重
	upstream localhost {
		server 192.168.5.155:8080 weight=5;		
		server 192.168.5.158:8080 weight=5;
	}
	
	server {
		location / {
			proxy_pass http://localhost;     #反向代理
		}
	}
}

  

    访问:http://192.168.5.154 ,发现显示Tomcat欢迎页,多次刷新,显示不同的欢迎页面

 

 

四. 运行 cgi 服务

   nginx 是不支持直接运行 cgi 程序的, 但支持 fastcgi, 当 ngix 接收到请求后, 通过TCP/unix domain方式与fastcgi进程管理器通信。

 

   a. 编写 fastcgi 方式的python服务

   WSGI 是 python应用程序或框架与web服务器之间的一种接口, 具体实现有 tornado,flup 等, 这里使用 flup

   下载flup: https://pypi.python.org/pypi/flup/1.0.2#downloads

   安装flup:  python setup.py install

  

#!/usr/bin/python
#encoding: utf-8

from flup.server.fcgi import WSGIServer


def myapp(envrion,start_response):
	start_response("200 OK",[("Content-Type","text/plain")])
	return ["Hello world\n"]


if __name__=='__main__':
	WSGIServer(myapp,bindAddress=("127.0.0.1",8008)).run()     # 监听8008端口

 

   b. 启动fastcgi服务: sudo ./hello.py   --method=prefork/threaded minspare=50 maxspare=50 maxchildren=1000

   c. 配置nginx,支持fastcgi

   

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

	fastcgi_pass 127.0.0.1:8008;       #指定8008,与wsgi服务保持一致
	fastcgi_param SCRIPT_FILENAME "";
	fastcgi_param PATH_INFO $fastcgi_script_name;
	include fastcgi.conf;              # 导入fastcgi配置
}

    

    浏览器访问: http://localhost/hello.py  显示hello world   

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics