一、配置静态缓存

目的:把从其他主机访问的前端静态资源,缓存到本地,降低网络通讯,提高性能。

http {

......

# 2024-03-08

upstream h5_server{

server 127.0.0.1:80;

}

# 2024-03-08 nginx增加缓存静态资源到本机

proxy_buffer_size 16k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 96k;

proxy_temp_file_write_size 96k;

# 临时缓存路径,目前看没啥用

proxy_temp_path /opt/tmp/ui_dir;

# 定义缓存的存储路径。

proxy_cache_path /opt/tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;

server {

......

#要缓存文件的后缀,可以在以下设置。

location ~ .*\.(gif|jpg|png|css|js)$ {

proxy_pass http://h5_server; # 前端服务地址(ip:port)

proxy_redirect off;

proxy_set_header Host $host;

proxy_cache cache_one;

proxy_cache_valid 200 302 2h;

proxy_cache_valid 301 1d;

proxy_cache_valid any 5m;

expires 2h;

add_header wall "hey! cache by nginx!";

}

}

配置后,访问前端,可以看到缓存了文件到目录中。

二、配置静态压缩

静态压缩,需要前端配合,事先把资源提前打包压缩,和原文件放一起,如:

demo.js, demo.js.gz 是同时存在的。

1)nginx -V 查看是否安装模块

[root@VM-24-9-opencloudos h5]# nginx -V

nginx version: nginx/1.14.1

built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC)

built with OpenSSL 1.1.1k FIPS 25 Mar 2021

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-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-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

如果存在:--with-http_gzip_static_module,说明已经安装静态压缩模块,如果没有需要安装(略)

2)nginx加入配置

location /h5 {

alias /opt/www/h5/;

index /h5/index.html;

try_files $uri $uri/ /h5/index.html;

# 打开静态压缩

gzip_static on;

}

配置好重启nginx即可。

可以看到,是我们手动压缩过的文件大小。

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。