`

深入浅出Nginx之六【代理缓存与负载均衡】

阅读更多

在上一篇博客中简要介绍了Nginx和Tomcat的整合使用,然而还有一些历史遗留问题尚未提及尴尬。本篇博客将简要介绍Nginx的代理缓存,以及使用多台Tomcat服务器进行负载均衡。

 

<一>. 代理数据存放目录:吻

 proxy_temp_path  /software/proxy_temp;

     说明:代理临时目录,存在于http作用域,nginx用作代理时,用来存放和后端服务器进行交互的相关数据, 如前端用户的请求、后端服务器的响应,默认位置为nginx安装路径下面的/proxy_temp。

 proxy_cache_path  /software/proxy_cache levels=1:2 keys_zone=cache_web:16m inactive=1h max_size=512m;

    说明:代理缓存目录,存在于http作用域,nginx进行web缓存时,将静态资源缓存到nginx当前所在的机器。

  1. proxy_temp_path和proxy_cache_path必须指定为同一分区。

  2. 参数levels=1:2  指定缓存空间为二级hash目录,第一级为1个字符,第二级为2个字符,比如/7/c2。

  3. 参数keys_zone=cache_web:16m  用户存放key和元数据的缓存区间,命名为cache_web,16m大小。 

  4. 参数inactive=1h  如果资源在1h内未被请求,从缓存区中清除。

  5. 参数max_size=512m  指定本地缓存空间的大小,如果资源过于庞大时,按照LRU算法进行清除。

 

<二>. 定义服务器集群:大笑

 upstream blog_server_cluster {
     server  192.168.142.56:8080 weight=1 max_fails=2 fail_timeout=30s;
     server  192.168.142.78:8080 weight=2 max_fails=2 fail_timeout=30s;
     server  192.168.142.91:8080 weight=2 max_fails=2 fail_timeout=30s;
 }

     说明:存在于http作用域,同一组服务器相互之间进行负载的均衡。
  1. weight  指定各个服务器的权重,默认为1,权重越大处理的负载越多。
  2. max_fails  指定失败请求次数,大于该次数时,nginx认为该服务器处于不可用状态。
  3. fail_timeout  失败请求时间,大于该时间,nginx认为本次请求失败。

 

<三>. 设置web缓存:微笑

 proxy_cache  cache_web;
 proxy_cache_key  $host$uri$is_args$query_string;

 proxy_cache_valid  200 304 1d;
 proxy_cache_valid  301 302 1m;
 proxy_cache_valid  any 1m;

     说明:web缓存,存在于location作用域,将后端服务器的静态资源缓存到nginx所在的本地机器。
  1. proxy_cache  指定为上述定义的缓存区域。
  2. proxy_cache_key  指定缓存所使用的key的组合字符串,然后用md5进行加密。
     $host  主机名,如www.excelsoft.com
     $uri  请求的项目内部路径,如/user/list.jsp。
     $is_args  如果有请求参数则为?符号,否则为空字符串""。
     $query_string  请求参数字符串。
  3. proxy_cache_valid  对不同状态码的URL设置不同的缓存时间(1d/1h/1m)。

 

<四>. http状态码:status code酷

  1. 2xx:success
     200  请求成功 [Ok]
  2. 3xx:redirect 
     301  请求的资源被移除到新的位置,新的URI将在响应的Location字段中返回  [Moved Permanently]
     302  请求的资源临时从不同的URI进行响应  [Found]
     304  重复的请求,该响应不包含消息体   [Not Modified]
  3. 4xx:request error
     404  请求的资源未能在服务器上找到  [Not Found]
     403  找到请求的资源,但是无权限访问  [Forbidden]
  4. 5xx:server error
     500  程序出错,服务器无法处理  [Internal Server Error]
     502  错误网关,内部网络连接设置不正确,或者当前服务器待处理的连接过多,以至于连接超时  [Bad Gateway]

 

     到目前为止,已经比较全面的介绍了Nginx用作反向代理和负载均衡的诸多知识,为了能从整体上对配置文件有一个清楚地把握,下面给出nginx.conf文件的详尽内容眨眼,具体细节可参见之前的几篇博客:

 

user  nick excelsoft;

worker_processes  2;

error_log  /opt/nginx/logs/error.log notice;

pid  /opt/nginx/logs/nginx.pid;

worker_rlimit_nofile  65535;

events {
    use  epoll;
    worker_connections  65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    charset  utf-8;

    log_format  access '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for" '
                       '"$sent_http_content_type" "$request_time"'; 

    access_log  /opt/nginx/logs/access.log access;

    sendfile  on;
    tcp_nopush  on;
    tcp_nodelay  on;
    keepalive_timeout  65;
    
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers  8 32k;
    gzip_http_version  1.1;
    gzip_comp_level  3;
    gzip_types  text/plain text/css text/javascript application/x-javascript application/xml;
    gzip_vary  on;

    server_names_hash_bucket_size  128;
    client_header_buffer_size  32k;
    large_client_header_buffers  4 32k;
    client_max_body_size  100m;
    client_body_buffer_size  128k;

    proxy_connect_timeout  90;
    proxy_read_timeout  90;
    proxy_send_timeout  90;

    proxy_buffer_size  16k;
    proxy_buffers  4 32k;
    proxy_busy_buffers_size  64k;
    proxy_temp_file_write_size  128k;

    proxy_temp_path  /software/proxy_temp;
    proxy_cache_path  /software/proxy_cache levels=1:2 keys_zone=cache_web:16m inactive=1h max_size=512m;

    upstream blog_server_cluster {
        server  192.168.142.56:8080 weight=1 max_fails=2 fail_timeout=30s;
        server  192.168.142.78:8080 weight=2 max_fails=2 fail_timeout=30s;
        server  192.168.142.91:8080 weight=2 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       80;
        server_name  www.excelsoft.com;

        location / {
            proxy_pass  http://blog_server_cluster;

            proxy_set_header Host $host;
    	    proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            index  index.html index.htm index.jsp index.do;
            access_log  /opt/nginx/logs/access.excelsoft.log access;
        }

        location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ {
            proxy_pass  http://blog_server_cluster;
           
            proxy_cache  cache_web;
            proxy_cache_key  $host$uri$is_args$query_string;

            proxy_cache_valid  200 304 1d;
            proxy_cache_valid  301 302 1m;
            proxy_cache_valid  any 1m; 

            expires  15d;
            access_log  off;
        }

        location ~ \.(js|css)$ {
            proxy_pass  http://blog_server_cluster;

            proxy_cache  cache_web;
            proxy_cache_key  $host$uri$is_args$query_string;

            proxy_cache_valid  200 304 1d;
            proxy_cache_valid  301 302 1m;
            proxy_cache_valid  any 1m;

            expires  1h;
            access_log  off;
        }
    }

    server {
        listen       80;
        server_name  static.excelsoft.com;

        root  /software/static;
        access_log  off;

        location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires  15d;
        }

        location ~ \.(js|css)$ {
            expires  1h;
        }
    }
}

 

     关于nginx的常用配置,还有一些未尽事宜大笑,在最后的一篇博客将进行详尽的讲解。 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics