`

httpServer / proxyServer / nginx 1.7.9

阅读更多

 

http://photofile.su****.com/NginxStatus

Active connections: 4 
server accepts handled requests
 18447518 18447518 33307316 
Reading: 1 Writing: 1 Waiting: 2 

Windows Nginx  启停操作 / 起停操作

my_nginx.bat批处理启停

%~d0
cd "%~dp0"
start nginx.exe

手动启动Windows Nginx Server

D:\soft\nginx-1.0.3>nginx.exe

D:\soft\nginx-1.0.3>nginx

手动停止Windows Nginx Server
D:\soft\nginx-1.0.3>nginx.exe -s stop

D:\soft\nginx-1.0.3>nginx -s stop

 

重载配置文件运行Windows Nginx Server
D:\soft\nginx-1.0.3>nginx.exe -s reload

强制停止Windows Nginx Server

 

D:\soft\nginx-1.0.3>tasklist /fi "imagename eq nginx.exe"
映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
nginx.exe                     1000 RDP-Tcp#1                  1      4,488 K
nginx.exe                      716 RDP-Tcp#1                  1      4,828 K

D:\soft\nginx-1.0.3> taskkill /F /IM nginx.exe > nul

D:\soft\nginx-1.0.3>tasklist /fi "imagename eq nginx.exe"
信息: 没有运行的任务匹配指定标准。

D:\soft\nginx-1.0.3>netstat -ano | grep 80
  TCP    0.0.0.0:80             0.0.0.0:0              LISTENING       716(次进程)
D:\soft\nginx-1.0.3>netstat -ant | grep 80
  TCP    0.0.0.0:80             0.0.0.0:0              LISTENING       主栈中

D:\soft\nginx-1.0.3>tasklist /fi "imagename eq nginx.exe"
映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
nginx.exe                     1000 RDP-Tcp#1                  1      4,488 K
nginx.exe                      716 RDP-Tcp#1                  1      4,828 K
D:\soft\nginx-1.0.3>more D:\soft\nginx-1.0.3\logs\nginx.pid
1000(先杀主进程号)

D:\soft\nginx-1.0.3>taskkill -F -pid 1000
成功: 已终止 PID 为 716的进程。
D:\soft\nginx-1.0.3>taskkill -F -pid 716
成功: 已终止 PID 为 1000的进程。

D:\soft\nginx-1.0.3>tasklist /fi "imagename eq nginx.exe"
信息: 没有运行的任务匹配指定标准。

Nginx 常见应用技术指南[Nginx Tips] 第二版

http://zhengdl126.iteye.com/blog/769402

http://bbs.linuxtone.org/thread-1685-1-1.html

http://docs.linuxtone.org/ebooks/Webapp/nginx/

http://docs.linuxtone.org/ebooks/LoadBalance/nginx/

lemp.pdf

Nginx 常见应用技术指南(nginx tips).pdf

高性能Web服务器Nginx及相关新技术的..>

http://docs.linuxtone.org/ebooks/Webapp/nginx/%e9%ab%98%e6%80%a7%e8%83%bdWeb%e6%9c%8d%e5%8a%a1%e5%99%a8Nginx%e5%8f%8a%e7%9b%b8%e5%85%b3%e6%96%b0%e6%8a%80%e6%9c%af%e7%9a%84%e5%ba%94%e7%94%a8%e5%ae%9e%e8%b7%b5.ppt

http://www.inginx.com/download/nginx-pdf-download.zip

LNMP一键安装包

http://lnmp.org/index.html

Nginx和Apache性能的比较 

http://blog.163.com/log_log/blog/static/556948442009413102428555/

http://blog.chinaunix.net/space.php?uid=20776139&do=blog&cuid=1675870

varnish,squid,apache,nginx缓存文件比较

http://blog.51yip.com/server/1032.html

nginx和squid 2.5的gzip压缩问题

http://www.md5.cn/viewthread.php?tid=57

Nginx打开目录浏览功能

http://blog.licess.org/nginx-autoindex/

http://robinwu.iteye.com/blog/1177717

Nginx 安装成Windows 服务方法

http://koda.iteye.com/blog/600725

Nginx 性能优化(突破十万并发)

http://www.linuxany.com/archives/1601.html

apache配置文件httpd.conf自动转换成nginx配置文件nginx.conf

http://www.nginx.cn/96.html

作者:NetSeek  http://www.linuxtone.org (IT运维专家网|集群架构|性能调优)
欢迎转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明.
首发时间: 2008-11-25     更新时间:2009-1-14
目 录
一、        Nginx 基础知识
二、        Nginx 安装及调试
三、        Nginx Rewrite
四、        Nginx Redirect
五、        Nginx 目录自动加斜线:
六、        Nginx Location
七、        Nginx expires
八、        Nginx 防盗链
九、        Nginx 访问控制
十、        Nginx日志处理
十一、     Nginx Cache
十二、      Nginx负载均衡
十三、       Nginx简单优化       
十四、        如何构建高性能的LEMP环境
十五、        Nginx服务监控
十六、        常见问题与错误处理.
十七、        相关资源下载

 

下载:http://centos.alt.ru/pub/nginx/1.0/

nginx-stable-1.0.4-1.el5.i386.rpm 

nginx-stable-1.0.4-1.el5.x86_64.rpm

  

/etc/nginx/nginx.conf  

http://tieba.baidu.com/f?kw=nginx

http://baike.baidu.com/view/926025.htm

http://www.i-program.net/

Nginx 的中文站: http://www.nginx.cn

Nginx 的中文维基:http://wiki.codemongers.com/NginxChs

 

Redhat Enterprise Linux 5.5 手动编译安装nginx-1.0.0.tar.gz

http://linuxguest.blog.51cto.com/195664/578304
[root@nginx1 ~]#  tar -zxvf nginx-1.0.0.tar.gz
[root@nginx1 ~]#  cd nginx-1.0.0
[root@nginx1 ~]#  ./configure --prefix=/usr/local/nginx --user=nobody --group=nobody --with-http_stub_status_module --with-http_ssl_module
  #config完成后,查看结果,使用了哪些库,如果有些你希望使用的库没有使用,需要安装或者添加配置参数
   Configuration summary
   + using system PCRE library
   + using system OpenSSL library
   + md5: using OpenSSL library
   + sha1 library is not used
   + using system zlib library
[root@nginx1 ~]# make && make install
修改配置文件
[root@nginx1 ~]#  vim /usr/local/nginx/conf/nginx.conf
  2 user  nobody; #取消注释
  12 events {
  13     use epoll;
  14     worker_connections  51200;
  15 }
  36     server {
  37         listen       80;
  38         server_name  122.225.32.135;
  66         location ~ \.php$ {
  67
  68                 if ( $fastcgi_script_name ~ \..*\/.*php ) {
  69                         return 403;
  70                 }
  71             root           html;
  72             fastcgi_pass   unix:/tmp/php-cgi.sock;
  73             fastcgi_index  index.php;
  74             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  75             include        fastcgi.conf;
  76         }
  /usr/local/nginx/sbin/nginx  -t -c /usr/local/nginx/conf/nginx.conf  #测试配置文件是否正确
  /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 或者/usr/local/nginx/sbin/nginx 
四、测试
[root@nginx1 ~]# cd /usr/local/nginx/html/
[root@nginx1 ~]# echo "<?phpinfo();?>" >phpinfo.php
 使用浏览器访问 http://122.225.32.135/phpinfo.php
 如果能够正常显示PHP的信息,基本就算成功了。

 

使用 Nginx 提升网站访问速度

http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/index.html

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。
本 wiki 的目的是帮助中文用户安装、配置和交换有关 Nginx 的使用经验。因为 Nginx 的文档大部分是俄文的,因此本 wiki 的内容大部分源自Aleksandar Lazic <al-nginx AT none DOT at> 的英语翻译版。其他语言的版本请点击
Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。 目前 Nginx 的开发版为 0.7.x,稳定版为 0.6.x,历史稳定版为 0.5.x,建议使用 0.6 系列作为生产版本。 Nginx 的源代码使用 2-clause BSD-like license

强大的负载均衡+静态文件WEB服务器nginx实战

 

http://touya.iteye.com/blog/258480

  当前比较流行的负载均衡前端服务器主要有apache(with mod_proxy),nginx,lighttpd,squid,perlbal,pound,或者如果你的域名服务商提供DNS级别的负载均衡,也可 以(就是一个域名随机指向多个IP,定制性不高)。

    以前自己常用pound作为前端,它专注于负载均衡,支持https协议,配置还算简单,不过渐渐发现功能不够强大,转而研究其他一些既可以做负载均衡, 又能做web服务器的高性能工具吧。Perlbal是第一个看的,大牛Danga的杰作,它们开发的memcached(分布式内存cache系统)非常 好用,Perlbal也不差,虽然是基于Perl的,速度上比纯C开发的可能稍逊,但不得不说Danga大牛实力非凡。不过公司的机器都是 perl5.8.5,而Perlbal必须perl5.8.8以上,升级可能有兼容性问题,故只能作罢。

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

    中文维基地址:http://wiki.codemongers.com/NginxChs

模块依赖:
1 gzip支持,需要zlib http://www.zlib.net/ 下载最新版即可
2 rewrite module requires pcre library http://www.pcre.org/ 下载最新版即可
3 ssl 功能需要 openssl 库 http://www.openssl.org/ => http://www.openssl.org/source/ LASTEST版本即可

 

安装过程:

#下载以上source到/usr/local/src/nginx/目录下,解压,则该目录下情况如下:
[root@s16 nginx]# ls
nginx-0.6.32  nginx-0.6.32.tar.gz  openssl-0.9.8i  openssl-0.9.8i.tar.gz  pcre-7.8  pcre-7.8.tar.gz  zlib-1.2.3  zlib-1.2.3.tar.gz

cd nginx-0.6.32
./configure --with-pcre=../pcre-7.8 --with-zlib=../zlib-1.2.3 --with-openssl=../openssl-0.9.8i
make
make install

 

#OK,安装完成
#修改配置:
cd /usr/local/nginx
vi conf/nginx.conf
#例如,去掉例子中的8000端口的服务器配置的注释
sbin/nginx -t -c conf/nginx.conf (测试配置文件是否正确)
[root@s16 nginx]# sbin/nginx -t -c conf/nginx.conf  
2008/09/17 15:26:55 [info] 15879#0: the configuration file conf/nginx.conf syntax is ok
2008/09/17 15:26:55 [info] 15879#0: the configuration file conf/nginx.conf was tested successfully

 

sbin/nginx  (启动)
ps aux | grep nginx | grep -v grep (查看是否正常启动了)
#如果没有正常启动,查看errorlog,默认位置:/usr/local/nginx/logs/error.log

 

# 经过apache bench测试,nginx在serve静态文件方面性能不比apache(with mod_perl)好多少,基本上,以65K为分界点,小文件时nginx性能好(最高可以达到3倍左右速度),大文件时apache性能好(不过差别有 限),所以纯从速度上来讲,nginx并不比apache强,不过nginx小巧,消耗资源少,如果你有很多静态小文件需要serve,的确是个不错的选 择哦。

     这里推荐一种架构:

     1 前端nginx,并serve静态文件,如图片,js,css等,nginx是支持gzip压缩的

     2 后端动态程序用fastcgi(lighttpd的spawn_fcgi即可),可以支持php,perl等多种脚本语言了

、、、

     下面介绍一下nginx的常用配置:

保存文件,并chmod 755 /etc/init.d/nginx

用chkconfig --list nginx查看是否是一个可用后台启动服务,如果是的话,可以直接执行chkconfig --add nginx,这个后台服务搞定(代码中不可省略:#chkconfig: 2345 20 80)
接下可以用service nginx start|restart|stop来操作你的nginx服务器(restart时重新读入config)

怎么样?是不是方便多了?

     小结:本文是我自己实践nginx的整个经验总结,包括了前期准备、安装、配置、架构设计、和现有动态程序结合(公司使用的是Catalyst)、启动脚本等等,希望对大家有帮助,少走歪路。

 

Nginx+tomcat  做负载均衡

http://blog.chinaunix.net/u2/83793/showart_1354266.html

 

 

架构描述

前端一台nginx 服务器做负载均衡器,后端放N 台tomcat 组成集群处理服务,通过nginx 转发到后面(注:没做动静分离,静态动态全部都转给tomcat )

优点:实现了可弹性化的架构,在压力增大的时候可以临时添加tomcat 服务器添加到这个架构里面去

 

RedHat Linux Enterprse 5.3 x64 / nginx-1.0.1 setup

1. pcre-8.02 setup / pcre-8.32.zip

[root@nginxpre1 pcre-8.02]#pwd

/opt/soft/pcre-8.02

[root@nginxpre1 pcre-8.02]# make clean ; ./configure ; make ; make install

2. nginx-1.0.1 setup

[root@nginxpre1 nginx-1.0.1 ]#pwd

/opt/soft/nginx-1.0.1

[root@nginxpre1 nginx-1.0.1 ]# make clean

[root@nginxpre1 nginx-1.0.1 ]# ./configure --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/soft/pcre-8.02/

[root@nginxpre1 nginx-1.0.1 ]# make 

[root@nginxpre1 nginx-1.0.1 ]# make install

3. [root@nginxpre1 local]# ls -l *.tar
-rw-r--r-- 1 root root   71680 May 23 09:58 include.tar
-rw-r--r-- 1 root root  440320 May 21 18:14 lib.tar
-rw-r--r-- 1 root root 2775040 May 23 10:56 nginx.tar
[root@nginxpre1 local]# pwd
/usr/local/

4. root@nginxpre1 local]# ls -l include
total 76
-rw-r--r-- 1 root root  6783 May 21 18:02 pcrecpparg.h
-rw-r--r-- 1 root root 26529 May 21 18:02 pcrecpp.h
-rw-r--r-- 1 root root 13990 May 21 18:02 pcre.h
-rw-r--r-- 1 root root  5452 May 21 18:02 pcreposix.h
-rw-r--r-- 1 root root  6600 May 21 18:02 pcre_scanner.h
-rw-r--r-- 1 root root  6253 May 21 18:02 pcre_stringpiece.h
[root@nginxpre1 local]# ls -l lib
total 448
-rw-r--r-- 1 root root 190562 May 21 18:01 libpcre.a
-rw-r--r-- 1 root root  49512 May 21 18:01 libpcrecpp.a
-rwxr-xr-x 1 root root    965 May 21 18:01 libpcrecpp.la
lrwxrwxrwx 1 root root     19 May 21 18:01 libpcrecpp.so -> libpcrecpp.so.0.0.0
lrwxrwxrwx 1 root root     19 May 21 18:01 libpcrecpp.so.0 -> libpcrecpp.so.0.0.0
-rwxr-xr-x 1 root root  45310 May 21 18:01 libpcrecpp.so.0.0.0
-rwxr-xr-x 1 root root    918 May 21 18:01 libpcre.la
-rw-r--r-- 1 root root   5118 May 21 18:01 libpcreposix.a
-rwxr-xr-x 1 root root    979 May 21 18:01 libpcreposix.la
lrwxrwxrwx 1 root root     21 May 21 18:01 libpcreposix.so -> libpcreposix.so.0.0.0
lrwxrwxrwx 1 root root     21 May 21 18:01 libpcreposix.so.0 -> libpcreposix.so.0.0.0
-rwxr-xr-x 1 root root   9575 May 21 18:01 libpcreposix.so.0.0.0
lrwxrwxrwx 1 root root     16 May 21 18:01 libpcre.so -> libpcre.so.0.0.1
lrwxrwxrwx 1 root root     16 May 21 18:01 libpcre.so.0 -> libpcre.so.0.0.1
-rwxr-xr-x 1 root root 116790 May 21 18:01 libpcre.so.0.0.1
[root@nginxpre1 local]# ls -l nginx
total 36
drwx------ 2 nobody root 4096 May 21 18:12 client_body_temp
drwxr-xr-x 2 root   root 4096 May 23 10:56 conf
drwx------ 2 nobody root 4096 May 21 18:12 fastcgi_temp
drwxr-xr-x 2 root   root 4096 May 21 18:08 html
drwxr-xr-x 2 root   root 4096 May 21 18:12 logs
-rw-r--r-- 1 root   root    0 May 21 18:12 on
drwx------ 2 nobody root 4096 May 21 18:12 proxy_temp
drwxr-xr-x 2 root   root 4096 May 21 18:08 sbin
drwx------ 2 nobody root 4096 May 21 18:12 scgi_temp
drwx------ 2 nobody root 4096 May 21 18:12 uwsgi_temp

5. config and check nginx

[root@nginxpre1 local]# vim /usr/local/nginx/conf/nginx.conf

 49       # tangxje
 50           location /NginxStatus {
 51           stub_status on;
 52           access_log on;
 53   #       auth_basic "NginxStatus";
 54   #       auth_basic_user_file conf/nginx_passwd;
 55           }
 56       # tangxje
. . . . . .
123         # tangxje
124         #  location / {
125         #   root   html;
126         #   index  index.html index.htm;
127         # }
128         location / {
129             root   /opt/SNWCS/WC_EMALL.ear/Stores.war/ConsumerDirectStorefrontAssetStore/;
130             index  index.html index.htm;
131         }
132         # tangxje
. . . . . .

[root@nginxpre1 ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

6. start & stop nginx

stop nginx

[root@nginx1 nginx-1.0.1]# /usr/local/nginx/sbin/nginx -s stop

start nginx

[root@nginx1 nginx-1.0.1]# /usr/local/nginx/sbin/nginx

[root@nginx1 nginx-1.0.1]# ps -ef | grep nginx

avahi     3622     1  0 May20 ?        00:00:03 avahi-daemon: running [nginx1.local]
root     23860 22094  0 15:27 pts/1    00:00:00 grep nginx
root     24760     1  0 May27 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   24761 24760  0 May27 ?        00:05:48 nginx: worker process

7.http://192.168.119.102/NginxStatus

Active connections: 61
server accepts handled requests
 2195913 2195913 2200789
Reading: 0 Writing: 1 Waiting: 60
8.https://192.168.157.102

http://www.itrus.com.cn/verisignchina/Service/SSLInstall/201002/79.html

unconfigure nginx ssl :

start nginx

[root@nginxpre1 ~]# /usr/local/nginx/sbin/nginx
nginx: [emerg] SSL_CTX_use_certificate_chain_file("/usr/local/nginx/conf/cert.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory error:20074002:BIO routines:FILE_CTRL:system lib error:140DC002:SSL routines:SSL_CTX_use_certificate_chain_file:system lib)

configure nginx ssl :

[root@nginxpre1 ~]# vim /usr/local/nginx/conf/nginx.conf

114 #        ssl_certificate      cert.pem;
115          ssl_certificate      lindows.iteye.com.pem;
116 #        ssl_certificate_key  cert.key;
117 #         ssl_certificate_key  lindows.iteye.com.key; 

[root@nginxpre1 conf]# /usr/local/nginx/sbin/nginx
nginx: [emerg] no "ssl_certificate_key" is defined for the "ssl" directive in /usr/local/nginx/conf/nginx.conf:113

configure nginx ssl again :

[root@nginxpre1 ~]# vim /usr/local/nginx/conf/nginx.conf

114 #        ssl_certificate      cert.pem;
115          ssl_certificate      lindows.iteye.com.pem;
116 #        ssl_certificate_key  cert.key;
117           ssl_certificate_key  lindows.iteye.com.key; 

安装服务器证书 复制server.key及server.pem文件到Nginx安装目录下的conf目录。

打开Nginx安装目录下conf目录中的nginx.conf文件
找到
    # HTTPS server
    #server {
    #    listen       443;
    #    server_name  localhost;
    #    ssl                  on;
    #    ssl_certificate      cert.pem ;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_timeout  5m;
    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
将其修改为
    server {
        listen       443;
        server_name  localhost;
        ssl                  on;
        ssl_certificate      xiamiwang.net_public_key.pem;
        ssl_certificate_key  xiamiwang.net_private_key.key;
        ssl_session_timeout  5m;
        ssl_protocols  SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
保存退出,并重启Nginx。
通过https方式访问您的站点,测试站点证书的安装配置。

9. https://192.168.157.102

此连接是不受信任的
          您想使用 Firefox 来安全连接至 192.168.157.102,但是我们无法确认此连接为安全的。
          通常,当您尝试安全连接时,站点会出示受信任的标识,以证明您访问的是正确的地址。然而,现在无法验证此网站的标识。
          怎么办?
            如果您过去连接到此网站并且没有发现问题,那么此错误信息表示可能有人想冒充该网站,所以您应该停止浏览。
          技术细节
          192.168.157.102 使用了无效的安全证书。
该证书仅对下列名称有效:
  *.xiamiwang.net , xiamiwang.net 
(错误码: ssl_error_bad_cert_domain
          我已充分了解可能的风险
            如果您了解现在所发生的一切,您可以告诉 Firefox 并让它信任此站点的标识。
即使您信任此站点,这个错误还表明可能有人尝试干扰您的连接。
            不要随便添加例外,除非您知道并认同该网站不使用受信任标识的理由。

10. modify hosts temp

modify

C:\Windows\System32\drivers\etc\hosts

192.168.157.102  www.xiamiwang.net xiamiwang.net

https://www.xiamiwang.net

 

11.change another domain cert

[root@nginx1 conf]# vim /usr/local/nginx/conf/ngonx.conf

        ssl_certificate      xiamiwang.net_public_key.pem;
        ssl_certificate_key  tangxiujin.com_private_key.key; 

[root@nginx1 conf]# /usr/local/nginx/sbin/nginx -s stop
nginx: [emerg] SSL_CTX_use_PrivateKey_file("/usr/local/nginx/conf/su****cn.key") failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch)

nginx modify again

[root@nginx1 conf]# vim /usr/local/nginx/conf/ngonx.conf

        ssl_certificate      tangxiujin.com_public_key.pem;
        ssl_certificate_key  tangxiujin.com_private_key.key; 

 

[root@nginx1 conf]# ls -l /usr/local/nginx/conf/*.pem
-rw-r--r-- 1 root root 3440 May 31 18:16 tangxiujin.com_public_key.pem
[root@nginx1 conf]# ls -l *.key
-rw-r--r-- 1 root root 1706 May 31 17:37 tangxiujin.com_public_key.key

 

ok

12.http://192.168.119.102/NginxStatus

Active connections: 61
server accepts handled requests
 2195913 2195913 2200789
Reading: 0 Writing: 1 Waiting: 60

http://hi.baidu.com/rj52077/blog/item/95c1ad5419833f0f3a2935a8.html

Active connections: 163
server accepts handled requests
14470 14470 14371
Reading: 54 Writing: 109 Waiting: 0

------------------------------------------------------------------

active connections – 当前 Nginx 正处理的活动连接数163

server accepts handled requests -- 总共处理了 14470 个连接 , 成功创建 14470 次握手 (证明中间没有失败的 ), 总共处理了 14371个请求 ( 平均每次握手处理了 1.3 个数据请求 )。

reading -- nginx 读取到客户端的 Header 信息数54

writing -- nginx 返回给客户端的 Header 信息数109

waiting -- 开启 keep-alive (HTTP长连接,持久连接)的情况下,

这个值等于 active - (reading + writing ),意思就是 Nginx 已经处理完 正在等候下一次请求指令的驻留连接。

nginx status  解释

waiting = active - (reading + writing)

已经处理完正在等候下一次 = 后端发起的活动连接数  ( 读取到客户端head数 + 返回给客户端head数 )
active connections --  对后端发起的活动连接数 
reading -- nginx  读取到客户端的 Header信息数 
writing -- nginx 返回给客户端的 Header信息数 
waiting --  开启 keep-alive  的情况下,这个值等于 active - (reading + writing),意思
就是 Nginx 已经处理完成,正在等候下一次请求指令的驻留连接。在访问效率高,
请求很快被处理完毕的情况下,Waiting 数比较多是正常的。如果 reading + writing
数较多,则说明并发访问量非常大,正在处理过程中。

网页gzip压缩检测

http://gzip.zzbaike.com/

http://www.gongju.com/gzip/www.xiaomi.com

 

Header信息

http://image.s*****.cn/test.html

 

Server nginx/1.0.1
Date Fri, 01 Jul 2011 07:48:02 GMT
Content-Type text/html
Last-Modified Fri, 01 Jul 2011 07:03:11 GMT
Transfer-Encoding chunked
Expires Mon, 11 Jul 2011 07:48:02 GMT
Cache-Control max-age=864000
Content-Encoding gzip
Age 116
X-Via 1.1 yz24:8887 (Cdn Cache Server V2.0)
Connection keep-alive

http://www.gongju.com/gzip/www.xiaomi.com

地址: www.xiaomi.com  请求状态:200
content-length : 6627
powered-by-chinacache : HIT from CHN-NT-3-3FD
content-encoding : gzip
age : 1093
expires : Tue, 09 Oct 2012 00:18:10 GMT
x-cacheable : MI-WWW-Cacheable
server : MIFE/3.0
last-modified : Mon, 08 Oct 2012 23:33:26 GMT
connection : close
cache-control : max-age=1800
date : Mon, 08 Oct 2012 23:48:10 GMT
content-type : text/html; charset=utf-8

网页Gzip压缩检测工具

网 站Gzip压缩可以减小服务器带宽占用,提高用户打开网页速度,最多可以提升网站80%的性能,是每个网站必须开启的功能, 站长工具网页Gzip压缩检测工具方便站长朋友们检测特定网页是否进行了Gzip压缩或网站是否开启Gzip压缩功能, 本工具同时也可以检测到Gzip压缩前网页大小及Gzip压缩后用户访问的实际大小,并显示出Gzip压缩比率及针对专业用户响应头信息!

Gzip压缩使用注意
CompressionLevel 9是指压缩程度的等级,从1到9,9是最高等级。据了解,这样做最高可以减少8成大小的传输量(看档案内容而定),最少也能够节省一半。 CompressionLevel 预设可以采用 6 这个数值,以维持耗用处理器效能与网页压缩质量的平衡 已经是压缩过的图片格式如jpg,音乐档案如mp3、压缩文件如zip之类的,就没必要再压缩了

启用apache2的gzip功能
apache2官方在开发的时候,就已经把网页压缩考虑进去,内建了mod_deflate模块,所以apache2就不需要使用到mod_gzip了, 这两者的工作原理是类似的, 还有启用mod_deflate这个网页压缩的模块,功能和效率和mod_gzip是差不多的,甚至还好一些,就不需要再用mod_gzip模块了。

配置文件实例

<ifmodule mod_deflate.c>
DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE js css
</ifmodule>

nginx 配置 gzip压缩
默认情况下,Nginx的gzip压缩是关闭的,也只对只对text/html进行压缩,需要在编辑nginx.conf文件,在http段加入一下配置,常用配置片段如下:

gzip             on;
gzip_comp_level  6;                        # 压缩比例,比例越大,压缩时间越长。默认是1
gzip_types       text/xml text/plain text/css application/javascript application/x-javascript application/rss+xml; # 哪些文件可以被压缩
gzip_disable     "MSIE [1-6]\.";           # 无视IE6这个笨蛋~

lighttpd配置gzip
配置gzip主要是使用compress模块
1. 激活compress模块,将lightppd.conf文件的mod_compress的注释去掉
2. 配置压缩文件存放目录compress.cache-dir,注意运行lighttpd的用户需要有这个目录的读写权限,压缩文件的类型compress.filetype
3. 将动态生成的php也gzip,修改php.ini,加上zlib.output_compression = On和zlib.output_handler = On
4. 重启lighttpd
配置文件例子

compress.cache-dir  =  "/home/flymouse/lighttpd/compress/"
compress.filetype  =  ("text/plain", "text/html", "application/x-javascript", "text/css", "application/javascript", "text/javascript")

IIS 如何启用 GZip 压缩
微软 IIS 上如何启用 Gzip 压缩机制? 通过命令行执行如下命令启用对动态/静态内容的压缩输出:

appcmd set config /section:urlCompression /doDynamicCompression:True
appcmd set config /section:urlCompression /doStaticCompression:True

新 建一个服务器扩展。名字无所谓,下面的添加文件的路径是:c:\windows\system32\inetsrv\gzip.dll,然后启用这个扩展 IIS 6.0 上压缩额外的文件扩展名 修改 c:\windows\system32\inetsrv\MetaBase.xml 文件中 HcFileExtensions 添加额外的文件扩展名。

IIS 7.0 上压缩额外的文件扩展名
修改 ApplicationHost.config 文件,添加合适的 mimeType 并指定激活. 打开文件参考原有的行照葫芦画瓢就成。可能要设置多次才会成功,因为 mimeType 定义可能有些歧义。

 

http://blog.163.com/jar-c/blog/static/116401250201158112524132/

[root@nginxServer4 ~]# netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c 
     55 ESTABLISHED
      5 FIN_WAIT1
     31 LISTEN
     12 TIME_WAIT

[root@nginxServer4 ~]# netstat -n|awk '/^tcp/{++S[$NF]} END {for(a in S) print a,S[a]}'
TIME_WAIT 13
FIN_WAIT1 1
ESTABLISHED 149
LAST_ACK 1

[root@nginxServer4 ~]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

TIME_WAIT 457
ESTABLISHED 266
FIN_WAIT2 79
SYN_RECV 1

解析:
CLOSED  //无连接是活动的或正在进行
LISTEN  //服务器在等待进入呼叫
SYN_RECV  //一个连接请求已经到达,等待确认
SYN_SENT  //应用已经开始,打开一个连接
ESTABLISHED  //正常数据传输状态/当前并发连接数
FIN_WAIT1  //应用说它已经完成
FIN_WAIT2  //另一边已同意释放
ITMED_WAIT  //等待所有分组死掉
CLOSING  //两边同时尝试关闭
TIME_WAIT  //另一边已初始化一个释放
LAST_ACK  //等待所有分组死掉

 

[root@nginxpre1 ~]# grep many /usr/local/nginx/logs/error.log | more

2011/07/31 18:15:20 [alert] 19764#0: accept() failed (24: Too many open files)

 

 

使用 ulimit –n 65535 可即时修改,但重启后就无效了。

(注ulimit -SHn 65535 等效 ulimit -n 65535 ,-S 指soft ,-H 指hard)

 

方法1.在/etc/rc.local 中增加一行 ulimit -SHn 65535
方法2. 在/etc/profile 中增加一行 ulimit -SHn 65535
方法3.在/etc/security/limits.conf 最后行增加:

* soft nofile 65535

* hard nofile 65535

* soft nproc 65535

* hard nproc 65535

      在 /etc/sysctl.conf 最后行添加:

fs.file-max=65535

linux sysctl.conf中相关重要设定的详细说明

http://www.jiunile.com/linux-sysctl-conf%e4%b8%ad%e7%9b%b8%e5%85%b3%e9%87%8d%e8%a6%81%e8%ae%be%e5%ae%9a%e7%9a%84%e8%af%a6%e7%bb%86%e8%af%b4%e6%98%8e.html

备注:

ulimit 命令本身就有分软硬设置,加-H 就是硬,加-S 就是软,默认显示的是软限制

soft 限制指的是当前系统生效的设置值。 hard 限制值可以被普通用户降低。但是不能增加。

soft 限制不能设置的比 hard 限制更高。 只有 root 用户才能够增加 hard 限制值。

 

如何解决TIME_WAIT过多的解决办法(附Socket中的TIME_WAIT状态详解)

http://hi.baidu.com/xzhijun/blog/item/3a5f49609d443cd58cb10d01.html

如何关闭TIME_WAIT连接 如何查看nginx的访问流量

http://www.now163.net/2011/04/460.html

 

nginx的keepalive

http://hi.baidu.com/171892549/blog/item/e73df5dccbf194aecc116664.html

nginx.conf:
keepalive_timeout  0;

[root@lvs-2 ~]# curl -I http://202.102.xx.xx:8888
HTTP/1.1 200 OK
Server: nginx/0.8.52
Date: Fri, 15 Oct 2010 05:19:06 GMT
Content-Type: text/html
Content-Length: 151
Last-Modified: Wed, 13 Oct 2010 09:46:39 GMT
Connection: close
Accept-Ranges: bytes
keepalive_timeout  65;
[root@lvs-2 ~]# curl -I http://202.102.xx.xx:8888
HTTP/1.1 200 OK
Server: nginx/0.8.52
Date: Fri, 15 Oct 2010 05:18:30 GMT
Content-Type: text/html
Content-Length: 151
Last-Modified: Wed, 13 Oct 2010 09:46:39 GMT
Connection: keep-alive
Accept-Ranges: bytes

 

nginx缓存cache的5种方案

http://www.51testing.com/?uid-116228-action-viewspace-itemid-205763

 

 

Mac OS X  Nginx-1.1.0 Setup

Nginx优化-参数epoll,kqueue,rtsig,eventport,poll和select的区别

http://yjtjh.blog.51cto.com/1060831/294037

bogon:local root# more /System/Library/CoreServices/SystemVersion.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>ProductBuildVersion</key>
        <string>10K549</string>
        <key>ProductCopyright</key>
        <string>1983-2011 Apple Inc.</string>
        <key>ProductName</key>
        <string>Mac OS X</string>
        <key>ProductUserVisibleVersion</key>
        <string>10.6.8</string>
        <key>ProductVersion</key>
        <string>10.6.8</string>
</dict>
</plist>

bogon:local root# uname -a
Darwin bogon 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

bogon:html root# df -h
Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   293Gi  126Gi  167Gi    44%    /
devfs          114Ki  114Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
map auto_home    0Bi    0Bi    0Bi   100%    /home
/dev/disk1s1   466Gi  455Gi   10Gi    98%    /Volumes/dev
/dev/disk0s3   120Gi   68Gi   52Gi    57%    /Volumes/PROGRAM
/dev/disk0s4    50Gi   43Gi  7.9Gi    85%    /Volumes/BOOTCAMP
/dev/disk2s2   706Mi  651Mi   56Mi    93%    /Volumes/极品飞车6

bogon:html root# vim /usr/local/nginx/conf/nginx.conf

bogon:html root# /usr/local/nginx/sbin/nginx -s stop
bogon:html root# /usr/local/nginx/sbin/nginx
nginx: [emerg] invalid event type "epoll" in /usr/local/nginx/conf/nginx.conf:13

bogon:html root# /usr/local/nginx/sbin/nginx       
nginx: [warn] 1024 worker_connections are more than open file resource limit: 256

 

 

Nginx和Apache性能的比较 我的机子配置  2G内存
http://blog.chinaunix.net/space.php?uid=20776139&do=blog&cuid=1675870
1:安装好apache和nginx。它们共用80端口(哈哈,当然修改端口是很简单的),apache安装在 /opt/httpd目录下,nginx安装在/opt/nginx中。
2:安装好webbench(很简单,下载解压后,进入目录,make ,make install即可进行安装),用于压力测试。
3:cd /opt/httpd/bin
./apachectl start
netstat -antl | grep  80  //用以确认该服务是否真正启动!以下是测试部分:
[ root@*bin] # webbench - c 5000 - t 60 http: / / 127. 0. 0. 1/apache_pb. png
Webbench - Simple Web Benchmark 1. 5
Copyright ( c) Radim Kolar 1997- 2004, GPL Open Source Software.
Benchmarking: GET http: / / 127. 0. 0. 1/apache_pb. png
5000 clients, running 60 sec.
Speed=208453 pages/min, 5741557 bytes/sec.
Requests: 208279 susceed, 174 f ailed .
-t 60 表示在60秒的时间内,-c  表示并发数。红色字体部分为性能表示。
很直观的一点就是说有174个响应失败。
4:为了测试的准确性,我cp /opt/httpd/htdocs/apache_pb. png /opt/nginx/html/   //复制文件
接着关闭apache服务器, 以nginx来测试(我必须将其关闭,因为共用80端口,不关闭, 我启动不了nginx)
[ root@qingheliu bin] # pkill httpd //将服务kill掉
[ root@qingheliu bin] # netstat - antl | grep 80 //确认apache是否被kill
[ root@qingheliu bin] # /opt/nginx/sbin/nginx  //启动nginx
[ root@qingheliu bin] # netstat - antl | grep 80 //确认是否正常启动
tcp 0 0 0. 0. 0. 0: 80 0. 0. 0. 0: * LISTEN
//压力测试:
[ *] # webbench - c 5000 - t 60   http: / / 127. 0. 0. 1/apache_pb. png
Webbench - Simple Web Benchmark 1. 5
Copyright ( c) Radim Kolar 1997- 2004, GPL Open Source Software.
Benchmarking: GET http: / / 127. 0. 0. 1/apache_pb. png
5000 clients, running 60 sec.
Speed=325235 pages/min, 8656698 bytes/sec.
Requests: 325235 susceed, 0 failed .
[ root@qingheliu bin] #
比较红色字体的部分。事实胜过雄辩,显然nginx服务器功能更好!
 
nginx location 匹配的优先级
http://lvjian.blog.51cto.com/1304372/595342

(location =) > (location 完整路径 >) >(location ^~ 路径) >(location ~* 正则) >(location 路径)

只要匹配到,其它的都会忽略,然后返回到改匹配。

Nginx的location匹配规则和全局变量

http://hao3721.iteye.com/blog/1181883

Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的"/uri/",可以是字符串或正则表达式。
如果要使用正则表达式,则必须指定前缀:
~ 代表大小写敏感
~*  代表忽略大小写
^~ 不使用正则。 
一、基本语法
location [=|~|~*|^~|@] /uri/ { … }
〖=〗 表示精确匹配,如果找到,立即停止搜索并立即处理此请求。
〖~ 〗 表示区分大小写匹配
〖~*〗 表示不区分大小写匹配
〖^~ 〗 表示只匹配字符串,不查询正则表达式。
〖@〗 指定一个命名的location,一般只用于内部重定向请求。

nginx http 状态码 / 状态代码
[root@nginxpre1 http]# pwd
/opt/soft/nginx-1.1.0/src/http
[root@nginxpre1 http]# ls -l ngx_http_request.h
-rw-r--r-- 1 lindows games 18876 Jul 30 14:20 ngx_http_request.h
/*
 * Copyright (C) Igor Sysoev
 */
#ifndef _NGX_HTTP_REQUEST_H_INCLUDED_
#define _NGX_HTTP_REQUEST_H_INCLUDED_
#define NGX_HTTP_MAX_URI_CHANGES           10
#define NGX_HTTP_MAX_SUBREQUESTS           50
/* must be 2^n */
#define NGX_HTTP_LC_HEADER_LEN             32
#define NGX_HTTP_DISCARD_BUFFER_SIZE       4096
#define NGX_HTTP_LINGERING_BUFFER_SIZE     4096
#define NGX_HTTP_VERSION_9                 9
#define NGX_HTTP_VERSION_10                1000
#define NGX_HTTP_VERSION_11                1001
#define NGX_HTTP_UNKNOWN                   0x0001
#define NGX_HTTP_GET                       0x0002
#define NGX_HTTP_HEAD                      0x0004
#define NGX_HTTP_POST                      0x0008
#define NGX_HTTP_PUT                       0x0010
#define NGX_HTTP_DELETE                    0x0020
#define NGX_HTTP_MKCOL                     0x0040
#define NGX_HTTP_COPY                      0x0080
#define NGX_HTTP_MOVE                      0x0100
#define NGX_HTTP_OPTIONS                   0x0200
#define NGX_HTTP_PROPFIND                  0x0400
#define NGX_HTTP_PROPPATCH                 0x0800
#define NGX_HTTP_LOCK                      0x1000
#define NGX_HTTP_UNLOCK                    0x2000
#define NGX_HTTP_PATCH                     0x4000
#define NGX_HTTP_TRACE                     0x8000
#define NGX_HTTP_CONNECTION_CLOSE          1
#define NGX_HTTP_CONNECTION_KEEP_ALIVE     2
#define NGX_NONE                           1
#define NGX_HTTP_PARSE_HEADER_DONE         1
#define NGX_HTTP_CLIENT_ERROR              10
#define NGX_HTTP_PARSE_INVALID_METHOD      10
#define NGX_HTTP_PARSE_INVALID_REQUEST     11
#define NGX_HTTP_PARSE_INVALID_09_METHOD   12
#define NGX_HTTP_PARSE_INVALID_HEADER      13
/* unused                                  1 */
#define NGX_HTTP_SUBREQUEST_IN_MEMORY      2
#define NGX_HTTP_SUBREQUEST_WAITED         4
#define NGX_HTTP_LOG_UNSAFE                8
#define NGX_HTTP_OK                        200
#define NGX_HTTP_CREATED                   201
#define NGX_HTTP_ACCEPTED                  202
#define NGX_HTTP_NO_CONTENT                204
#define NGX_HTTP_PARTIAL_CONTENT           206
#define NGX_HTTP_SPECIAL_RESPONSE          300
#define NGX_HTTP_MOVED_PERMANENTLY         301
#define NGX_HTTP_MOVED_TEMPORARILY         302
#define NGX_HTTP_SEE_OTHER                 303
#define NGX_HTTP_NOT_MODIFIED              304
#define NGX_HTTP_BAD_REQUEST               400
#define NGX_HTTP_UNAUTHORIZED              401
#define NGX_HTTP_FORBIDDEN                 403
#define NGX_HTTP_NOT_FOUND                 404
#define NGX_HTTP_NOT_ALLOWED               405
#define NGX_HTTP_REQUEST_TIME_OUT          408
#define NGX_HTTP_CONFLICT                  409
#define NGX_HTTP_LENGTH_REQUIRED           411
#define NGX_HTTP_PRECONDITION_FAILED       412
#define NGX_HTTP_REQUEST_ENTITY_TOO_LARGE  413
#define NGX_HTTP_REQUEST_URI_TOO_LARGE     414
#define NGX_HTTP_UNSUPPORTED_MEDIA_TYPE    415
#define NGX_HTTP_RANGE_NOT_SATISFIABLE     416
/* Our own HTTP codes */
/* The special code to close connection without any response */
#define NGX_HTTP_CLOSE                     444
#define NGX_HTTP_NGINX_CODES               494
#define NGX_HTTP_REQUEST_HEADER_TOO_LARGE  494
#define NGX_HTTPS_CERT_ERROR               495
#define NGX_HTTPS_NO_CERT                  496
/*
 * We use the special code for the plain HTTP requests that are sent to
 * HTTPS port to distinguish it from 4XX in an error page redirection
 */
#define NGX_HTTP_TO_HTTPS                  497
/* 498 is the canceled code for the requests with invalid host name */
/*
 * HTTP does not define the code for the case when a client closed
 * the connection while we are processing its request so we introduce
 * own code to log such situation when a client has closed the connection
 * before we even try to send the HTTP header to it
 */
#define NGX_HTTP_CLIENT_CLOSED_REQUEST     499
#define NGX_HTTP_INTERNAL_SERVER_ERROR     500
#define NGX_HTTP_NOT_IMPLEMENTED           501
#define NGX_HTTP_BAD_GATEWAY               502
#define NGX_HTTP_SERVICE_UNAVAILABLE       503
#define NGX_HTTP_GATEWAY_TIME_OUT          504
#define NGX_HTTP_INSUFFICIENT_STORAGE      507
#define NGX_HTTP_LOWLEVEL_BUFFERED         0xf0
#define NGX_HTTP_WRITE_BUFFERED            0x10
#define NGX_HTTP_GZIP_BUFFERED             0x20
#define NGX_HTTP_SSI_BUFFERED              0x01
#define NGX_HTTP_SUB_BUFFERED              0x02
#define NGX_HTTP_COPY_BUFFERED             0x04
....... 
 
Nginx模块开发入门
http://www.evanmiller.org/nginx-modules-guide.html
http://www.cnblogs.com/leoo2sk/archive/2011/04/19/nginx-module-develop-guide.html
OpenResty  a powerful web app server by extending nginx
http://agentzh.org/misc/nginx/ngx_openresty-1.0.5.0.tar.gz
 
本文将基于Nginx最新的1.0.0版本,操作系统环境为Linux(Ubuntu10.10)。
前言
Nginx提要
        Nginx在Linux下的安装与运行
        Nginx配置文件基本结构
        Nginx模块工作原理概述
Nginx模块开发实战
        定义模块配置结构
        定义指令
        创建合并配置信息
        编写Handler
        组合Nginx Module
Nginx模块的安装
Nginx更深入的学习
Nginx参考文献
 
Nginx配置文件通常结构图示
Nginx http 模块工作原理概述

 

 

 

nginx和squid 2.5的gzip压缩问题

http://www.md5.cn/viewthread.php?tid=57

curl --header "Accept-Encoding: xxxx, xxxxxxx" -D x -0 http://192.168.157.103/test.js

上面的命令输出结果是乱码,证明是压缩的

curl --header "Accept-Encoding: xxxx, xxxxxxx" -D x -0 http://192.168.157.103/test.js

上面的命令输出结果是明码,证明是没压缩

修改nginx.conf文件有关gzip压缩的项

[root@nginxServer4 ~]# vim /usr/local/nginx/conf/nginx.conf

“gzip_http_version 1.1;”改为“gzip_http_version 1.0;”,增加一项“gzip_vary on;”,

    gzip  on;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_http_version 1.0;
    gzip_buffers     4  8k;
    #gzip_disable "MSIE [1-6].";
    #gzip_disable "MSIE [1-6] \.";
    #gzip_types       text/plain application/x-javascript text/css text/html application/xml;
    gzip_types       text/plain application/x-javascript text/css text/htm application/xml;

目的是为了让Nginx前端的Squid 2.5/2.6缓存服务器能够缓存经过gzip压缩的页面。

[root@nginxServer4 ~]# cat /usr/local/nginx/conf/nginx.conf

user  nobody;
worker_processes  8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 655350;
#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  40960;
}
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;
    tcp_nodelay	    on; 
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    gzip  on;
    gzip_vary on;
    gzip_min_length  1000;
    #gzip_http_version 1.1;
    gzip_buffers     4  8k;
    #gzip_disable "MSIE [1-6].";
    #gzip_disable "MSIE [1-6] \.";
    #gzip_types       text/plain application/x-javascript text/css text/html application/xml;
    gzip_types       text/plain application/x-javascript text/css text/htm application/xml;
    #gzip_types       text/plain application/javascript text/css text/htm application/xml;

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
      	#tangxje
        location / {
            root   '/opt/SNWCS/WC_EMALL.ear/Stores.war/ConsumerDirectStorefrontAssetStore';
            index  index.html index.htm;
        }
	location ~* ^.+.(gif|jpg|jpeg|png|bmp|swf|html)$ {
     	    	access_log /opt/rsync_log/nginx4_image_http.log;
	 	root '/opt/SNWCS/WC_EMALL.ear/Stores.war/ConsumerDirectStorefrontAssetStore';
	 	#ftpuser '/opt/webroot';
		expires 10d;
	 	break;
        }
	location ~ \.(ico|css|js|txt)$ {
                root '/opt/SNWCS/WC_EMALL.ear/Stores.war/ConsumerDirectStorefrontAssetStore';
                access_log '/opt/rsync_log/nginx_js.log';
                expires 3d;
                break;
        }
        location /NginxStatus {
          stub_status on;
          access_log on;
        }
        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;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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

    # HTTPS server
    server {
        listen       443;
        server_name  localhost;
        ssl                  on;
    #   ssl_certificate      cert.pem;
    #   ssl_certificate      serversu****cn.pem;
        ssl_certificate      serversu****com.pem;
    #   ssl_certificate_key  cert.key;
    #   ssl_certificate_key  su****cn.key;
	ssl_certificate_key  su****com.key;
        ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~* ^.+.(gif|jpg|jpeg|png|bmp|swf|html)$ {
                access_log /opt/rsync_log/nginx4_image_https.log;
                root '/opt/SNWCS/WC_EMALL.ear/Stores.war/ConsumerDirectStorefrontAssetStore';
                #ftpuser '/opt/webroot';
                expires 11d;
                break;
        }
        location ~ \.(ico|css|js|txt)$ {
                root '/opt/SNWCS/WC_EMALL.ear/Stores.war/ConsumerDirectStorefrontAssetStore';
                access_log '/opt/rsync_log/nginx_js_css_txt.log';
                expires 3d;
                break;
	}
     }
    # tangxje@cnsu****.com
}

 

 

nginx nmon //192.168.157.102

[yace@nginxpre2 ~]$ /nmon/nmon_x86_rhel45 -f  -t -s 15 -c 960 -m /nmon

[yace@nginxpre2 ~]$ ps -ef | grep nmon
yace      2820  1537  0 Sep06 pts/3    00:04:30 /nmon/nmon_x86_rhel45
yace     15771 15742  0 Sep10 pts/1    00:01:17 /nmon/nmon_x86_rhel45
yace     23755     1  0 15:21 ?        00:00:00 /nmon/nmon_x86_rhel45 -f -t -s 15 -c 960 -m /nmon
yace     23999 23954  0 15:25 pts/5    00:00:00 grep nmon
root     25835     1  0 00:00 ?        00:00:17 /nmon/nmon_x86_rhel45 -fT -N -m /nmon -s 60 -c 1440

 

 

参考资料
使用 Nginx 提升网站访问速度 

http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/

刘 柄成 (javayou@gmail.com), 软件工程师, 广州市摩网信息技术有限公司 
简介: 本文主要介绍如何在 Linux 系统上安装高性能的 HTTP 服务器 —— Nginx、并在不改变原有网站结构的条件下用 Nginx 来提升网站的访问速度。
刘柄成一直使用 J2EE/J2ME 从事移动互联网方面的开发。DLOG4J 的作者,您可以通过 Java 自由人网站来跟他联系,网址是:http://www.javayou.com ,另外他的邮件地址是 javayou@gmail.com。
 
 
 

nginx怎么加expire头

http://blog.csdn.net/love__coder/archive/2010/09/02/5859820.aspx
 

     nginx怎么对网站的图片、js和css添加过期头呢?

     在nginx配置文件server配置下,添加如下配置:

  server {

 location ~* ^.+\.(jpg|jpeg|gif|png|bmp)$ {
        access_log /data/logs/image.log;
        root '/usr/opt/nginx/html/www1';
        expires 30d;
        break;
    }

 

    location ~* ^.+\.(js|css)?${
       access_log /data/logs/jsandcss.log;   
       root '/usr/opt/nginx/html/www1';
       expires 30d;
    }

    }

     keyword:nginx expire

 

Apache/Nginx Cache Last-Modified、Expires和Etag相关工作原理

http://www.yanghengfei.com/archives/524/

 

使用ETag和expires 调优web 服务器性能

正确使用Etag和Expires标识处理,可以使得页面更加有效被Cache。

在 客户端通过浏览器发出第一次请求某一个URL时,根据 HTTP 协议的规定,浏览器会向服务器传送报头(Http Request Header),服务器端响应同时记录相关属性标记(Http Reponse Header),服务器端的返回状态会是200,格式类似如下:

HTTP/1.1 200 OK

Date: Tue, 03 Mar 2009 04:58:40 GMT

Content-Type: image/jpeg

Content-Length: 83185

Last-Modified: Mon, 22 Nov 2010 16:29:24 GMT

Cache-Control: max-age=2592000


Expires: Thu, 02 Apr 2009 05:14:08 GMT

Etag: "xok.la-961AA72-4CEA99B4415628″
客户端第二次请求此URL时,根据 HTTP 协议的规定,浏览器会向服务器传送报头(Http Request Header),服务器端响应并记录相关记录属性标记文件没有发生改动,服务器端返回304,直接从缓存中读取:

HTTP/1.x 304 Not Modified

Date: Tue, 03 Mar 2009 05:03:56 GMT

Content-Type: image/jpeg

Content-Length: 83185

Last-Modified: Mon, 22 Nov 2010 16:29:24 GMT

Cache-Control: max-age=2592000

Expires: Thu, 02 Apr 2009 05:14:08 GMT

Etag: "xok.la-961AA72-4CEA99B4415628″
其中Last-Modified、Expires和Etag是标记页面缓存标识

一、Last-Modified、Expires和Etag相关工作原理

1、Last-Modified

在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个Last-Modified的属性标记 (Http Reponse Header)此文件在服务期端最后被修改的时间,格式类似这样:

Last-Modified: Mon, 22 Nov 2010 16:29:24 GMT
客户端第二次请求此URL时,根据 HTTP 协议的规定,浏览器会向服务器传送 If-Modified-Since 报头(Http Request Header),询问该时间之后文件是否有被修改过:

If-Modified-Since: Mon, 22 Nov 2010 16:29:24 GMT
如果服务器端的资源没有变化,则自动返回 HTTP 304 (NotChanged.)状态码,内容为空,这样就节省了传输数据量。当服务器端代码发生改变或者重启服务器时,则重新发出资源,返回和第一次请求时类 似。从而保证不向客户端重复发出资源,也保证当服务器有变化时,客户端能够得到最新的资源。

注:如果If-Modified-Since的时间比服务器当前时间(当前的请求时间request_time)还晚,会认为是个非法请求

2、Etag工作原理

HTTP 协议规格说明定义ETag为”被请求变量的实体标记” (参见14.19)。简单点即服务器响应时给请求URL标记,并在HTTP响应头中将其传送到客户端,类似服务器端返回的格式:

Etag: "xok.la-961AA72-4CEA99B4415628″
客户端的查询更新格式是这样的:

If-None-Match: "xok.la-961AA72-4CEA99B4415628″
如果ETag没改变,则返回状态304。

即:在客户端发出请求 后,Http Reponse Header中包含 Etag: “xok.la-961AA72-4CEA99B4415628″

标 识,等于告诉Client端,你拿到的这个的资源有表示 ID:xok.la-961AA72-4CEA99B4415628。当下次需要发Request索要同一个 URI的时候,浏览器同时发出一个If-None-Match报头( Http RequestHeader)此时包头中信息包含上次访问得到的Etag: “xok.la-961AA72-4CEA99B4415628″标识。

If-None-Match: "xok.la-961AA72-4CEA99B4415628"
,这样,Client端等于Cache了两份,服务器端就会比对2者的etag。如果If- None-Match为False,不返回200,返回304 (Not Modified) Response。

3、Expires

给出的 日期/时间后,被响应认为是过时。如Expires: Thu, 02 Apr 2009 05:14:08 GMT

需和Last-Modified结合使用。用于控制请求文件的有效时间,当请求数据在有效期内时客 户端浏览器从缓存请求数据而不是服务器端. 当缓存中数据失效或过期,才决定从服务器更新数据。

4、Last-Modified和Expires

Last- Modified标识能够节省一点带宽,但是还是逃不掉发一个HTTP请求出去,而且要和Expires一起用。而Expires标识却使得浏览器干脆连 HTTP请求都不用发,比如当用户F5或者点击Refresh按钮的时候就算对于有Expires的URI,一样也会发一个HTTP请求出去,所 以,Last-Modified还是要用的,而 且要和Expires一起用。

5、 Etag和Expires

如果服务器端 同时设置了Etag和Expires 时,Etag原理同样,即与Last-Modified/Etag对应的HttpRequest Header:If-Modified-Since和If-None-Match。我们可以看到这两个Header的值和WebServer发出的 Last-Modified,Etag值完全一样;在完全匹配If-Modified-Since和If-None-Match即检查完修改时间和 Etag之后,服务器才能返回304.

6、Last-Modified和Etag

Last-Modified 和ETags请求的http报头一起使用,服务器首先产生 Last-Modified/Etag标记,服务器可在稍后使用它来判断页面是否已经被修改,来决定文件是否继续缓存

过程如下:

1. 客户端请求一个页面(A)。

2. 服务器返回页面A,并在给A加上一个Last-Modified/ETag。

3. 客户端展现该页面,并将页面连同Last-Modified/ETag一起缓存。

4. 客户再次请求页面A,并将上次请求时服务器返回的Last-Modified/ETag一起传递给服务器。

5. 服务器检查该Last-Modified或ETag,并判断出该页面自上次客户端请求之后还未被修改,直接返回响应304和一个空的响应体。

注:

1、Last- Modified和Etag头都是由Web Server发出的Http Reponse Header,Web Server应该同时支持这两种头。

2、Web Server发送完Last-Modified/Etag头给客户端后,客户端会缓存这些头;

3、 客户端再次发起相同页面的请求时,将分别发送与Last-Modified/Etag对应的Http RequestHeader:If-Modified-Since和If-None-Match。我们可以看到这两个Header的值和 WebServer发出的Last-Modified,Etag值完全一样;

4、 通过上述值到服务器端检查,判断文件是否继续缓存;

二、Apache、 Lighttpd和Nginx中针配置Etag和Expires,有效缓存纯静态如css/js/pic/页面/流媒体等文件。

A、Expires

A.1、 Apache Etag

使用Apache的mod_expires 模块来设置,这包括控制应答时的Expires头内容和Cache-Control头的max-age指令

ExpiresActive On

ExpiresByType image/gif "access plus 1 month"

ExpiresByType image/jpg "access plus 1 month"

ExpiresByType image/jpeg "access plus 1 month"

ExpiresByType image/x-icon "access plus 1 month"

ExpiresByType image/bmp "access plus 1 month"

ExpiresByType image/png "access plus 1 month"

ExpiresByType text/html "access plus 30 minutes"

ExpiresByType text/css  "access plus 30 minutes"

ExpiresByType text/txt  "access plus 30 minutes"

ExpiresByType text/js   "access plus 30 minutes"

ExpiresByType application/x-javascript   "access plus 30 minutes"

ExpiresByType application/x-shockwave-flash     "access plus 30 minutes"

<ifmodule mod_expires.c>

<filesmatch "\.(jpg|gif|png|css|js)$">

ExpiresActive on

ExpiresDefault "access plus 1 year"

</filesmatch>

</ifmodule>
当设置了expires后,会自动输出Cache-Control 的max-age 信息

具体关于 Expires 详细内容可以查看Apache官方文档。

在这个时间段里,该文件的请求都将直接通过缓存服务器获取,

当然如果需要忽略浏览器的刷新请求(F5),缓存服务器squid还需要使用 refresh_pattern 选项来忽略该请求

refresh_pattern -i \.gif$ 1440 100% 28800 ignore-reload

refresh_pattern -i \.jpg$ 1440 100% 28800 ignore-reload

refresh_pattern -i \.jpeg$ 1440 100% 28800 ignore-reload

refresh_pattern -i \.png$ 1440 100% 28800 ignore-reload

refresh_pattern -i \.bmp$ 1440 100% 28800 ignore-reload

refresh_pattern -i \.htm$ 60 100% 100 ignore-reload

refresh_pattern -i \.html$ 1440 50% 28800 ignore-reload

refresh_pattern -i \.xml$ 1440 50% 28800 ignore-reload

refresh_pattern -i \.txt$ 1440 50% 28800 ignore-reload

refresh_pattern -i \.css$ 1440 50% 28800 reload-into-ims

refresh_pattern -i \.js$ 60 50% 100 reload-into-ims

refresh_pattern . 10 50% 60
有关Squid中Expires的说明,请参考Squid官方中refresh_pattern介 绍。

A.2、Lighttpd Expires

和Apache一样Lighttpd设置expire也要先查看是否支持了mod_expire模 块,

下面的设置是让URI中所有images目录下的文件1小时后过期;

expire.url = ( "/images/" => "access 1 hours" )
下面是让作用于images目录及其子目录的文件;

$HTTP["url"] =~ "^/images/" {
 
expire.url = ( "" => "access 1 hours" )
 
}
也可以指定文件的类型;

$HTTP["url"] =~ "\.(jpg|gif|png|css|js)$" {
 
expire.url = ( "" => "access 1 hours" )
 
}
具体参考Lighttpd官方Expires解释

A.3、Nginx中Expireslocation ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

location ~ .*\.(js|css)?$

{

expires 1h;

}
这类文件并不常修改,通过 expires 指令来控制其在浏览器的缓存,以减少不必要的请求。 expires 指令可以控制 HTTP 应答中的” Expires “和” Cache-Control “的头标(起到控制页面缓存的作用)。其他请参考Nginx中Expires

B.1、Apache中Etag设置
在Apache中设置Etag的支持比较简单,只用在含有静态文件的目录中建立一个文件.htaccess, 里面加入:

FileETag MTime Size
这样就行了,详细的可以参考Apache的FileEtag文档页

B.2、 Lighttpd Etag
在Lighttpd中设置Etag支持:

etag.use-inode: 是否使用inode作为Etag

etag.use-mtime: 是否使用文件修改时间作为Etag

etag.use-size: 是否使用文件大小作为Etag

static-file.etags: 是否启用Etag的功能

第四个参数肯定是要enable的, 前面三个就看实际的需要来选吧,推荐使用修改时间

B.3、 Nginx Etag

Nginx中默认没有添加对Etag标识.Igor Sysoev的观点”在对静态文件处理上看不出如何Etag好于Last-Modified标识。”

Note:

Yes, it's addition,and it's easy to add, however, I do not see howETag is better than Last-Modified for static files. -Igor Sysoev

A nice short description is here:

http://www.mnot.net/cache_docs/#WORK

It looks to me that it makes some caches out there to cache theresponse from the origin server more reliable as in rfc2616(ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt) is written.
3.11 Entity Tags 13.3.2 Entity Tag Cache Validators 14.19 ETag

当然也有第三方nginx- static-etags 模块了,请参考

https://github.com/mikewest/nginx-static-etags

三、对于非实时交互动态页面中Expires和Etag处理

对 数据更新并不频繁、如tag分类归档等等,可以考虑对其cache。简单点就是在非实时交互的动 态程序中输出expires和etag标识,让其缓存。但需要注意关闭session,防止http response时http header包含session id标识;

3.1、Expires

如expires.php

<?php
 
header('Cache-Control: max-age=86400,must-revalidate');
 
header('Last-Modified: ' .gmdate('D, d M Y H:i:s') . ' GMT' );
 
header("Expires: " .gmdate ('D, d M Y H:i:s', time() + '86400′ ). ' GMT');
 
?>
以上信息表示该文件自请求后24小时后过期。

其他需要处理的动态页面直接调用即可。

3.2、Etag

根据Http返回状态来处理。当返回304直接从缓 存中读取

如etag.php

>


cache();

echo date("Y-m-d H:i:s");

function cache()

{

$etag = "http://xok.la";

if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag)

{

header('Etag:'.$etag,true,304);

exit;

}

else header('Etag:'.$etag);

}

?>


本文来自: E点废墟(www.xok.la)

详细出处参考:http://xok.la/2010/11/apache_nginx_cache_last-modified%e3%80%81expires_etag.html

 

varnish,squid,apache,nginx缓存文件比较

 

http://blog.51yip.com/server/1032.html

一,测试环境

1,硬件是奔腾双核,机子三年前买的。系统是archlinux

2,测试varnish和squid的时候,web服务用的apache

3,测试apache的时候,启动了5个进程,不过随着压力的增加,进程会增加的。

4,测试nginx的时候,启动了十个nginx进程,20个php-cgi进程

 

5,varnish,squid,nginx用的是反向代理的形势,也就是说访问图片的时候,要先透过缓存工具

二,测试

1,varnish

[root@BlackGhost bin]# /usr/local/bin/webbench -c 100 -t 20 http://127.0.0.1:8080/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://127.0.0.1:8080/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
100 clients, running 20 sec.

Speed=476508 pages/min, 47258114 bytes/sec.
Requests: 158836 susceed, 0 failed.

varnish的缓存效率命中率真的好高,看下图:

varnish

varnish

访问了这么次,没有缓存只有一次,效率真的很高。

2,squid

[root@BlackGhost bin]# /usr/local/bin/webbench -c 100 -t 20 http://localhost:9000/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://localhost:9000/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
100 clients, running 20 sec.

Speed=133794 pages/min, 7475018 bytes/sec.
Requests: 44598 susceed, 0 failed.

从 测试效果来说,squid挺让我失望的,在测试前,我心里是这样估计的,缓存最好的是 varnish,其次是squid,然后nginx,最后是apache,现在呢,squid是最差的。后来我看了一下log文件,发现正常情况下,缓存 和没有缓存的比率不是1:2,如果在高压力下,缓存和没有缓存的比率更小。

3,apache

[root@BlackGhost conf]# /usr/local/bin/webbench -c 100 -t 20 http://localhost/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://localhost/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
100 clients, running 20 sec.

Speed=160890 pages/min, 15856005 bytes/sec.
Requests: 53630 susceed, 0 failed.

4,nginx

[root@BlackGhost conf]# /usr/local/bin/webbench -c 100 -t 20 http://localhost:10000/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://localhost:10000/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
100 clients, running 20 sec.

Speed=304053 pages/min, 30121517 bytes/sec.
Requests: 101351 susceed, 0 failed.

从 上面的测试结果我们可以发现,varnish > nginx > apache > squid,我想这个结果,根大家预期的结果有点出入,因为squid做老牌文件缓存工具怎么会这么差呢,squid的命中率低,我在网上查了一下,很多 人都是这样的,这个可能根个人配置有关系,也许真正的高手,才能让squid发挥最大功力。

 

loadrunner nginx

action.h

#include "web_api.h"


Action()
{



	web_url("http://192.168.157.103",
		"URL=http://192.168.157.103{url}",
		"Resource=1",
		"RecContentType=image/png",
		//"Model=HTTP",
		"Referer=",
		LAST);
/*
	web_image_check("web_image_check",
  		"Src={url}",
  		LAST);

*/	

 	return 0;
}

 

参数化{url} ---- url.dat 文件

参数化{url} ---- url.dat 内容

url
/content/catentries/00000000010117/000000000101173192/000000000101173192_ls.png
/content/catentries/00000000010117/000000000101177652/000000000101177652_ls.png
/content/catentries/00000000010117/000000000101174760/000000000101174760_ls.png
/content/catentries/00000000010117/000000000101177878/000000000101177878_ls.png
/content/catentries/00000000010117/000000000101173607/000000000101173607_ls.png
/content/catentries/00000000010117/000000000101174470/000000000101174470_ls.png
/content/catentries/00000000010117/000000000101176867/000000000101176867_ls.png
/content/catentries/00000000010117/000000000101170137/000000000101170137_ls.png
/content/catentries/00000000010117/000000000101171897/000000000101171897_ls.png
/content/catentries/00000000010117/000000000101174822/000000000101174822_ls.png
/content/catentries/00000000010117/000000000101174352/000000000101174352_ls.png
/content/catentries/00000000010117/000000000101175062/000000000101175062_ls.png
/content/catentries/00000000010117/000000000101174703/000000000101174703_ls.png
/content/catentries/00000000010117/000000000101177723/000000000101177723_ls.png

 

《淘宝网Nginx定制与开发实战》ppt下载

http://www.oschina.net/news/23925/taobao-nginx

本次Velocity大会(http://velocity.oreilly.com.cn/2011/ )上由淘宝网的叔度和清无分享的《淘宝网Nginx定制与开发实战》演示稿现已可以下载。
ppt里面包含了一些Tengine和其它一些淘宝开源Nginx模块相关信息。
RedHat Linux 的 EPoll模型
http://wenku.baidu.com/view/6b834869a98271fe910ef98e.html
http://dl.iteye.com/topics/download/337b233f-1f35-3423-9778-979e064056aa
RHEL 5.5 select / poll / epoll
[root@b2cdevnginx03 nmon]# man select
NAME
 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O multiplexing
[root@b2cdevnginx03 nmon]# man poll

[root@b2cdevnginx03 nmon]# man ppoll
NAME
       poll, ppoll - wait for some event on a file descriptor
[root@b2cdevnginx03 nmon]# man epoll
NAME
       epoll - I/O event notification facility
 
nginx 静态缓存demo
http://i-david.iteye.com/blog/1309846
1,环境介绍
linux:ubuntu 11.10 server
nginx:1.0.5(apt-get方式安装)

2,vim nginx.conf
在 http{……}中添加 如下
Java代码  收藏代码
  1. ##cache##  
  2. proxy_connect_timeout 5 ;  
  3. proxy_read_timeout 60 ;  
  4. proxy_send_timeout 5 ;  
  5. proxy_buffer_size 16k;  
  6. proxy_buffers 4  64k;  
  7. proxy_busy_buffers_size 128k;  
  8. proxy_temp_file_write_size 128k;  
  9. proxy_cache_key $host$server_port$request_uri;  
  10. proxy_temp_path /data/cache/temp;  
  11. proxy_cache_path /data/cache/yy levels=1 : 2  keys_zone=yy:200m inactive=1d max_size=30g;  
  12. ##end##  


3,vim conf.d/cache.conf
Java代码  收藏代码
  1. server {  
  2.         listen 80 ;  
  3.   
  4.         location / {  
  5.                 proxy_pass http://yy.ttpod.com;   
  6.         }  
  7.   
  8.         location ~ .*\.(gif|jpg|png|jpeg|bmp|css|js|flv|ico|swf|zip|html|htm|txt)(.*) {  
  9.                 proxy_pass http://yy.ttpod.com;   
  10.                 proxy_redirect off;  
  11.                 proxy_set_header Host $host;  
  12.                 proxy_cache yy;  
  13.                 proxy_cache_valid 200   302  6h;  
  14.                 proxy_cache_valid 301  1d;  
  15.                 proxy_cache_valid any 1m;  
  16.                 expires 30d;  
  17.         }  
  18.   
  19. }  


4,验证语法及重新加载配置
root@server-2:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
OK 通过

root@server-2:~# service nginx reload
Reloading nginx configuration: nginx.
重新加载配置 通过

5,测试
ps:wml页面
浏览器输入 http://8.0.0.2/
页面还加载 http://yy.ttpod.com/ 页面内容 通过

效果对比


图中三张图片,前两张图片由代理缓存服务器提供,第三张图片由源服务器提供
前两张图片不需要下载,第三张图片需要重新下载



由代理缓存服务器提供的图片,被设置了缓存时间,发送请求的时间还未过期,故浏览器不会重新下载

 

 

 

**********************************************************************************

Nginx1.0.1 在解析 https 带 cookie时的性能表现比较很差。

一般来讲软件解析 HTTPS相对差些,硬件(如:Ctrix负载设备)解析 HTTPS 成 HTTP 再分发给多台Nginx会更快些

Ctrix NetScaler NS9.2: Build 56.2.nc / MPX 9600

 

LoadRunner Script

#include "web_api.h"

Action()
{
	lr_think_time(1);
	lr_start_transaction("查看图片");
// WC_SERVER=0 为ctrix 模拟配置cookie头 
/*
	web_add_cookie("WC_SERVER=0; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=1; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=2; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=3; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=4; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=5; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=6; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=7; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=8; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=9; DOMAIN=cookie.xxxxxx.com");
	web_add_cookie("WC_SERVER=10; DOMAIN=cookie.xxxxx.com");
*/
	web_url("morelogo6.png",
		"URL=http://cookie.xxxxxx.com/OtherArea/headerAndFooter/images/morelogo6.png",
		"Resource=1",
		"RecContentType=image/png",
		"Referer=",
		LAST);
	lr_end_transaction("查看图片", LR_AUTO);
	return 0;
}

 

 

 

4.4    场景四:(单场景)Citrix不配置基于cookie的分发规则_ HTTPS协议
4.4.1    场景说明
业务场景    模拟Citrix不配基于cookie的分发规则,并且基于HTTPS协议
测试目的    模拟高并发的压力下,测试Citrix不配条基于cookie的分发规则(HTTPS协议)时的处理能力,同时将测试结果与其他所有场景的测试结果作对比。
测试数据    1条价格图片的请求Url
测试策略    逐渐增加负载,监控各步骤的平均相应时间和资源消耗。
测试时间    30min(根据测试情况作适当调整)
虚拟用户    20000(根据测试情况作适当调整)
思考时间    NO
监控指标    响应时间,TPS,吞吐率,各服务器的CPU ,DISK IO, NET ,SystemOut.log/SystemError

 

4.4.2    结果摘要
测试时间    2012-9-28 21:39 - 2012-9-28 21:49
平均响应时间    max=48.6s ,min=0.012s,avg=2.8s

TPS    avg=740.3
平均Hits/s    700
Hostname    Server Type    CPU
10.19.250.113    Nginx    100%
4.4.3    性能指标
4.4.3.1    平均响应时间
图(略)
4.4.3.2    TPS
图(略)
4.4.4    测试过程
4.4.4.1    2012-9-28 21:39 - 21:49 第一次压测。
场景设置:20000用户(1500Vu/15s);30Min;
测试目的:测试Citrix在基于HTTPS协议,不配Cookie的情况下的处理能力
结果摘要:Hits:9916;TPS:9937/sec;ResponseTime:0.98sec;
资源利用率:10.19.250.113 100%
4.4.5    发现问题
1、    两台Nginx(250.113、250.114)的CPU利用率达到了95%左右,与昨天测试中两台Nginx的CPU相差很大。
2、    20000用户压测时,在HTTPS协议的情况下,Citrix的Hits达到了1500后不再增加,成为了该场景的瓶颈。
3、当压113、114时,两台CPU达到了100%,最高的hits达到了1500左右;
单压113时,CPU达到了100%,hits达到了700左右;
单压114时,CPU达到了100%,hits达到了700左右;
4、当HTTPS在不配Cookie的时候,Nginx的压力会达到100%,原因是无Cookie的时候,有Nginx对HTTPS协议进行解密,导致压力太大。

  

nginx post error
http://union.su****.com/aas/open/vistorAd.action?userId=1028&webSiteId=1027&adInfoId=1001&adBookId=1021&subUserEx=emar110643|&vistURL=http://image2.su****.cn/images/advertise/hg/juheye/index.html (http://union.su****.com/aas/open/vistorAd.action?userId=1028&webSiteId=1027&adInfoId=1001&adBookId=1021&subUserEx=emar110643%7C&vistURL=http://image2.su****.cn/images/advertise/hg/juheye/index.html)

http://union.su****.com/aas/open/vistorAd.action?userId=1028&webSiteId=1027&adInfoId=1001&adBookId=1021&subUserEx=emar110643|&vistURL=http://image2.su****.cn/images/advertise/hg/juheye/index.html (http://union.su****.com/aas/open/vistorAd.action?userId=1028&webSiteId=1027&adInfoId=1001&adBookId=1021&subUserEx=emar110643%7C&vistURL=http://image2.su****.cn/images/advertise/hg/juheye/index.html)

 

 

nginx server list

nginx/1.0.2 http://rhel.ieesee.net
nginx/0.7.62 http://pv2.jyimg.com/ 
nginx/0.8.54
http://images1.jyimg.com
http://images2.jyimg.com
http://images3.jyimg.com
http://images4.jyimg.com
http://images5.jyimg.com
http://images6.jyimg.com
404 Not Found nginx/0.7.62 http://a0331.9wad.com/NginxStatus
504 Gateway Time-out nginx/0.8.42 http://yunfile.com/ls/hanguo17g/
404 Not Found nginx/0.7.67 http://www.berryrecords.com/china-chengdu-the-second-site-2/
404 Not Found nginx/0.8.53 http://apple.qq.com/qq/apple/mac/
404 Not Found nginx/QQ http://g1.cnc.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin=170475387
404 Not Found nginx/0.6.31 qq.com
404 Not Found image.su****.cn nginx/1.0.1
http://haerbin.fuwo.com nginx/0.7.65
nginx/0.6.34 502 Bad Gateway
http://www.onlylady.com/ent/feilinchongyindian-shendupandian-220048-12.html
nginx 404 bad request http://www.xici.net/#d144280505.htm
nginx 404 not found http://www.linuxpk.com/58136.html
nginx/0.8.47 404 not found http://s1.lingoes.cn/download/lingoes_2.7.1_cn.exe
nginx/0.8.32 504 Gateway Time-out http://finance.ifeng.com/news/20101230/3135961.shtml
nginx/0.7.67 404 not found http://xunlei.v.usportnews.com/video/?vid=43147&ch=sports&r=53600&a=1&c=1&t=1
nginx/0.7.67 403 not found http://xunlei.3gtv.net/video/?vid=50791&ch=joke&
nginx 302 found http://www.njnews.cn/njbyw/jzpd/default.shtml
nginx  403 not found http://www.people.com.cn
nginx error! /usr/share/nginx/html/404.html   http://www.onlinedown.net/SOFT/614.htm
nginx/0.6.35 504 Gateway Time-out http://www.52blackberry.com/thread-270995-1-1.html
nginx/1.0.5 504 Gateway Time-out http://detail.zol.com.cn/tablepc/index272616.shtml
nginx/0.8.33 403 Forbindden http://down1a.zol.com.cn/liaotian/NM30.zip?key=275944ef794a7157c502c6942a9c6850
404 Not Found   nginx/0.7.64  http://www.chinanews.com.cn/expo/news/2010/04-30/2258074.shtml
400 Bad Request nginx/0.6.32  http://bbs.chinanews.com.cn/thread- 1673019-1-1.html?833961153=2415636579
502 Bad Gateway nginx/0.6.32 http://comment.xunlei.com/subject/626519
502 Bad Gateway nginx/0.7.64 http://www.lingaoyi.com/games/battlefield-3-release-in-2009/
503 Service Temporarily Unavailable nginx/0.6.35 http://ctc-1.farm-2.damipan.com
504 Gateway Time-out nginx/0.7.58 http://bbs.topzj.com/regdomain.php
504 Gateway Time-out  nginx/0.6.36  http://topic.csdn.net/t/20010929/10/306907.html
403 Forbidden nginx/0.6.35 http://123.55.252.122:8040
403 Forbidden nginx/0.6.33 http://phpchina.com/
403 Forbidden  nginx/0.6.35 http://www.uplinux.com/download/doc/php/php_manual_zh.chm
404 Not Found nginx http://wiki.ubuntu.org.cn/Image:Wiki_system_s.png
404 Not Found nginx/0.7.54 https://f5demo.s135.com/tmui/Control/jspmap/tmui/system/device
/properties_general.jsp
404 Not Found nginx/0.6.31 hexun.com
404 Not Found nginx/0.6.31 duote.com
404 Not Found nginx/0.5.35 http://www.nju.org.cn/mobile/

 

D:\soft\157.195_nginx的目录(待研究)

 驱动器 D 中的卷是 data
 卷的序列号是 38CD-F23F

 D:\TestCase\20130131_nginx\192.168.157.195 的目录

2013-01-31  20:46    <DIR>          .
2013-01-31  20:46    <DIR>          ..
2013-01-31  20:36    <DIR>          bind-9.9.2-P1
2013-01-31  20:35         7,277,498 bind-9.9.2-P1.tar.gz
2013-01-31  20:36    <DIR>          clientlogs
2013-01-31  20:37    <DIR>          cmake-2.8.4
2013-01-31  20:35         5,477,628 cmake-2.8.4.tar.gz
2013-01-31  20:35             1,997 db2ese.rsp
2013-01-31  20:41               369 DBCon2.py
2013-01-31  20:35                 2 dead.letter
2013-01-31  20:37    <DIR>          dedup-master
2013-01-31  20:35                31 delete_cookie.config
2013-01-31  20:37    <DIR>          Desktop
2013-01-31  20:37    <DIR>          fio
2013-01-31  20:37    <DIR>          haproxy-1.4.22
2013-01-31  20:35           831,791 haproxy-1.4.22.tar.gz
2013-01-31  20:41         5,378,934 httpd-2.2.22.tar
2013-01-31  20:37    <DIR>          ihslogs
2013-01-31  20:35            54,306 install.log
2013-01-31  20:35             4,795 install.log.syslog
2013-01-31  20:37    <DIR>          InstallShield
2013-01-31  20:37    <DIR>          isus
2013-01-31  20:35           533,551 jmagick-6.4.0-src.tar.gz
2013-01-31  20:35                 0 jpegsrc.v8b.tar.gz
2013-01-31  20:37    <DIR>          keepalived-1.2.2
2013-01-31  20:35           249,557 keepalived-1.2.2.tar.gz
2013-01-31  20:46                 0 list.txt
2013-01-31  20:35            16,858 mbox
2013-01-31  20:41         4,067,066 mod-pagespeed-stable_current_i386.rpm
2013-01-31  20:41         4,082,959 mod-pagespeed-stable_current_x86_64.rpm
2013-01-31  20:41             2,955 modsecurity-apache.tar.gz
2013-01-31  20:41           783,102 modsecurity-apache_2.6.4.tar.gz
2013-01-31  20:41    <DIR>          mysql-5.5.28
2013-01-31  20:35        24,739,429 mysql-5.5.28.tar.gz
2013-01-31  20:35         8,306,169 MySQL-client-community-6.0.11-0.rhel5.i386.rpm
2013-01-31  20:35        26,885,954 MySQL-server-community-6.0.11-0.rhel5.i386.rpm
2013-01-31  20:35        40,950,493 mysql_cluster_rpm.zip
2013-01-31  20:41           691,002 nginx-1.0.12.tar.gz
2013-01-31  20:41    <DIR>          openssh-5.9p1
2013-01-31  20:35         1,110,014 openssh-5.9p1.tar.gz
2013-01-31  20:37    <DIR>          plglogs
2013-01-31  20:41    <DIR>          PMT
2013-01-31  20:41    <DIR>          preheat
2013-01-31  20:35            29,467 records.conf
2013-01-31  20:35            29,467 records.config
2013-01-31  20:41    <DIR>          redis
2013-01-31  20:41           610,267 redis-2.4.6.tar.gz
2013-01-31  20:41           618,164 redis-2.4.8.tar.gz
2013-01-31  20:41    <DIR>          replace-age
2013-01-31  20:41           783,102 root@10.21.139.35
2013-01-31  20:41    <DIR>          sitCluster1
2013-01-31  20:41           235,520 SquidAnalyzer-4.3.tar
2013-01-31  20:41            79,705 steelThread-redmon-6a66903.zip
2013-01-31  20:41    <DIR>          TingT
2013-01-31  20:42    <DIR>          trafficserver-3.2.0
2013-01-31  20:35         2,516,870 trafficserver-3.2.0.tar.bz2
2013-01-31  20:35             8,071 trafficserver-46ab04c.tar.gz
2013-01-31  20:42    <DIR>          trafficserver-c1ceba8
2013-01-31  20:35            38,568 trafficserver-c1ceba8.tar.gz
2013-01-31  20:42    <DIR>          tsar
2013-01-31  20:42    <DIR>          varnish-3.0.2
2013-01-31  20:41         1,977,852 varnish-3.0.2.tar.gz
2013-01-31  20:41         2,070,020 varnish-3.0.3.tar.gz
2013-01-31  20:41        90,767,360 varnish.20130130.tar
2013-01-31  20:35             3,844 vpd.properties
2013-01-31  20:42    <DIR>          was64
2013-01-31  20:35             1,882 xorg.conf.new
              40 个文件    231,216,619 字节
              27 个目录 16,978,657,280 可用字节

 

nginx exception

http://hi.baidu.com/jiaodj/item/7be75bfa113c9412d6ff8cde

make: *** 没有规则可以创建“default”需要的目标“build” 问题解决

安装 nginx 时出现 make: *** 没有规则可以创建“default”需要的目标“build” 提示,看nginx configure 时的提示是因为pcre没安装的问题,所以安装一下

在  下载 pcre 

tar xvzf pcre-**.tar.gz

cd nginx-**/

./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/software/pcre-**

make && make install

 

./configure:error:theHTTPrewritemodulerequiresthePCRElibrary解决

http://www.2cto.com/os/201303/198960.html

有时候,我们需要单独安装nginx,来处理大量的下载请求。

单独在Centos5安装nginx遇到的rewrite和HTTPcache错误解决办法:

wgethttp://nginx.org/download/nginx-0.8.33.tar.gz

tar-zxvfnginx-0.8.33.tar.gz

cdnginx-0.8.33

./configure--prefix=/usr/local/nginx

安装Nginx时报错

./configure:error:theHTTPrewritemodulerequiresthePCRElibrary.

安装pcre-devel解决问题

yum-yinstallpcre-devel

错误提示:./configure:error:theHTTPcachemodulerequiresmd5functions

fromOpenSSLlibrary.Youcaneitherdisablethemodulebyusing

--without-http-cacheoption,orinstalltheOpenSSLlibraryintothesystem,

orbuildtheOpenSSLlibrarystaticallyfromthesourcewithnginxbyusing

--with-http_ssl_module--with-openssl=<path>options.

解决办法:

yum-yinstallopensslopenssl-devel

总结:

yum-yinstallpcre-developensslopenssl-devel

./configure--prefix=/usr/local/nginx

make

makeinstall

一切搞定

 

http://exim.mirror.fr/pcre/pcre-8.37.tar.gz

 

nginx的upstream目前支持5种方式的分配

http://lihuipeng007.blog.163.com/blog/static/12108438820108206101535/

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

例如:

upstream bakend {

server 192.168.0.14 weight=10;

server 192.168.0.15 weight=10;

}

3、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

例如:

upstream bakend {

ip_hash;

server 192.168.0.14:88;

server 192.168.0.15:80;

}

4、fair(第三方) 

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

upstream backend {

server server1;

server server2;

fair;

}

5、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {

server squid1:3128;

server squid2:3128;

hash $request_uri;

hash_method crc32;

}

upstream bakend{#定义负载均衡设备的Ip及设备状态

ip_hash;

server 127.0.0.1:9090 down;

server 127.0.0.1:8080 weight=2;

server 127.0.0.1:6060;

server 127.0.0.1:7070 backup;

}

在需要使用负载均衡的server中增加

proxy_pass http://bakend/;

每个设备的状态设置为:

1.down 表示单前的server暂时不参与负载

2.weight 默认为1.weight越大,负载的权重就越大。

3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误

4.fail_timeout:max_fails次失败后,暂停的时间。

5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug

client_body_temp_path 设置记录文件的目录 可以设置最多3层目录

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

 

Nginx配置upstream实现负载均衡

http://www.linuxidc.com/Linux/2015-03/115207.htm

1. 在http节点下,添加upstream节点。
upstream linuxidc { 
  server 10.0.6.108:7080; 
  server 10.0.0.85:8980; 
}
2.  将server节点下的location节点中的proxy_pass配置为:http:// + upstream名称,即“http://linuxidc”.
location / { 
		root  html; 
		index  index.html index.htm; 
		proxy_pass http://linuxidc; 
}

3.  现在负载均衡初步完成了。

文件 /usr/local/nginx/conf/nginx.conf

内容:

 

end

  • 大小: 46 KB
  • 大小: 79.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics