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


nginx做為反向代理時,能夠將來自upstream的響應緩存至本地,並在后續的客戶端請求同樣內容時直接從本地構造響應報文。

nginx的緩存數據結構:

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

磁盤空間:存儲數據

 

  • 用法:

Syntax:

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];

Default:

Context:

http

 

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"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM