1、圖片防盜鏈javascript
簡單便是最好,最簡單的方法是用ngx_http_referer_mmodule模塊的valid_referers參數來構建,這也是最多見和最經常使用的方法:php
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.htmlcss
2、圖片緩存html
利用nginx的ngx_cache_purge模塊和ngx_http_proxy_module模塊進行構建;java
ngx_cache_purge模塊: http://wiki.nginx.org/CachePurgeChsnode
ngx_http_proxy_module模塊: http://nginx.org/en/docs/http/ngx_http_proxy_module.htmlnginx
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; } }
3、圖片壓縮web
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了