nginx配置静态资源缓存, 同时配合k8s的SVC(core dns)实现负载均衡

Posted 李昊轩的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx配置静态资源缓存, 同时配合k8s的SVC(core dns)实现负载均衡相关的知识,希望对你有一定的参考价值。

最近发现以上线的tomcat web服务静态资源加载比较慢, 而且每次都会重新从服务端拉取, 这里肯定是有缺陷并且需要改进的. 然后决定使用nginx解决, 通过配置静态资源缓存实现动静分离, 同时发现k8s的dns解析果然简单粗暴, 非常实用, 直接无侵入式的实现了nginx的负载均衡(通过在server块的地址里直接配置SVC), 搞定!

后面我也会单独写一篇k8s的coredns/kubedns的工作原理, 和大家一起探讨.

下面是详细的配置文件
Nginx.conf

# 静态资源加速, 动静分离
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

##cache##
	proxy_cache_path /home/nginx_cache/ levels=1:2 keys_zone=my_zone:10m inactive=300s max_size=5g;

        server 
                listen 8003;                                                                                                                                                                                            
                server_name nginx-svc;                                                                                                                                                                              
               	proxy_buffering on;
                                                                                                                                                                    
                proxy_redirect http:// $scheme://;                                                                                                                                                                         
                location /                                                                                                                                                                                                
                    proxy_cache my_zone;
        			proxy_cache_valid 200 304 302 120m;
        			proxy_pass http://target-svc:3000;   
        			
        			if ($request_filename ~* .*\\.(?:htm|html)$)
                    
                        add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
                    
                    if ($request_filename ~* .*\\.(?:js|css)$)
                    
                        expires	120m;
                    
                    if ($request_filename ~* .*\\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)
                    
                        expires	120m;
                    
                                                                                                                                                               
                  	## set the real ip to invoid proxy-server-ip                                                                                                                                                                     
                    proxy_set_header   Host    $host:$server_port;                                                                                                                                                                 
                    proxy_set_header   X-Real-IP   $remote_addr;                                                                                                                                                                   
                    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;                                                                                                                                                 
                    proxy_set_header   X-Forwarded-Proto $scheme;                                                                                                                                                                  
                    proxy_set_header Upgrade $http_upgrade;                                                                                                                                                                        
                    proxy_set_header Connection "upgrade";                                                                                                                                                                         
                    proxy_read_timeout  600s;      
                    
                                                                                                                                                                                                    
                         
                                                                                                                                                                                                         
               


以上是关于nginx配置静态资源缓存, 同时配合k8s的SVC(core dns)实现负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

h5项目(特别是vue)缓存严重的解决方案,配合nginx

Nginx静态资源部署

nginx缓存静态资源,只需几个配置提升10倍页面加载速度

nginx配置管理浏览器静态缓存策略

nginx静态资源缓存策略配置

Nginx——nginx作为静态资源web服务(浏览器缓存示例演示)