nginx配置缓存静态资源

本文详细介绍了如何在Nginx中配置静态缓存,以减少网络通信并加速前端资源加载。此外,还涉及了静态资源的压缩配置,需确保前端配合。通过安装和启用相关模块,可以显著提高网站性能。
摘要由CSDN通过智能技术生成

一、配置静态缓存

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

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即可。

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

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值