反向代理概述
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
Nginx负载均衡部署
#环境准备 lb01 10.0.0.5 172.16.1.5 web01 10.0.0.8 172.16.1.8 web02 10.0.0.7 172.16.1.7 web03 10.0.0.9 172.16.1.9 #三台都安装上Nginx #安装Nginx软件 #安装依赖软件包集合 yum -y install openssl openssl-devel pcre pcre-devel #下载安装Nginx软件 #软件可以去官网下 [root@lb01 ~]# cd /home/oldboy/tools/ [root@lb01 tools]# tar -xf nginx-1.10.2.tar.gz [root@lb01 tools]# ll total 896 drwxr-xr-x 9 1001 1001 4096 Mar 29 16:18 nginx-1.10.2 -rw-r--r-- 1 root root 910812 Mar 29 16:16 nginx-1.10.2.tar.gz [root@lb01 tools]#useradd -s /sbin/nologin -M www [root@lb01 nginx-1.10.2]#./configure --user=www --group=www --prefix=/application/nginx-1.10.2 --with-http_stub_status_module --with-http_ssl_module [root@lb01 nginx-1.10.2]# make [root@lb01 nginx-1.10.2]# make install ln -s /application/nginx-1.10.2/ /application/nginx
配置用于测试的web服务
Nginx web01 web02 web03 的配置如下 [root@lb01 nginx-1.10.2]# cd /application/nginx/conf/ [root@lb01 conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } access_log logs/access_www.log main; } server { listen 80; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } access_log logs/access_blog.log main; } } #配置完成后检查语法,并启动Nginx [root@web01 conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful [root@web01 conf]# ../sbin/nginx mkdir /application/nginx/html/{www,blog} for dir in www blog;do echo "`hostname` $dir" >/application/nginx/html/$dir/hehe.html;done for dir in www blog;do cat /application/nginx/html/$dir/hehe.html;done lb01 www lb01 blog #测试结果如下 [root@lb01 conf]# curl 10.0.0.7/hehe.html web02 www [root@lb01 conf]# curl 10.0.0.8/hehe.html web01 www [root@lb01 conf]# curl 10.0.0.9/hehe.html web03 www
实现一个简单的负载均衡
[root@lb01 conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; upstream server_pools { server 10.0.0.7; server 10.0.0.8; server 10.0.0.9; } server { listen 80; location / { proxy_pass http://server_pools; } access_log logs/access.log main; } } 检查语法启动服务 [root@lb01 conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful [root@lb01 conf]# ../sbin/nginx -s reload linux下测试结果如下 [root@lb01 conf]# ../sbin/nginx -s reload [root@lb01 conf]# curl 10.0.0.5/hehe.html web02 www [root@lb01 conf]# curl 10.0.0.5/hehe.html web01 www [root@lb01 conf]# curl 10.0.0.5/hehe.html web03 www [root@lb01 conf]# curl 10.0.0.5/hehe.html web02 www [root@lb01 conf]# curl 10.0.0.5/hehe.html web01 www [root@lb01 conf]# curl 10.0.0.5/hehe.html web03 www