nginx负载均衡与动静分离

来源:互联网 发布:手机画中画软件 编辑:程序博客网 时间:2024/06/04 19:53

nginx由nginx内核和模块组成,内核的设计非常简洁,要处理的工作也非常简单,只需要通过配置文件,把客户端的请求映射到对应的location,然后由location来匹配并启动对应的模块去完成对应的工作。

  • 环境

192.168.2.118 nginx
192.168.2.112 lap/discus
192.168.2.111 lap/discus

  • nginx安装

其中默认安装需要rewrite模块,所以要先安装pcre

yum install pcre-devel pcre -y openssl openssl-develuseradd wwwmkdir nginxtestcd nginxtestwget -c http://nginx.org/download/nginx-1.12.0.tar.gztar -zxvf nginx-1.12.0.tar.gzcd nginx-1.12.0./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_modulemakemake install

nginx初始目录

[root@mysqlm nginx]# lsconf  html  logs  sbin

启动nginx并查看启动状态

/usr/local/nginx/sbin/nginx[root@mysqlm sbin]# ps -ef | grep nginxroot      3997     1  0 21:22 ?        00:00:00 nginx: master process ./nginxwww       3998  3997  0 21:22 ?        00:00:00 nginx: worker processroot      4000  1428  0 21:23 pts/0    00:00:00 grep nginx[root@mysqlm sbin]# netstat -tunpl | grep 80tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3997/nginx

浏览器访问nginx页面
这里写图片描述

至此nginx安装完成。

  • nginx负载均衡配置

上述安装完成的nginx配置文件都是默认值。
在nginx.conf下的http模块尾部添加

include vhost/*.conf;

把nginx.conf默认的80端口改成8081(只要不是80,为了避免冲突)

在conf目录下添加vhost目录并添加lee.conf内容如下

upstream web_lee1{        server 192.168.2.112:80 weight=1 max_fails=2 fail_timeout=30s;        server 192.168.2.111:80 weight=1 max_fails=2 fail_timeout=30s;}server {        listen       80;        server_name  www.lee1.com;        access_log  logs/lee1.access.log;        index  index.jsp index.html index.htm;        root  /data/webapps/lee1;        location / {            index  index.html index.htm;            proxy_set_header  Host  $host;            proxy_set_header  X-Real-IP  $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_pass http://web_lee1;        }#       access_log  /usr/local/nginx/logs/lee1/access.log main;#       error_log   /usr/local/nginx/logs/lee1/error.log  crit; }

上述的server表示一个虚拟主机,也可以配置多个server,也即多个虚拟域名的主机,此时多个域名对应同一个端口80

可以把upstream放在nginx.conf下,然后把server放在单独的文件中以便维护
浏览器访问192.168.2.118可以看到如下

这里写图片描述

此时nginx会轮询访问111和112的apache服务。只要有111服或112服的apache服务在运行,就可以直接访问。任何其中一个服务器的apache服务挂了,nginx会自动判断并直接访问apache服务正常的那台服务器。

  • nginx的动静分离部署

ngingx可以把客户端的动态请求和静态请求分开,nginx处理静态页面,而动态页面由nginx转到php/jsp/tomcat等后端去处理。
首先进入/usr/local/apache2/htdocs/static/image/common下,把logo.png改成logo.png.bak,此时首页里的logo消失

这里写图片描述

把112的发布目录发送到118的/data/webapps/lee1/下

scp -r htdocs/* root@192.168.2.118:/data/webapps/lee1/

在上面配置文件nginx.conf中,添加下面内容到server

location ~ .*\.(php|jsp|cgi|shtml)?$            {                 proxy_set_header Host  $host;                 proxy_set_header X-Real-IP $remote_addr;                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                 proxy_pass http://web_lee1;            }        location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$         {            root /data/webapps/lee1/;           # expires      60d;         }

然后重新加载nginx

/usr/local/nginx/sbin/nginx -s reload 

浏览器输入网址后可以重新显示logo图像,说明此时读取的是118里的图片

这里写图片描述