于吉吉的技术博客

建造高性能门户网

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  65 随笔 :: 6 文章 :: 149 评论 :: 0 Trackbacks
nginx的官方网站是:http://www.nginx.org

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

nginx的特性
1.目前官方 Nginx 并不支持Windows,您只能在包括Linux,UNIX,BSD系统下安装和使用
2.Nginx 本身只是一个HTTP和反向代理服务器,它无法像Apache一样通过安装各种模块来支持不同的页面脚本,例如PHP、CGI等
3.Nginx 支持简单的负载均衡和容错
4.支持作为基本 HTTP 服务器的功能,例如日志、压缩、Byte ranges、Chunked responses、SSL、虚拟主机等等,应有尽有

安装
安装nginx前需要确保系统中已经安装PCRE包,PCRE library这个是HTTP Rewrite模块,也即是url静态化的包,到http://www.pcre.org下载最新的PCRE源码包,

或直接使用yum install pcre,apt-get instll pcre进行安装

登录 http://www.nginx.org/en/download.html 下载最新的版本

wget http://www.nginx.org/download/nginx-0.8.53.tar.gz

tar zxvf nginx-0.8.53.tar.gz

cd nginx-0.8.53

./configure --prefix=/usr/local/nginx --with-pcre=/home/download/pcre-8.01  --with-http_ssl_module --with-openssl=/home/download/openssl-1.0.0a --with-http_stub_status_module 

##指定pcre目录和启动ssl,https模块

make

make install

##为什么Nginx的性能要比Apache高得多?这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的 Squid、Memcached都采用的是epoll网络I/O模型,处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效

配置
编辑vi /usr/local/nginx/conf/nginx.conf,其中/usr/local/nginx为安装路径

##指定nginx的用户名和用户组
user  nobody;

##启动进程数
worker_processes  8;
worker_rlimit_nofile 
10240;

##指定PID文件
pid        logs/nginx.pid;

##指定工作模式和链接上限
events {
    
use epoll;
    worker_connections  
10240;
}

##http服务器
http {
include       mime.types;
default_type  text
/html;

##指定日志格式
log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] $request '
                  
'"$status" $body_bytes_sent "$http_referer" '
                  
'"$http_user_agent" "$http_x_forwarded_for"';

##指定accesslog
access_log logs/nginx.log main;

##指定超时
keepalive_timeout  300;

##开启gzip模块
gzip  on;
gzip_min_length  
1000;
gzip_buffers     
4 8k;
gzip_types       text
/*;
gzip_http_version 
1.0;
output_buffers   
1 32k;
postpone_output  
1460;
gzip_proxied     any;
gzip_vary        on;

##指定请求的缓冲
client_header_timeout   5m;
client_body_timeout     5m;
send_timeout            5m;
sendfile                on;
tcp_nopush              on;
tcp_nodelay             off;
client_header_buffer_size 16k;
large_client_header_buffers 
4 64k;
server_names_hash_bucket_size 
128;
ssi on;
ssi_silent_errors on;
ssi_types text
/shtml;

##指定虚拟主机
server {
listen      
80;
server_name _;
server_name_in_redirect  off;

location 
/ {
 root 
/dev/null;
}

}

##指定include文件
include servers/*.com;

}


新建proxy.conf

proxy_set_header X-Forwarded-For $remote_addr;    ##设定header
proxy_set_header RealIP $remote_addr;
proxy_set_header Accept-Encoding ' ';
proxy_hide_header Vary;    ##隐藏header
add_header via_up $upstream_addr;
proxy_connect_timeout   2m;    ##代理连接超时
proxy_send_timeout      2m;    ##代理发送超时
proxy_read_timeout      2m;    ##代理发送超时
proxy_temp_file_write_size 2048m;##设定缓存文件夹大小
proxy_buffer_size               256k; 
proxy_buffers                   4 256k; 
proxy_busy_buffers_size 512k; 

proxy_ignore_client_abort off
;

proxy_next_upstream error timeout invalid_header
;

新建目录/conf/servers,并新建配置文件test.com

server {
        listen   
80;
        server_name  245.test.com;
        root /home/htmlfile/test;

        location 
= / {
            proxy_temp_path /var/www/cache
;
            index index.html index.htm;
        }


}

修改配置文件后,通过以下命令检查配置是否正确

/usr/local/nginx/sbin/nginx -t 

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动命令
/usr/local/nginx/sbin/nginx 
停止命令
/usr/local/nginx/sbin/nginx  -s stop
平滑重新加载配置文件
kill -HUP `cat /usr/local/nginx/logs/nginx.pid
添加到自启动
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

posted on 2010-11-30 12:45 陈于喆 阅读(2601) 评论(0)  编辑  收藏 所属分类: linux安装配置

只有注册用户登录后才能发表评论。


网站导航: