nginx 緩存服務器配置


1.服務器簡介

Centos 7.4
nginx緩存服務器地址:    192.168.56.28 [root@nginx-cache ~]
nginx前端圖片服務器地址:192.168.56.30 [root@nginx-front ~]

2.nginx緩存服務器編譯參數

[root@nginx-cache ~]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module \
--with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module \
--with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module \
--with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic \
--with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 \
-grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic
' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

 3.nginx圖片緩存服務器nginx配置

[root@nginx-cache ~]# cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    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;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    include /etc/nginx/conf.d/*.conf;

  #為存儲承載從代理服務器接收到的數據的臨時文件定義目錄 proxy_temp_path /etc/nginx/temp_dir;
# /etc/nginx/cache_dir本地路徑,用來設置Nginx緩存資源的存放地址 # levels #默認所有緩存文件都放在同一個/etc/nginx/cache_dir下,但是會影響緩存的性能,因此通常會在/etc/nginx/cache_dir下面建立子目錄用來分別存放不同的文件。假設levels=1:2,Nginx為將要緩存的資源生成的key為
#d628235be0b8e19f5f78c37f5f226819,那么key的最后一位9,以及倒數第2-3位81作為兩級的子目錄,也就是該資源最終會被緩存到/etc/nginx/cache_dir/9/81目錄中 # key_zone #參數用來為這個緩存區起名,500m指內存緩存空間大小為500MB,用來為這個緩存區起數用來為這個緩存區起在共享內存中設置一塊存儲區域來存放緩存的key和metadata(類似使用次數),這樣nginx可以快速判斷一個request是否命中或者未命中緩存,
#1m可以存儲8000個key,10m可以存儲80000個key # max_size #最大cache空間,如果不指定,會使用掉所有disk space,當達到配額后,會刪除最少使用的cache文件 # inactive #未被訪問文件在緩存中保留時間,本配置中如果1天未被訪問則不論狀態是否為expired【時間:h(時)/m(分)/s(秒)/d(天)】,緩存控制程序會刪掉文件。inactive默認是10分鍾。需要注意的是,inactive和expired配置項的含義是不同的,
#expired只是緩存過期,但不會被刪除,inactive是刪除指定時間內未被訪問的緩存文件 # use_temp_path #如果為off,則nginx會將緩存文件直接寫入指定的cache文件中,而不是使用temp_path存儲,official建議為off,避免文件在不同文件系統中不必要的拷貝 proxy_cache_path /etc/nginx/cache_dir levels=1:2 keys_zone=cache_one:500m max_size=1g inactive=1d use_temp_path=off; upstream front_picture { server 192.168.56.30; } server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; proxy_pass http://front_picture; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
# proxy_cache #啟用proxy cache,並指定key_zone。另外,如果proxy_cache off表示關閉掉緩存。
# proxy_cache off;
proxy_cache cache_one; # 使用名稱為cache_one的對應緩存配置(名稱任意命名,但是必須與keys_zone中緩存區域名稱一致) proxy_cache_valid 200 24h; # 指定狀態碼200的緩存時間為24h expires 6h; # 過期時間 proxy_redirect off; # 指定修改被代理服務器返回的響應頭中的location頭域跟refresh頭域數值 proxy_pass http://front_picture; # 指代理后轉發的路徑,注意是否 需要 最后的 /【注意:不走代理的服務,無法進行緩存】 } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } }

4.存放前端圖片服務器nginx配置

[root@nginx-front ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    #server {
    #    listen       443;
    #    root         /usr/share/nginx/html;
    #    ssl_certificate "/etc/pki/nginx/server.crt";
    #    ssl_certificate_key "/etc/pki/nginx/private/server.key";
    #    ssl_session_cache shared:SSL:1m;
    #    ssl_session_timeout  10m;
    #    ssl_ciphers HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers on;
    #    include /etc/nginx/default.d/*.conf;
    #    location / {
    #    }
    #    error_page 404 /404.html;
    #        location = /40x.html {
    #    }
    #    error_page 500 502 503 504 /50x.html;
    #        location = /50x.html {
    #    }
    #}
}

5.存放前端圖片服務器圖片存放位置

[root@nginx-front ~]# ll /usr/share/nginx/html/
total 336
-rw-r--r-- 1 root root  13392 Sep 11 13:23 1.gif
-rw-r--r-- 1 root root   3650 Oct  3  2019 404.html
-rw-r--r-- 1 root root   3693 Oct  3  2019 50x.html
lrwxrwxrwx 1 root root     20 Sep 22 14:40 en-US -> ../../doc/HTML/en-US
drwxr-xr-x 2 root root     27 Sep 22 14:40 icons
lrwxrwxrwx 1 root root     18 Sep 22 14:40 img -> ../../doc/HTML/img
lrwxrwxrwx 1 root root     25 Sep 22 14:40 index.html -> ../../doc/HTML/index.html
-rw-r--r-- 1 root root    368 Oct  3  2019 nginx-logo.png
lrwxrwxrwx 1 root root     14 Sep 22 14:40 poweredby.png -> nginx-logo.png

6.啟動兩個服務器nginx,查看nginx-cache服務器緩存目錄

[root@nginx-front ~]# nginx
[root@nginx-cache ~]# nginx
[root@nginx
-cache ~]# ll /etc/nginx/ total 72 drwx------ 2 nginx root 6 Sep 23 15:01 cache_dir drwxr-xr-x 2 root root 23 Sep 23 10:50 conf.d drwxr-xr-x 2 root root 6 Oct 3 2019 default.d -rw-r--r-- 1 root root 1077 Oct 3 2019 fastcgi.conf -rw-r--r-- 1 root root 1077 Oct 3 2019 fastcgi.conf.default -rw-r--r-- 1 root root 1007 Oct 3 2019 fastcgi_params -rw-r--r-- 1 root root 1007 Oct 3 2019 fastcgi_params.default -rw-r--r-- 1 root root 2837 Oct 3 2019 koi-utf -rw-r--r-- 1 root root 2223 Oct 3 2019 koi-win -rw-r--r-- 1 root root 5231 Oct 3 2019 mime.types -rw-r--r-- 1 root root 5231 Oct 3 2019 mime.types.default -rw-r--r-- 1 root root 1473 Sep 23 15:02 nginx.conf -rw-r--r-- 1 root root 2607 Sep 23 10:50 nginx.conf_bak -rw-r--r-- 1 root root 2656 Oct 3 2019 nginx.conf.default -rw-r--r-- 1 root root 636 Oct 3 2019 scgi_params -rw-r--r-- 1 root root 636 Oct 3 2019 scgi_params.default drwx------ 2 nginx root 6 Sep 23 15:02 temp_dir -rw-r--r-- 1 root root 664 Oct 3 2019 uwsgi_params -rw-r--r-- 1 root root 664 Oct 3 2019 uwsgi_params.default -rw-r--r-- 1 root root 3610 Oct 3 2019 win-utf [root@nginx-cache ~]# ll /etc/nginx/cache_dir/ total 0 [root@nginx-cache ~]# ll /etc/nginx/temp_dir/ total 0

7.瀏覽器訪問1.gif圖片

8.查看nginx-cache服務器緩存目錄【對比緩存文件與原文件大小】

[root@nginx-cache ~]# tree /etc/nginx/cache_dir/
/etc/nginx/cache_dir/
└── 9
    └── 81
        └── d628235be0b8e19f5f78c37f5f226819
2 directories, 1 file

[root@nginx-cache ~]# tree /etc/nginx/temp_dir/
/etc/nginx/temp_dir/
0 directories, 0 files

[root@nginx-cache ~]# du -sh /etc/nginx/cache_dir/9/81/d628235be0b8e19f5f78c37f5f226819
16K /etc/nginx/cache_dir/9/81/d628235be0b8e19f5f78c37f5f226819
[root@nginx-front ~]# du -sh /usr/share/nginx/html/1.gif
16K /usr/share/nginx/html/1.gif

 

####


免責聲明!

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



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