/ 中存储网

Nginx服务器的安装配置

2013-11-21 14:34:01 来源:IT技术网
  Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。

  Nginx 是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,它已经在该站点运行超过两年半了。

  Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

  在高连接并发的情况下,Nginx是Apache服务器不错的替代品: 能够支持高达50,000个并发连接数的响应, Nginx为我们选择了epoll and kqueue作为开发模型.   下载Nginx http://nginx.net/

  Nginx 的中文维基 http://wiki.nginx.org/NginxChs

  由于Nginx的配置文件中用到正则,所以需要pcre模块的支持

  下载直址:http://www.pcre.org/ 解压再cp到和nginx目录内

注:最好编译安装,把安装好的目录给nginx. #./configure --prefix=/usr/local/pcre;make;make install

  Nginx的编译参数如下:

  cd nginx-0.8.7/

  ./configure --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_memcached_module (启用memcache缓存) --with-http_rewrite_module (启用支持url重写) --with-pcre=/usr/local/pcre 如下模块可去除

注:--without-http_fastcgi_module --without-http_memcached_module --without-http_limit_zone_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module

其中参数 --with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。

  make && make install   Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。

  启动Nginx: # sbin/nginx 即可

  Nginx 的参数包括有如下几个:

-c :使用指定的配置文件而不是 conf 目录下的 nginx.conf 。

-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。

-v:显示 nginx 版本号。

-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。     sbin/nginx –t –c conf/nginx2.conf   Nginx 支持下表中的信号:

TERM, INT     快速关闭程序,中止当前正在处理的请求

QUIT     处理完当前请求后,关闭程序

HUP     重新加载配置,并开启新的工作进程,关闭旧的进程,此操作不会中断请求

USR1     重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件

USR2     平滑升级可执行程序

WINCH     从容关闭工作进程   有两种方式来通过这些信号去控制 Nginx

第一是通过 logs 目录下的nginx.pid 查看当前运行的 Nginx 的进程 ID,通过 kill –XXX 来控制 Nginx。

第二如果系统中只有一个Nginx进程,那也可以通过 killall 命令来完成,例如运行 killall –s HUP nginx 来让Nginx 重新加载配置。   配置文件实例讲解 nginx.conf

user  nobody; # 工作进程的属主

worker_processes  4; # 工作进程数,一般与 CPU 核数等同

#error_log  logs/error.log;

error_log  logs/error.log  notice; 全局错误日志

#error_log  logs/error.log  info;

pid        logs/nginx.pid;

worker_rlimit_nofile 65535; 配置要和系统的单进程打开文件数一致     events { # 工作模式及连接数

use epoll; # Linux 下性能最好的 event 模式

worker_connections  2048; # 每个工作进程允许最大的同时连接数

# MaxClient = worker_processes * worker_connections

# 因此这两个数的乘积若大于系统最大可用tcp/ip栈数是没有意义的。   

http { # 设定http服务器,利用它的反向代理功能提供负载均衡支持

include       mime.types; #设定mime类型

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; # 日志文件名        # 设定请求缓冲

client_header_buffer_size 1k;

large_client_header_buffers 4 4k;        client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

sendfile        on;

# 指定nginx是否调用sendfile函数(zero copy方式)来输出文件,普通应用必须设为on.

#tcp_nopush     on;

tcp_nodelay     on;

keepalive_timeout  65;

#gzip  on; 开启gzip模块,需configure时指定。

#include      gzip.conf;        server {

listen       80;  #HTTP 的端口

server_name  www.xhy.com;

charset utf-8;

access_log  logs/host.access.log  main;               location ~ ^/NginxStatus/ {

stub_status on; #Nginx 状态监控配置

access_log off;

auth_basic    "NginxStatus";

auth_basic_user_file  ./htpasswd; #实验中此处的相对路径为conf ?        location ~ ^/(WEB-INF)/ {

deny all;               location ~ .(htm|html|asp|jar|wma)$ {

#如果访问 .htm .html .asp等资源,则直接取本地文件,不用转发

root /opt/webapp;

expires 24h;

注:expires指令来控制其在浏览器的缓存,以减少不必要的请求。

expires指令可以控制http应答中的Expires和Cache-Control的头标.

亦可写为:expires 1 January, 1999, 00:00:01 GMT; 或 10s 30m 1d max off location ~ ^/(img|js|css)/ {

#如果访问 /img/*, /js/*, /css/*资源,直接取本地文件,不转发

root  /data3/html;

expires 30m; location / {

root   html;

index  index.html index.htm;   

error_page 404 /html/404.html;

# redirect server error pages to the static page /50x.html

error_page 502 503 /html/502.html;

error_page 500 504 /50x.html;

location = /50x.html {

root   html;        }

server { #默认虚拟主机(空主机头),指用户通过ip访问或未在server_name设置的域名访问

listen 80 default;

return 500;

#rewrite ^(.*) http://www.baidu.com permanent; #如下四种flag标记

#last – 相当于Apache中的L

#break – 中止Rewirte,不在继续匹配

#redirect – 返回临时重定向的HTTP状态302,相当于Apache中的R

#permanent – 返回永久重定向的HTTP状态301,相当于Apache中的R=301     }   Nginx监控

必须生成一个htpasswd文件,用Apache的htpasswd工具生成.

#htpasswd -c /usr/local/nginx/conf/htpasswd admin

监控页面http://localhost/NginxStatus 显示如下:

Active connections: 10  # 当前Nginx正处理的活动连接数

server accepts handled requests

15 15 20

Reading: 0 Writing: 3 Waiting: 67

共处理15个连接,成功创建15次握手,共处理了20个请求

reading 读取到客户端的header信息数

writing 返回给客户端的header信息数

waiting 开启keep-alive的情况下 active-(reading+writing) 即为已经处理完正在等候下一次请求指令的驻留连接   动态请求处理 以jsp为例

通常先定义由Nginx直接处理的一些静态文件请求,其他所有的请求如jsp通过proxy_pass指令传送给后端的服务器如tomcat       location / {

proxy_pass [url]http://localhost:8080;[/url]

注:此处亦可通过upstream来定义一个服务器的集群,实现负载均衡。

proxy_set_header X-Real-IP $remote_addr;     根据URL分发

Redirect/Rewrite功能

Nginx目录自动加斜线     expires控制     模块应用