faith丶

导航

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

 

####

posted on 2020-09-23 16:53  faith丶  阅读(1469)  评论(0编辑  收藏  举报