Nginx緩存功能、防盜鏈、URL重寫

nginx作爲反向代理時,可以未來自upstream的響應緩存至本地,並在後續的客戶端請求一樣內容時直接從本地構造響應報文。

javascript

nginx的緩存數據結構:php

共享內存:存儲鍵和緩存對象元數據css

磁盤空間:存儲數據html

 

  • 用法:

Syntax: java

proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];nginx

Default: web

正則表達式

Context: 算法

httpapache

 

proxy_cache zone|off:定義一個用於緩存的共享內存區域,其可被多個地方調用;緩存將聽從upstream服務器的響應報文首部中關於緩存的設定,如 "Expires"、"Cache-Control: no-cache"、 "Cache-Control: max-age=XXX"、"private"和"no-store" 等,但nginx在緩存時不會考慮響應報文的"Vary"首部。爲了確保私有信息不被緩存,全部關於用戶的私有信息能夠upstream上經過"no-cache" or "max-age=0"來實現,也可在nginx設定proxy_cache_key必須包含用戶特有數據如$cookie_xxx的方式實現,但最後這種方式在公共緩存上使用可能會有風險。所以,在響應報文中含有如下首部或指定標誌的報文將不會被緩存。
    Set-Cookie
    Cache-Control containing "no-cache", "no-store", "private", or a "max-age" with a non-numeric or 0 value
    Expires with a time in the past
    X-Accel-Expires: 0


proxy_cache_key:設定在存儲及檢索緩存時用於「鍵」的字符串,可使用變量爲其值,但使用不當時有可能會爲同一個內容緩存屢次;另外,將用戶私有信息用於鍵能夠避免將用戶的私有信息返回給其它用戶;
proxy_cache_lock:啓用此項,可在緩存未命令中阻止多個相同的請求同時發往upstream,其生效範圍爲worker級別;
proxy_cache_lock_timeout:proxy_cache_lock功能的鎖定時長;
proxy_cache_min_uses:某響應報文被緩存以前至少應該被請求的次數;
proxy_cache_path:定義一個用記保存緩存響應報文的目錄,及一個保存緩存對象的鍵及響應元數據的共享內存區域(keys_zone=name:size),其可選參數有:
    levels:每級子目錄名稱的長度,有效值爲1或2,每級之間使用冒號分隔,最多爲3級;
    inactive:非活動緩存項從緩存中剔除以前的最大緩存時長;
    max_size:緩存空間大小的上限,當須要緩存的對象超出此空間限定時,緩存管理器將基於LRU算法對其進行清理;
    loader_files:緩存加載器(cache_loader)的每次工做過程最多爲多少個文件加載元數據;
    loader_sleep:緩存加載器的每次迭代工做以後的睡眠時長;
    loader_threashold:緩存加載器的最大睡眠時長;
    例如:  proxy_cache_path  /data/nginx/cache/one    levels=1      keys_zone=one:10m;
            proxy_cache_path  /data/nginx/cache/two    levels=2:2    keys_zone=two:100m;
            proxy_cache_path  /data/nginx/cache/three  levels=1:1:2  keys_zone=three:1000m;
proxy_cache_use_stale:在沒法聯繫到upstream服務器時的哪一種情形下(如error、timeout或http_500等)讓nginx使用本地緩存的過時的緩存對象直接響應客戶端請求;其格式爲:
    proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_404 | off
proxy_cache_valid [ code ...] time:用於爲不一樣的響應設定不一樣時長的有效緩存時長,例如:proxy_cache_valid  200 302  10m;
proxy_cache_methods [GET HEAD POST]:爲哪些請求方法啓用緩存功能;
proxy_cache_bypass string:設定在哪一種情形下,nginx將不從緩存中取數據;例如:
     proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
     proxy_cache_bypass $http_pragma $http_authorization;

http {
    proxy_cache_path  /data/nginx/cache  levels=1:2    keys_zone=STATIC:10m
                                         inactive=24h  max_size=1g;
    server {
        location / {
            proxy_pass             http://www.magedu.com;
            proxy_set_header       Host $host;
            proxy_cache            STATIC;
            proxy_cache_valid      200  1d;
            proxy_cache_valid       301 302 10m;
            proxy_cache_vaild        any 1m;
            proxy_cache_use_stale  error timeout invalid_header updating
                                   http_500 http_502 http_503 http_504;
        }
    }
}
  • 壓縮


nginx將響應報文發送至客戶端以前能夠啓用壓縮功能,這可以有效地節約帶寬,並提升響應至客戶端的速度。一般編譯nginx默認會附帶gzip壓縮的功能,所以,能夠直接啓用之。

http {
    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;
    gzip_disable msie6;
}

gzip_proxied指令能夠定義對客戶端請求哪類對象啓用壓縮功能,如「expired」表示對因爲使用了expire首部定義而沒法緩存的對象啓用壓縮功能,其它可接受的值還有「no-cache」、「no-store」、「private」、「no_last_modified」、「no_etag」和「auth」等,而「off」則表示關閉壓縮功能。

  • 配置示例

反向代理啓用upstream和緩存:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
 
    proxy_cache_path /nginx/cache/first  levels=1:2   keys_zone=first:10m max_size=512m;
 
    upstream websrv {
        server 172.16.100.11 weight=1;
        server 172.16.100.12 weight=1;
        server 127.0.0.1:8080 backup;
    }
    server {
        listen       80;
        server_name  www.magedu.com;

        add_header X-Via $server_addr;
        add_header X-Cache-Status $upstream_cache_status;

        location / {
            proxy_pass http://websrv;
            proxy_cache first;
            proxy_cache_valid 200 1d;
            proxy_cache_valid 301 302 10m;
            proxy_cache_valid any 1m;
            index  index.html index.htm;

            if ($request_method ~* "PUT") {
                proxy_pass http://172.16.100.12;
                break;
            }
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen 8080;
        server_name localhost;
        root /nginx/htdocs;
        index index.html;
    }
}
加入頭信息:
        add_header X-Via $server_addr;
        add_header X-Cache-Status $upstream_cache_status;
配置緩存:
proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:10m max_size=512m;

 啓用:

            proxy_cache first;
            proxy_cache_valid 200 1d;
            proxy_cache_valid 301 302 10m;
            proxy_cache_valid any 1m;
  • 啓用Nginx日誌緩存:

設定錯誤日誌格式及級別:

http {
log_format combined '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $body_bytes_sent '
        '"$http_referer" "$http_user_agent"';
access_log /var/log/nginx/access.log combined;
error_log /var/log/nginx/error.log crit;
...
}

記錄相似apache格式的日誌:

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 /var/log/nginx/access.log main;


啓用日誌緩存:

http {
  ...
  open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
  ...
}
  • URL重寫

實現域名跳轉

server
{
listen 80;
server_name jump.magedu.com;
index index.html index.php;
root /www/htdocs;
rewrite ^/ http://www.magedu.com/;
}

實現域名鏡像

server
{
listen 80;
server_name mirror.magedu.com;
index index.html index.php;
root /www/htdocs;
rewrite ^/(.*)$ http://www.magedu.com/$1 last;
}
  • 防盜鏈功能

簡單的防盜鏈配置:

location ~* \.(gif|jpg|png|swf|flv)$ {
  valid_referers none blocked www.magedu.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.magedu.com/403.html;
    # return 404
  }
}

第一行:gif|jpg|png|swf|flv
表示對gif、jpg、png、swf、flv後綴的文件實行防盜鏈
第二行:www.magedu.com
表示對www.magedu.com這個來路進行判斷if{}裏面內容的意思是,若是來路不是指定來路就跳轉到錯誤頁面,固然直接返回404也是能夠的。

  • if語句中的判斷條件

正則表達式匹配:
    ~:與指定正則表達式模式匹配時返回「真」,判斷匹配與否時區分字符大小寫;
    ~*:與指定正則表達式模式匹配時返回「真」,判斷匹配與否時不區分字符大小寫;
    !~:與指定正則表達式模式不匹配時返回「真」,判斷匹配與否時區分字符大小寫;
    !~*:與指定正則表達式模式不匹配時返回「真」,判斷匹配與否時不區分字符大小寫;

文件及目錄匹配判斷:
    -f, !-f:判斷指定的路徑是否爲存在且爲文件;
    -d, !-d:判斷指定的路徑是否爲存在且爲目錄;
    -e, !-e:判斷指定的路徑是否存在,文件或目錄都可;
    -x, !-x:判斷指定路徑的文件是否存在且可執行;

 

  • if設定限速

爲某個特定路徑限速:

server {
    server_name www.magedu.com;

    location /downloads/ {
        limit_rate 20k;
        root /web/downloads/;
    }
    ..
}

限制搜索引擎的bot速度:

if ($http_user_agent ~ Google|Yahoo|MSN|baidu) {
    limit_rate 20k;
}
  • nginx經常使用的全局變量

下面是nginx經常使用的全局變量中的一部分,它們常常用於if語句中實現條件判斷。
$arg_PARAMETER        This variable contains the value of the GET request variable PARAMETER if present in the query string.$args                 This variable contains the query string in the URL, for example foo=123&bar=blahblah if the URL is http://example1. com/? foo=123&bar=blahblah$binary_remote_addr   The address of the client in binary form.$body_bytes_sent      The bytes of the body sent.$content_length       This variable is equal to line Content-Length in the header of request.$content_type         This variable is equal to line Content-Type in the header of request.$document_root        This variable is equal to the value of directive root for the current request.$document_uri         The same as $uri.$host                 This variable contains the value of the 'Host' value in the request header, or the name of the server processing if the 'Host' value is not available.$http_HEADER          The value of the HTTP header HEADER when converted to lowercase and with "dashes" converted to "underscores", for example, $http_user_agent, $http_referer.$is_args              Evaluates to "?" if $args is set, returns "" otherwise.$request_uri          This variable is equal to the *original* request URI as received from the client including the args. It cannot be modified. Look at $uri for the post-rewrite/altered URI. Does not include host name. Example:            "/foo/bar.php?arg=baz".$scheme               The HTTP scheme (that is http, https). Evaluated only on demand, for example: rewrite ^(.+)$ $scheme://example.com$1 redirect;$server_addr          This variable contains the server address. It is advisable to indicate addresses correctly in the listen directive and use the bind parameter so that a system call is not made every time this variable is accessed.$server_name          The name of the server.$server_port          This variable is equal to the port of the server, to which the request arrived.$server_protocol      This variable is equal to the protocol of request, usually this is HTTP/1.0 or HTTP/1.1.$uri                  This variable is equal to current URI in the request (without arguments, those are in $args.) It can differ from $request_uri which is what is sent by the browser. Examples of how it can be modified are internal redirects, or with the use of index. Does not include host name. Example: "/foo/bar.html"

相關文章
相關標籤/搜索