Build nginx Image Server Part-i install Nginx
- Installing Pcre
- Download Ngx_cache_purge and unzip to clear the cache
- Download Nginx and Unzip
- CD nginx-1.7.7
- Compile,--prefix use the default value, then Nginx installed in/usr/local/nginx
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0 --with-http_stub_status_module --with-http_ssl_modulemake && make install
PART-II Configuration
Vim/usr/local/nginx/conf/nginx.conf, and edit as follows:
User www www; worker_processes8; Error_log/data3/nginx/error.log Crit;#error_log Logs/error.log;#error_log Logs/error.log Notice;#error_log Logs/error.log Info;pid/usr/local/nginx/nginx.pid; Events {Use Epoll; Worker_connections65535;} HTTP {include Mime.types; Default_type Application/octet-stream; CharSet utf-8; Server_names_hash_bucket_size128; Client_header_buffer_size32k; Large_client_header_buffers432k; Client_max_body_size300m; SendfileOn; Tcp_nopushOn; Keepalive_timeout60; Tcp_nodelayOn; Client_body_buffer_size512k; Proxy_connect_timeout5; Proxy_read_timeout60; Proxy_send_timeout5; Proxy_buffer_size16k; Proxy_buffers464k; Proxy_busy_buffers_size128k; Proxy_temp_file_write_size128k; GzipOn; Gzip_min_length1k; Gzip_buffers426H; Gzip_http_version1.1; Gzip_comp_level2; Gzip_types Text/plainapplication/x-javascript text/css Application/xml; Gzip_varyOn;# Proxy_temp_path and Proxy_cache_path must be in the same partition Proxy_temp_path/data0/proxy_temp_dir;# set the Web cache name to Cahche_one, the memory cache space size is 200m,1 days without the content being accessed automatically clears the hard disk buffer space size to 300G Proxy_cache_path/data0/proxy_cache_dir levels=1:2 Keys_zone=cache_one:200m inactive=1d max_size=30g;# upstream backend_server{# server 192.168.1.121:80 weight=1 max_fail=2 fail_timeout=30s;# server 192.168.1.122:80 weight=1 max_fail=2 fail_timeout=30s;# server 192.168.1.123:80 weight=1 max_fail=2 fail_timeout=30s;# }#以下为缓存服务器 Log_format Cache ' * * *$time _local \ n ' "$upstream _cache_status \ n ' "$remote _addr,$http _x_forwarded_for \ n ' cache-control:$upstream _http_cache_control \ n ' Expires:$upstream _http_expires \ n ' ""$request "($status) \ n ""$http _user_agent "\ n"; server {Listen80; server_name192.168.1.120; Location/{Proxy_cache Cache_one;# set different cache times for different HTTP status codes proxy_cache_valid20030412h;#以域名, URI, parameters are combined into the Web server's key value, Ngnix the cache contents to the level two cache directory based on the key value hash Proxy_cache_key$host$uri$is _args$args; Proxy_set_header Host$host; Proxy_set_header x-forwarded-for$remote _addr;#此处跳转到真实图片服务器 Proxy_pass/HTTP/192.168.1.120:8080; Access_log/data3/nginx/cache.log Cache; Expires1d; } location ~/purge (/.*) {#设置只允许指定的ip或ip段才可以清除url缓存#allow 127.0.0.1;#allow 192.168.0.0/16;#deny all; Proxy_cache_purge Cache_one$host$$is _args$args; }# #扩展名为. PHP,. JSP,. Cig End of dynamic applications do not cache# location ~.*\. (php|jsp|cgi)? $# {# Proxy_set_header Host $host;# Proxy_set_header x-forwarded-for $remote _addr;# Proxy_pass Http://backend_server;#} Access_logOff;#error_page 404/404.html;# REDIRECT Server error pages to the static page/50x.html# Error_page500502503504/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;#} }#真实的图片服务器 server{Listen8080; server_name localhost; Location/{root/data0/images/; }#访问日志, usually off Access_log/data3/nginx/access.log combined; }# 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 SSL; # server_name localhost; # ssl_certificate Cert.pem; # ssl_certificate_key Cert.key; # Ssl_session_cache shared:ssl:1m; # ssl_session_timeout 5m; # ssl_ciphers high:!anull:! MD5; # ssl_prefer_server_ciphers on; # location/{# root html; # index index.html index.htm; #} #}}
To this, Nginx Image service area is set up. Under/data0/images/put a picture 200.jpg test access http://192.168.1.120/200.jpg, Cache_log record as follows:
***12/Nov/2014:16:15:26 +0800 MISS 192.168.1.19, - Cache-Control: - Expires: - "GET /200.jpg HTTP/1.1"(200) "Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0" ***12/Nov/2014:16:15:38 +0800 HIT 192.168.1.19, - Cache-Control: - Expires: - "GET /200.jpg HTTP/1.1"(200) "Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0"
Access Http://192.168.1.120/purge/200.jpg Clear Cache
Build Nginx Image Server