DBMNG数据库管理与应用

所谓独创的能力,就是经过深思的模仿。
当前位置:首页 > 服务器配置 > nginx

Nginx的静态资源缓存以及压缩

Nginx是一款轻量级的网页服务器、反向代理器以及电子邮件代理服务器。Nginx采用的是异步非阻塞的通信机制(epoll模型),支持更大的并发连接.所谓的epoll模型:当事件没有准备好时,就放入epoll(队列)里面。如果有事件准备好了,那么就去处 理;如果事件返回的是EAGAIN,那么继续将其放入epoll里面。从而,只要有事件准备好了,我们就去处理它,只有当所有事件都没有准备好时,才在 epoll里面等着。这样,我们就可以并发处理大量的并发了,当然,这里的并发请求,是指未处理完的请求,线程只有一个,所以同时能处理的请求当然只有一 个了,只是在请求间进行不断地切换而已,切换也是因为异步事件未准备好,而主动让出的。这里的切换是没有任何代价,你可以理解为循环处理多个准备好的事 件,事实上就是这样的。
    与多线程方式相比,这种事件处理方式是有很大的优势的,不需要创建线程,每个请求占用的内存也很少,没有上下文切换, 事件处理非常的轻量级,并发数再多也不会导致无谓的资源浪费(上下文切换)。对于IIS服务器,每个请求会独占一个工作线程,当并发数上到几千时,就同时 有几千的线程在处理请求了。这对操作系统来说,是个不小的挑战:因为线程带来的内存占用非常大,线程的上下文切换带来的cpu开销很大,自然性能就上不 去,从而导致在高并发场景下性能下降严重。
    Nginx通过异步非阻塞的事件处理机制,Nginx实现由进程循环处理多个准备好的事件,从而实现高并发和轻量级。

    Master/Worker结构:一个master进程,生成一个或多个worker进程(网上找的图):

    Master-Worker设计模式核心思想是将原来串行的逻辑并行化,并将逻辑拆分成很多独立模块并行执行。其中主要包含两个主要组件Master和Worker,Master主要用来管理worker进程,将逻辑进行拆分,拆分为互相独立的部分,将每个独立部分下发到多个Worker并行执行,向各worker进程发送信号,监控worker进程的运行状态,当worker进程退出后(异常情况下),会自动重新启动新的worker进程。
    Worker主要进行实际逻辑计算,并将结果返回给Master。多个worker进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。一个请求,只可能在一个worker进程中处理,一个worker进程,不可能处理其它进程的请求。worker进程的个数是可以设置的,一般我们会设置与机器cpu核数一致。更多的worker数,只会导致进程来竞争cpu资源了,从而带来不必要的上下文切换。
    每个worker进程都是从master进程fork过来。在master进程里面,先建立好需要listen的socket之后,然后再fork出多个worker进程,这样每个worker进程都可以去accept这个socket(当然不是同一个socket,只是每个进程的这个socket会监控在同一个ip地址与端口,这个在网络协议里面是允许的)。一般来说,当一个连接进来后,所有在accept在这个socket上面的进程,都会收到通知,而只有一个进程可以accept这个连接,其它的则accept失败。
    下面我个人采用Nginx实现压缩以及缓存静态资源文件,来提高服务器的请求效率,配置文件如下,具体的都在注释中体现:


[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre name="code" class="html">#user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     include       mime.types;  
  18.     default_type  application/octet-stream;  
  19.   
  20.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.     #                  '$status $body_bytes_sent "$http_referer" '  
  22.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  23.   
  24.     #access_log  logs/access.log  main;  
  25.   
  26.     sendfile        on;  
  27.     #tcp_nopush     on;  
  28.   
  29.     #keepalive_timeout  0;  
  30.     keepalive_timeout  65;  
  31.       
  32.     ##缓存cache参数配置##  
  33.     proxy_connect_timeout 5;  
  34.     proxy_read_timeout 60;  
  35.     proxy_send_timeout 5;  
  36.     proxy_buffer_size 16k;  
  37.     proxy_buffers 4 64k;  
  38.     proxy_busy_buffers_size 128k;  
  39.     proxy_temp_file_write_size 128k;  
  40.     #缓存到nginx的本地目录  
  41.     proxy_temp_path  /Users/heyongjian/Desktop/heyongjian/FSvcWeb/nginx/temp/;  
  42.     proxy_cache_path /Users/heyongjian/Desktop/heyongjian/FSvcWeb/nginx/temp/cache_temp levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;  
  43.     ##end##  
  44.       
  45.     #压缩配置#  
  46.     gzip  on;           #打开gzip压缩功能  
  47.     gzip_min_length 1k; #压缩阈值  
  48.     gzip_buffers 4 16k; #buffer 不用修改  
  49.     gzip_comp_level 2;  #压缩级别:1-10,数字越大压缩的越好,时间也越长  
  50.     gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;  #        压缩文件类型  
  51.     gzip_vary off;      #跟Squid等缓存服务有关,on的话会在Header里增加 "Vary: Accept-Encoding"  
  52.     gzip_disable "MSIE [1-6]\.";  #IE1-6版本不支持gzip压缩  
  53.   
  54.     #具体的服务器地址及端口号,该处可以配置集群实现负载均衡,cluster为服务器集群名  
  55.     upstream cluster{  
  56.         server 10.16.39.12:8080; #并且可以分配权重weight,这样来配置集群服务器的访问优先权  
  57.         #...  
  58.     }  
  59.       
  60.       
  61.     server {  
  62.         listen       80;  
  63.         #nginx服务器,即代理服务器名  
  64.         server_name   localhost;  
  65.   
  66.         rewrite ^(.*)$ https://$host$1 permanent; #这里是http跳转https  
  67.         #charset koi8-r;  
  68.   
  69.         #access_log  logs/host.access.log  main;  
  70.   
  71.          
  72.          #缓存相应的文件(静态文件)  
  73.          location ~ \.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {  
  74.              proxy_pass http://cluster;         #如果没有缓存则通过proxy_pass转向请求  
  75.              proxy_redirect off;  
  76.              proxy_set_header Host $host;  
  77.              proxy_cache cache_one;  
  78.              proxy_cache_valid 200 302 1h;                              #对不同的HTTP状态码设置不同的缓存时间,h小时,d天数  
  79.              proxy_cache_valid 301 1d;  
  80.              proxy_cache_valid any 1m;  
  81.              expires 30d;  
  82.        }  
  83.           
  84.         #动态请求代理给相应服务器  
  85.         location / {  
  86.             proxy_pass http://cluster;   
  87.             proxy_redirect    off;  
  88.             proxy_set_header Host  $host;  
  89.             proxy_set_header X-Forwarded-For  $remote_addr;   
  90.        }  
  91.          
  92.        #purge插件缓存清理  
  93.        location ~ /purge(/.*) {  
  94.                allow              127.0.0.1;        #能够清除缓存的服务器IP地址  
  95.                #allow             10.16.39.12;  
  96.                deny               all;  
  97.                proxy_cache_purge  cache_one $1$is_args$args;  
  98.         }  
  99.         #error_page  404              /404.html;  
  100.   
  101.         # redirect server error pages to the static page /50x.html  
  102.         #  
  103.         error_page   500 502 503 504  /50x.html;  
  104.         location = /50x.html {  
  105.             root   html;  
  106.         }  
  107.   
  108.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  109.         #  
  110.         #location ~ \.php$ {  
  111.         #    proxy_pass   http://127.0.0.1;  
  112.         #}  
  113.   
  114.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  115.         #  
  116.         #location ~ \.php$ {  
  117.         #    root           html;  
  118.         #    fastcgi_pass   127.0.0.1:9000;  
  119.         #    fastcgi_index  index.php;  
  120.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  121.         #    include        fastcgi_params;  
  122.         #}  
  123.   
  124.         # deny access to .htaccess files, if Apache's document root  
  125.         # concurs with nginx's one  
  126.         #  
  127.         #location ~ /\.ht {  
  128.         #    deny  all;  
  129.         #}  
  130.     }  
  131.   
  132.   
  133.     # another virtual host using mix of IP-, name-, and port-based configuration  
  134.     #  
  135.     #server {  
  136.     #    listen       8000;  
  137.     #    listen       somename:8080;  
  138.     #    server_name  somename  alias  another.alias;  
  139.   
  140.     #    location / {  
  141.     #        root   html;  
  142.     #        index  index.html index.htm;  
  143.     #    }  
  144.     #}  
  145.   
  146.       
  147.     #代理http跳转https这块配置  
  148.     # HTTPS server  
  149.     #  
  150.     server {  
  151.         listen       443 ssl;  
  152.         server_name  localhost;  
  153.           ssl on;  
  154.   
  155.         ### SSL log files ###    
  156.         access_log      logs/ssl-access.log;    
  157.         error_log       logs/ssl-error.log;  
  158.           
  159.         ### SSL cert files ###    
  160.         ssl_certificate      ../../cert/server.crt;    
  161.         ssl_certificate_key  ../../cert/server.key;  
  162.   
  163.         #ssl_session_cache    shared:SSL:1m;  
  164.         #ssl_session_timeout  5m;  
  165.   
  166.         #ssl_ciphers  HIGH:!aNULL:!MD5;  
  167.         #ssl_prefer_server_ciphers  on;  
  168.   
  169.         location / {  
  170.                 proxy_pass http://cluster;   
  171.                 ### force timeouts if one of backend is died ##    
  172.                 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;    
  173.      
  174.                 ### Set headers ####    
  175.                 proxy_set_header Host $host;    
  176.                 proxy_set_header X-Real-IP $remote_addr;    
  177.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
  178.      
  179.                 ### Most PHP, Python, Rails, Java App can use this header ###    
  180.                 proxy_set_header X-Forwarded-Proto https;    
  181.      
  182.                 ### By default we don't want to redirect it ####    
  183.                 proxy_redirect     off;    
  184.         }  
  185.     }  
  186.   
  187. }  
这样配置以后:


1.http会直接跳转https;

2.访问web应用时,静态文件会被压缩,而且能压缩到原来的30%,效果很明显;

3.第一次访问web应用时,静态文件会被缓存到指定的Nginx本地目录,再次访问时,就不会需要再次请求服务器;

4.使用ngx_cache_purge(https://github.com/FRiCKLE/ngx_cache_purge)模块进行缓存清理,例如:www.wolfdream.cn/purge/xxx.jpg 就可以清除xxx图片缓存了,另外超时的缓存Nginx会自动删除。

通过Nginx进行调优,可以使访问效率提高很多,上述若有错误,欢迎拍砖指出!


以上参考博文:http://www.open-open.com/lib/view/open1417488526633.html 

                            http://blog.csdn.NET/zhaoyue007101/article/details/42144459

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号