Chinaunix首页 | 论坛 | 博客
  • 博客访问: 935509
  • 博文数量: 245
  • 博客积分: 11429
  • 博客等级: 上将
  • 技术积分: 2662
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-15 00:16
文章存档

2011年(56)

2010年(174)

2009年(15)

分类: 系统运维

2009-08-18 02:29:00

                           Nginx  安装详解 

作者:feiyang

 qq:979835161

  msn:

 Nginx具有很高的稳定性。其它HTTP服务器,当遇到访问的峰值,或者有人恶意发起慢速连接时, 

也很可能会导致服务器物理内存耗尽频繁交换,失去响应,只能重启服务器。例如当前apache一旦上到 

200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得它的CPU与 

内存占用率非常低。nginx官方表示保持10,000个没有活动的连接,它只占2.5M内存,所以类似DOS 这 

样的攻击对nginx来说基本上是毫无用处的。就稳定性而言, nginxlighthttpd更胜一筹。  

     Nginx支持热部署。它的启动特别容易并且几乎可以做到7*24不间断运行,即使运行数个月也不 

需要重新启动。你还能够在不间断服务的情况下,对软件版本进行进行升级。  

      Nginx采用master-slave模型能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻 

塞延迟。当采用select()/poll()调用时,还可以限制每个进程的连接数。  

       Nginx代码质量非常高,代码很规范, 手法成熟, 模块扩展也很容易。特别值得一提的是强大 

Upstream与 Filter链。 Upstream为诸如 reverse proxy, 与其他服务器通信模块的编写奠定了很好的 

基础。而Filter链最酷的部分就是各个filter不必等待前一个filter执行完毕。它可以把前一个filter 

的输出做为当前filter的输入,这有点像Unix的管线。这意味着,一个模块可以开始压缩从后端服务器 

发送过来的请求,且可以在模块接收完后端服务器的整个请求之前把压缩流转向客户端。  

       Nginx采用了一些os提供的最新特性如对sendfile (Linux 2.2+)accept-filter (FreeBSD  

4.1+)TCP_DEFER_ACCEPT (Linux 2.4+) 的支持,从而大大提高了性能  

二、 Nginx  安装及调试 

1Pcre  安装 

./configure 

 make && make install 

 cd ../ 

nginx  编译安装 : 

   下载:

/configure   --user=www   --group=www   --prefix=/usr/local/nginx/   --with-http_stub_status_module 

--with-openssl=/usr/local/openssl 

make && make install 

3Nginx  配置文件测试:   

# /usr/local/nginx/sbin/nginx -t //Debug 配置文件的关键命令需要重点撑握

2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf 

syntax is ok 

2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf was 

tested successfully 

3Nginx   启动:   

# /usr/local/nginx/sbin/nginx 

4Nginx   配置文件修改重新加载:   

# kill -HUP `cat /usr/local/nginx/logs/nginx.pid 

三、 Nginx Rewrite 

1. Nginx Rewrite 基本标记(flags) 

last -  基本上都用这个Flag 。 

      ※相当于Apache 里的[L]标记,表示完成rewrite,不再匹配后面的规则 

break - 中止Rewirte,不再继续匹配 

redirect - 返回临时重定向的HTTP 状态 302 

permanent - 返回永久重定向的HTTP 状态 301 

      ※原有的url 支持正则 重写的url 不支持正则 

2.  正则表达式匹配,其中: 

   * ~        为区分大小写匹配 

   * ~*       为不区分大小写匹配 

   * !~!~*   分别为区分大小写不匹配及不区分大小写不匹配 

3.  文件及目录匹配,其中: 

   * -f !-f 用来判断是否存在文件 

   * -d !-d 用来判断是否存在目录 

   * -e !-e 用来判断是否存在文件或目录 

   * -x !-x 用来判断文件是否可执行 

修改nginx.confi  文件

user  nobody nobody;#运行用户

worker_processes  2;启动进程

error_log  logs/error.log notice;#全局错误日志及PID文件

pid        logs/nginx.pid;

events {#工作模式及连接数上限

        use epoll;

        worker_connections      1024;

}

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

        #?露篓mime??

        include      /usr/local/nginx/conf/mime.types;

        default_type  application/octet-stream;

 #设定日志格式

  log_format main         '$remote_addr - $remote_user [$time_local] '

                                                '"$request" $status $bytes_sent '

                                                '"$http_referer" "$http_user_agent" '

                                                '"$gzip_ratio"';

        log_format download '$remote_addr - $remote_user [$time_local] '

                                                '"$request" $status $bytes_sent '

                                                '"$http_referer" "$http_user_agent" '

                                                '"$http_range" "$sent_http_content_range"';

#设定请求缓冲

     client_header_buffer_size    1k;

        large_client_header_buffers  4 4k;

 #开启gzip模块

      gzip on;

        gzip_min_length  1100;

        gzip_buffers     4 8k;

        gzip_types       text/plain;

        output_buffers   1 32k;

        postpone_output  1460;

#access log

        access_log  /usr/local/nginx/logs/access.log;

        client_header_timeout  3m;

        client_body_timeout    3m;

#access log

        access_log  /usr/local/nginx/logs/access.log;

 

        client_header_timeout  3m;

        client_body_timeout    3m;

        send_timeout           3m;

        sendfile                on;

        tcp_nopush              on;

        tcp_nodelay             on;

        keepalive_timeout  65;

  #设定负载均衡的服务器列表

     upstream ngix {

 #weigth参数表示权值,权值越高被分配到的几率越大

                #本机上的Squid开启3128端口

      server 192.168.3.70:80 weight=5;

                server 192.168.3.69:80   weight=1;

                server 192.168.3.68:80   weight=3;

        }

  #设定虚拟主机

   server {

                listen          80;

                server_name     192.168.3.70 

                charset gb2312;

                #?露篓卤戮???禄煤??

           #设定本虚拟主机的访问日志

    

  access_log  logs/

                #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid

                #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果

                location ~ ^/(img|js|css|GIF|jpeg|jpg|JPEG)/  {

                        root    /usr/local/nginx/html/;

                        expires 24h;

                }

 #对 "/" 启用负载均衡

         location / {

                        proxy_pass      

                       

                        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;

                        client_max_body_size    10m;

                        client_body_buffer_size 128k;

                        proxy_connect_timeout   90;

                        proxy_send_timeout      90;

                        proxy_read_timeout      90;

                        proxy_buffer_size       4k;

                        proxy_buffers           4 32k;

                        proxy_busy_buffers_size 64k;

                        proxy_temp_file_write_size 64k;

                }

               #设定查看Nginx状态的地址

                location /NginxStatus {

                        stub_status             on;

                        access_log              on;

                        auth_basic              "NginxStatus";

                        auth_basic_user_file  conf/htpasswd;

                }

        }

}

运行以下命令检测配置文件是否无误:

  如果没有报错,那么就可以开始运行Nginx了,执行以下命令即可:

  备注:conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可,内容大致如下:

Htasswd -c /usr/local/nginx/conf/nginx.users nginx

  3.) 查看 Nginx 运行状态

  输入地址 3.70/NginxStatus/,输入验证帐号密码,即可看到类似如下内容:

Active connections: 328

server accepts handled requests

9309 8982 28890

Reading: 1 Writing: 3 Waiting: 324

当出页面出现

404 Not Found

nginx/0.7.16

可以用[root@ipkzhu66 html]# curl -I 3.70/

HTTP/1.1 200 OK

Server: nginx/0.7.16

Date: Mon, 17 Aug 2009 21:16:24 GMT

Content-Type: text/html

Content-Length: 151

Last-Modified: Mon, 17 Aug 2009 19:39:38 GMT

Connection: keep-alive

Accept-Ranges: bytes

第一行表示目前活跃的连接数

第三行的第三个数字表示Nginx运行到当前时间接受到的总请求数,如果快达到了上限,就需要加大上限值了。

Nginx cache 的配置 可以将文件缓存到本地 则需要增加如下几个字参数

Proxy_store on

Proxy_store_access user:rw group:rw all:rw;

Proxy_temp_path 缓存目录;

经过上一步配置之后,虽然文件被缓存到了本地磁盘上,但每次请求仍会想远端拉取文件

,为了避免去远端拉去文件 必须修改proxy_pass

If(!-e $request_filename) {

Proxy_pass 

}

这个条件是当请求的文件在本地的proxy_temp_path 制定的目录下不存在是,在想后端拉去。更多更高级的应用可以研究ncache 官方网站:

Nginx 简单优化:

  1.减小nginx编译后的文件大小

   默认的nginx编译选项里居然使用debug模式(-g)的(debug模式会插入很多跟踪和ASSERT之类)编译以后一个nginx有好几兆,去掉nginx的的不够模式编译,编译以后只有几百k

auto/cc/gcc 最后几行又:

#debug

CFLAGS="$CFLAGS -g

注释掉这几行,重新编译

修改nginxheader伪装服务器

   1)修改nginx.h

      # vi 

       #Define NGINX_VERSION "1.8"

        #Define NGINX_VER LTWS/" NGINX_VERSION

   #define  NGINX_VAR  "NGINX"

   #define NGINX_OLDPID_EXT ".oldbin"

修改nginx_http_header_filter_module

 Static char ngx_http_server_srting[]="Server: nginx"CRLF;

改为

Static char ngx_http_server_string[]="Server:LTWS" CRLF;

修改 nginx_http_header_filter_module

Static u_char ngx_http_error_full_tail[]="


"NGINX_VER:
" CRLF

" " CRLF

"" CRLF

;

static u_char ngx_http_error_full_tail[] =

"


" NGINX_VER "
" CRLF

"" CRLF

"" CRLF

;

static u_char ngx_http_error_tail[] =

"


nginx
" CRLF

"" CRLF

"" CRLF

改为:

static u_char ngx_http_error_full_tail[] =

"


" NGINX_VER "
" CRLF

"


" CRLF 添加一行

"" CRLF

"" CRLF

;

static u_char ngx_http_error_tail[] =

"


LTWS
" CRLF

"" CRLF

4.tcmalloc 优化nginx 性能

下载: 

    

     

tar zxvf libunwind-0.99-alpha.tar.gz
cd libunwind-0.99-alpha/
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install

下载google-perftools-0.99.2.tar.gz

tar zxvf google-perftools-0.99.2.tar.gz
cd google-perftools-0.99.2/
./configure
make && make install

echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig

编译nginx 加载google_perftools_module

./configure --with-google_perftools_module

在主配置文件中加入nginxConf

Google_perftools_profiles /path/to/profile;

5.内核参数优化

在末尾增加以下内容:

       net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_syncllkies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 5000 65000

阅读(2663) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~