Kubernetes

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

本文主要是介绍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)实现负载均衡的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!