Nginx搭建反向代理负载均衡和web缓存服务器

       互联网中当web服务器无法承受大量用户访问时,可以采用负载均衡加缓存技术来降低单台服务器和数据库的压力,常用的开源软件有nginx、varnish、squid等。而相对来说nginx具有轻量级、多核cpu利用、安装配置简单等优点,个人更喜欢用nginx。centos环境下的安装配置。

1、安装环境

安装nginx需要的环境,pcre(作用rewrite)、zlib(作用压缩)、ssl(支持https等),这个可以yum安装,也可以自己下载源码编译安装。

yum -y install zlib;

yum -y install pcre  pcre-devel;

yum -y install openssl openssl-devel;

 

  1. 安装ngx cache purge缓存插件

下载ngx_cache_purge-2.3版本为例

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

解压到某个目录tar zxvf ngx_cache_purge-2.3.tar.gz

 

  1. 下载安装nginx

下载nginx1.13.12版本为例

wget http://nginx.org/download/nginx-1.13.12.tar.gz

tar -zxvf nginx-1.13.12.tar.gz

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/步骤二中解压的路径/ngx_cache_purge-2.3 --with-http_proxy_module

解释: --prefix 为安装路径,--with-为需要安装的模块,具体可以运行 ./configure --help 查看有效模块,--add-module=/步骤二中解压的路径/ngx_cache_purge-2.3

make && make install;

 

4、运行nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

5、配置nginx.conf

vim /usr/local/nginx/conf/nginx.conf

示例:

http {

    include       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  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

    ##cache begin##

    proxy_buffering on;

    proxy_cache_valid any 1m;

    proxy_cache_path /var/nginxcache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=10m;

    proxy_temp_path /var/nginxcache;

    proxy_buffer_size 4k;

    proxy_buffers 100 8k;

    ##cache end##

    ## Apache backend servers ##

    upstream backendserver{

      server 192.168.0.101:80;

  server 192.168.0.102:8080;

}

    ##nginx local server begin##

      listen 80;

      server_name myserver;

      index index.html index.htm;

      root /var/www/html;

      location /

      {

          proxy_next_upstream http_502 http_504 error timeout invalid_header;

          proxy_cache my-cache;

          proxy_cache_valid 200 304 1m;

          proxy_cache_valid any 2m ;

          proxy_cache_key $host$uri$is_args$args;

          proxy_set_header Host $host;

          proxy_set_header X-Forwarded-For $remote_addr;

          proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;

          proxy_pass http://backendserver1;

          expires 1m;

      }

      location ~ /purge(/.*)

      {

          auth_basic "TDT  Center  CACHE  Center";

          auth_basic_user_file /tmp/htpasswd;

          allow 127.0.0.1;

          deny all;

      }

##nginx local server end##

}

 

缓存相关配置说明:

proxy_cache my-cache;指定使用哪个共享缓存空间,my-cache与proxy_cache_path中name对应。

proxy_cache_key:设置缓存使用的key,默认为完整的访问URL,根据实际情况设置缓存key。

proxy_cache_valid 200 304 1m:为不同的响应状态码设置缓存时间。如果是any为全部。

max_size指的是缓存文件可以占用的最大空间。

Inactive:缓存时间,优先级大于proxy_cache_valid,为避免proxy_cache_valid不起作用,应将Inactive设置大于proxy_cache_valid。

expires 1m;用在发给客户端的响应中,添加"Expires"头,可重定义后端html的head头"Expires"。

反向代理负载均衡相关配置说明:

proxy_pass http://backendserver;指定反向代理到后端服务器。

upstream backendserver{

      server 192.168.0.101:80;

  server 192.168.0.102:8080;

}

nginx 提供轮询(round robin)、用户 IP 哈希(client IP)和指定权重 3 种方式默认为轮询作为负载均衡策略哈希方式如下:

upstream backendserver{

ip_hash;

      server 192.168.0.101:80;

  server 192.168.0.102:8080;

}

权重方式如下:

upstream backendserver{

      server 192.168.0.101:80 weight=1;

  server 192.168.0.102:8080 weight=9;

}

proxy_set_header Host $host;重新定义或者添加发往后端服务器的请求头

proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;避免后端服务器head头设置了no-cache、no-store,造成无法缓存。

其他参数设置,诸如缓存buffer等可以根据实际情况设置

proxy_connect_timeout 90;

proxy_send_timeout 90;

proxy_read_timeout 90;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

       采用nginx架设一台缓存加反向代理到后台多台服务器可以满足很多情况下的并发访问压力问题,特别是web应用中有数据库访问的,如果对数据访问时效性要求不高时,应用缓存后对数据库压力可以大大降低。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值