nginx的高級用法


一、根據url中的參數來確定緩存的key

set_by_lua_block $dataArg {
local enc = ngx.req.get_uri_args()["enc"]
local key = ngx.req.get_uri_args()["key"]
local name = ngx.req.get_uri_args()["name"]
local str = tostring(enc)..tostring(key)..tostring(name)
return str
}
proxy_cache_key $host$uri$dataArg;

二、根據源站傳過來的跨域頭做判斷

set $cors_origin "*";
if ($http_origin != ""){
set $cors_origin $http_origin;
}
more_set_headers "Access-Control-Allow-Origin:$cors_origin";

三、根據不同的運營商走不同的upstream

1、下層傳遞一個請求頭:
 location / {
             proxy_buffer_size  128k;
             proxy_buffers   32 32k;
             proxy_busy_buffers_size 128k;
             proxy_set_header  Host $host;
             proxy_set_header CDN ctyun;
             proxy_set_header X-Forwarded-For $remote_addr;
             proxy_set_header isp cm;   #傳遞一個請求頭為cm(移動)
             add_header Powered-By-ctcdn "$server_addr";
             proxy_cache $cache_store;
             proxy_pass $scheme://test-upstream_$scheme$server_port;
    }

2、上層做判斷
server {
    listen 80;
    server_name www.test.com;
    resolver 8.8.8.8 114.114.114.114;
    underscores_in_headers on; #nginx開啟客戶讀取自定義頭部的值
 
    error_log  /data/log/nginx/error.log;
    access_log /data/log/nginx/access.log nginxlog;

    location / {
             proxy_buffer_size  128k;
             proxy_buffers   32 32k;
             proxy_busy_buffers_size 128k;
             proxy_set_header  Host $host;
             proxy_set_header CDN test;
             add_header Powered "$server_addr";
             proxy_cache $cache_store;

   ##此處即下層如果有移動聯通電信設備;在上層需要移動回移動的upstream;聯通回聯通的upstream;電信回電信的upstream;
            if ($http_isp = "ct") {
               proxy_pass $scheme://test-ct_$scheme$server_port;
        }
            if ($http_isp = "cn") {
               proxy_pass $scheme://test-cn_$scheme$server_port;
        }
            if ($http_isp = "cm") {
               proxy_pass $scheme://test-cm_$scheme$server_port;
        } 
    } 

3、上層upstream配置
upstream test-cm_http80 {
        server 1.1.1.1:80;
    }

upstream test-cn_http80 {
        server 2.2.2.2:80;
    }

upstream test-ct_http80 {
        server 3.3.3.3:80;
    }

四、proxy_cache_use_stale模塊的作用

緩存過期,但是源站有問題,可以直接返回用戶舊的內容,返回舊文件總比報錯強,一般應用在源站有問題的情況;
Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...;
Default:  proxy_cache_use_stale off;
Context:  http, server, location
官網介紹:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_use_stale
測試過程:
upstream test_http80 {
    server 1.1.1.1:8085;
}
server {
    listen       80;
    server_name  www.test.com;
    resolver 8.8.8.8 114.114.114.114;
    proxy_cache $cache_store;
    error_log  /log/error.log;
    access_log /log/access.log nginxlog;

    location ~*\.(txt|jpg|gif|png|bmp|ico)$ {
        more_clear_headers -s '200 206 304' 'Set-Cookie' 'Server' 'X-Varnish' 'x-hits' 'X-Cache' 'Via' 'Age';
        proxy_ignore_headers   "Cache-Control" "Set-Cookie" "Expires" "Vary";
             proxy_cache_valid  200 206 5s;
             proxy_cache_key $host$uri;
             proxy_set_header Host $host;
             proxy_set_header X-Forwarded-For $remote_addr;
             proxy_set_header CDN test;
             age_header;
             expires  5s;
             proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
             proxy_http_version 1.1;
             proxy_set_header Connection "keep-alive";
             add_header Powere-test "$upstream_cache_status from $ipadd";
             proxy_pass "${scheme}://test_${scheme}${server_port}";
    }
}

最終效果:當上游服務器1.1.1.1無法訪問時,返回舊內容;

 五、當訪問資源404時,對url做改寫

server {
    listen 80;
    server_name www.zhide666.cn;

    location / {
        proxy_pass http://www.upstream.com;
    }
#當資源不在本地,但是訪問該資源的時候資源不存在,當上游服務器返回404狀態碼時,也需要通過error_page rewrite到指定的url;
    location ~/portal/(.*)$ {
        proxy_intercept_errors on;  #當error_page和proxy_proxy_pass同時存在時則需要加該配置;
        #recursive_error_pages on;
        proxy_pass http://www.upstream.com;
        error_page 404 https://www.zhide666.cn/ketang/$1$is_args$args;  #不加is_args和args會忽略url中的參數;
    }
#當資源在本地,但是訪問該資源的時候資源不存在,則通過error_page rewrite到指定的url;
    location ~/movie/(.*)$ {
        root /data/movie/;
        error_page 404 https://www.zhide666.cn/ketang/$1$is_args$args;
    }
}

 


免責聲明!

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



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