Nginx自動回源配置


1.背景

為了解決每次軟件版本更新,導致辦公網公網帶寬緊張,通過Nginx回源至本地機房,域名解析采用內外網單獨解析,外地辦公同事可以通過CDN進行更新,公司內部同事通過內網DNS解析獲取本地資源更新

nginx常見的回源方式nginx proxy_store或proxy_cache模塊

2.proxy_store方式回源

以下是Nginx回源配置:

server {
        listen       80;
        server_name  內網訪問域名;
        location / {
            expires -1;
            proxy_set_header Accept-Encoding '';
            root /data/www;
            proxy_store on;
            proxy_store_access user:rw group:rw all:rw;
            proxy_temp_path /data/tmp;
            if ( !-e $request_filename)  {
                 proxy_pass 雲對象存儲地址;
            }
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /ngx_stat {
            stub_status on;
            access_log off;
        }
}

備注:為什么使用對象存儲地址而不是CDN地址,是由於上傳至對象存儲的文件還不能及時同步到各個CDN節點,導致CDN上有很多請求報錯日志

3.proxy_cache方式回源

參考鏈接:http://www.osyum.com/article/show/176/

以下是Nginx回源配置:

重點關注proxy_cache配置

#nginx 主配置文件
vim nginx.conf
#定義nginx運行的用戶和用戶組
user  app;
#啟動進程,通常設置成和CPU的數量相等
worker_processes  auto;
#改指令是當義工nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)
#與nginx進程數相除,但是nginx分配請求並不是那么均勻,所以最好與ulimit -n的值保持一致
worker_rlimit_nofile 65535;
events {
    #單個后台worker process進程的最大並發連接數(最大連接數=連接數*進程數)
    worker_connections  16383;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #訪問日志格式,其中X-B3-TraceId參數是鏈路跟蹤參數
    log_format json '{"@timestamp":"$time_iso8601",'
                 '"host":"$server_addr",'
                 '"clientip":"$remote_addr",'
                 '"size":$body_bytes_sent,'
                 '"responsetime":$request_time,'
                 '"upstreamtime":"$upstream_response_time",'
                 '"upstreamhost":"$upstream_addr",'
                 '"http_host":"$host",'
                 '"url":"$uri",'
                 '"xff":"$http_x_forwarded_for",'
                 '"referer":"$http_referer",'
                 '"agent":"$http_user_agent",'
                 '"X-B3-TraceId":"$http_X_B3_TraceId",'
                 '"Content-Length":"$http_Content_Length",'
                 '"appkey":"$http_appkey",'
                 '"method":"$http_method",'
                 '"status":"$status",'
                 '"ups_status":$upstream_status}';
    #全局訪問日志,采用json日志格式
    access_log  /data/logs/nginx/access.log  json;
    #全局錯誤日志
    #錯誤日志定義等級,默認error級別,[ debug | info | notice | warn | error | crit ]
    error_log   /data/logs/nginx/error.log;
    #sendfile指令制定nginx是否調用sendfile函數(zero copy方式)來輸出文件
    #對於普通應用必須設為on
    #如果用來進行下載等應用磁盤I/O重負載應用,可設置為off,以平衡磁盤與網絡I/O處理速度
    #降低系統的uptime
    sendfile        on;
    #防止網絡阻塞
    tcp_nopush     on;
    #提高數據的實時響應性
    tcp_nodelay    on;
    #隱藏nginx版本號
    server_tokens off;
    #keepalive超時時間,客戶端到服務器端的連接持續有效時間,當出現對服務器的后端請求時,
    #keepalive-timeout功能可避免建立或重新建立連接
    keepalive_timeout  65;
    #定義讀取客戶端請求標頭的超時。如果客戶端在此時間內未傳輸整個標頭,
    #則請求將以408(請求超時)錯誤終止
    client_header_timeout 15;
    #定義讀取客戶端請求正文的超時。如果客戶端在此時間內未傳輸任何內容,
    #則請求會因408(請求超時)錯誤終止
    client_body_timeout 15;
    #后端服務器數據回傳時間(代理發送超時)
    send_timeout 25;
    client_header_buffer_size 4096K;
    #允許客戶端請求的最大單文件字節數
    client_max_body_size 10m;
    proxy_cache_path  /data/nginx/proxy_cache/cache levels=1:2 keys_zone=downloadcache:600m max_size=400g inactive=48h use_temp_path=on;
    proxy_temp_path    /data/nginx/proxy_cache/temp;   
    proxy_cache_key    $host$request_uri;
    #開啟gzip壓縮
    #gzip  on;
    #gzip_min_length 1k;
    #gzip_buffers 4 16k;
    #壓縮級別大小,最大為9,值越小,壓縮后比例越小,CPU處理更快,值越大,消耗CPU比較高
    #gzip_comp_level 2;
    #gzip_types text/plain application/javascript text/css application/xml text/javascript application/json;
    #gzip_vary off;

include /usr/local/nginx/conf/vhost/*.conf;
}

#虛擬主機配置文件
vim default.conf
server {
        listen       443 ssl;
        server_name  域名;
        ssl_certificate /usr/local/nginx/conf/ssl/域名.crt;
        ssl_certificate_key /usr/local/nginx/conf/ssl/域名.key;
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $http_x_forward_for;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header Host $host;
            proxy_cache          downloadcache;
            proxy_cache_valid      200 600s;
            proxy_cache_valid      304 600s;
            proxy_cache_use_stale    invalid_header http_403 http_404 http_500 http_502;
            proxy_cache_lock      on;
            proxy_cache_lock_timeout  5s;
            proxy_pass https://域名;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /ngx_stat {
            stub_status on;
            access_log off;
        }
}

4.大文件回源方法

針對大文件回源可以采用360開源的ngx_http_subrange_module模塊

https://www.cnblogs.com/bugutian/p/4528661.html


免責聲明!

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



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