Nginx 그림 도 난 방지 체인, 캐 시, 압축 예제

1. 그림 도 난 방지 체인
   간단 하면 가장 좋 고, 가장 간단 한 방법 은 ngxhttp_referer_mmodule 모듈 의 validreferers 매개 변 수 를 구축 하 는 것 도 가장 흔 하고 자주 사용 하 는 방법 입 니 다.
location ~* \.(gif|jpg|jpeg|png|bmp|swf)$
      {
        valid_referers none blocked www.test.com test.com;
        if ($invalid_referer){
          rewrite ^/ http://www.test.com/return.html;
          #return 403;
          }
        expires 15d;
       }

ngx_http_referer_mmodule 모듈 참조:http://nginx.org/en/docs/http/ngx_http_referer_module.html
그림 캐 시
nginx 를 이용 한 ngxcache_purge 모듈 과 ngxhttp_proxy_module 모듈 구축;
ngx_cache_purge 모듈:http://wiki.nginx.org/CachePurgeChs
ngx_http_proxy_모듈 모듈: http://nginx.org/en/docs/http/ngx_http_proxy_module.html
user  nginx;
worker_processes 8;
error_log  /usr/local/webserver/nginx/logs/nginx_error.log  crit;
pid        /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by thisprocess.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
 
http
{
  include       mime.types;
  default_type  application/octet-stream;
  charset  utf-8;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 300m;
  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 60;
  tcp_nodelay on;
  client_body_buffer_size  512k;
  proxy_connect_timeout    5;
  proxy_read_timeout       60;
  proxy_send_timeout       5;
  proxy_buffer_size        16k;
  proxy_buffers            4 64k;
  proxy_busy_buffers_size 128k;
  proxy_temp_file_write_size 128k;
  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types       text/plainapplication/x-javascript text/css application/xml;
  gzip_vary on;
  # :proxy_temp_path proxy_cache_path            
  proxy_temp_path   /data0/proxy_temp_dir;
  #  Web      cache_one,         200MB,1             ,         5GB。
  proxy_cache_path  /data0/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=5g;
  
  server #        
  {
    listen       80;
    server_name  your_server_ip;
 
    location /
    {
         proxy_cache cache_one;
         #    HTTP            
         proxy_cache_valid  200 304 12h;
         #   、URI、     Web   Key ,Nginx  Key   ,              
         proxy_cache_key  $host$uri$is_args$args;
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://your_server_ip:8080; #             
         log_format cache '***$time_local '
                    '$upstream_cache_status '
                    'Cache-Control: $upstream_http_cache_control '
                    'Expires: $upstream_http_expires '
                    '"$request" ($status) '
                    '"$http_user_agent" '; #      (         hit miss ,         ,       ,     ,        )
                  access_log  /var/log/nginx/cache.log cache; #        
                  expires      1d;
    }
    #      ,    URL http://192.168.8.42/test.txt,    http://192.168.8.42/purge/test.txt      URL   。
    location ~ /purge(/.*)
    {
     #        IP IP      URL  。
     allow            127.0.0.1;
     deny            all;
     proxy_cache_purge   cache_one$host$1$is_args$args;
    }
    #    .php、.jsp、.cgi            。
    location ~ .*\.(php|jsp|cgi)?$
    {
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
    } 
    access_log  off;
  } 
#        
server
{
  listen 8080; 
  server_name your_server_ip; 
  location /
  {
    root /;
  }
  access_log /usr/local/webserver/nginx/logs/nginx_access.log;
}
}


ngx_cache_purge : http://nginx.org/en/docs/http/ngx_http_gzip_module.html

gzip on;  
gzip_min_length  1k;  
gzip_buffers     4 16k;  
gzip_http_version 1.0;  
gzip_comp_level 2;  
gzip_types  text/plain application/x-javascript text/css application/xml application/x-httpd-php p_w_picpath/jpeg p_w_picpath/gif p_w_picpath/png;  
gzip_vary on; 
gzip_disable "MSIE [1-6]\.";

1 : Gzip

2 : , 1K ,

3 :buffer, , , ,

4 : , HTTP/1.0, ; , HTTP/1.1

5 : ,1-10, , ,

6 : , ,JavaScript , , js ,

7 : Squid ,on Header "Vary: Accept-Encoding", ,

8 :IE6 Gzip , Gzip


좋은 웹페이지 즐겨찾기