一、Nginx实现反向代理
概念
反向代理:在收到客户端请求之后,会修目标IP地址和端口
正向代理:在收到客户端请求之后,会修源IP地址和端口
上游服务器:代理服务器后端的哪些真正给客户端提供服务的节点,这样的服务器称之为上游服务器
下游服务器:客户端就是下游节点
模块:nginx_http_proxy_module
指令:
proxy_pass:指定上游服务器的ip和端口
:指定在重新封装请求报文的时候,添加一个新的首部
部署步骤:
0.检查环境
1.安装服务
yum -y install nginx httpd php
2.配置httpd端口
vim /etc/httpd/conf/httpd.conf
###########
Listen 45678
############
3.启动httpd
systemctl start httpd
systemctl enable httpd
netstat -anp|grep httpd
4.修改Nginx配置文件
vim /etc/nginx/nginx.conf
########
删除默认的80端口server          #切记最后需要加一个}。
###############
cd /etc/nginx/conf.d
vim pass.conf
###########
 server {
     listen      80;
     location / {  
     proxy_pass   http://127.0.0.1:45678;
     }
}
###########
5.启动nginx
nginx -t              #进行语法检测
systemctl restart nginx
systemctl enable nginx
netstat -anp|grep nginx
firewall-cmd --add-port=80/tcp
firewall-cmd --add-port=80/tcp --permanent
6.浏览器验证
7.验证php
echo "<?php phpinfo(); ?>" > /var/www/html/index.php
8.浏览器验证
9.配置nginx网站
mkdir /nginx
echo "123" > /nginx/index.html
vim /etc/nginx/conf.d/pass.conf
###################
 server {
     listen      80;
     location ~* \.php$ {  
     proxy_pass   http://127.0.0.1:45678;
     }
     location / {  
     root /nginx;
     }
}
################
10.重启nginx
nginx -t
systemctl restart nginx
11.浏览器验证
IP/index.html
IP/index.php
补充一:
location如下:
        location /admin {
            proxy_pass http://www.ken.com/;
            proxy_pass http://www.ken.com;   
        }
请求的url 是http://www.ken.com/admin/a.html
如果代理方式是 proxy_pass http://www.ken.com/; 那么去www.ken.com的根目录下找a.html,/代表完全代理。
如果代理方式是 proxy_pass http://www.ken.com; 那么去www.ken.com的根目录下的admin找a.html补充二:
如果location中使用了模式匹配(正则),那么,location中的url会直接补充到代理节点的后面.
    此时,上游服务器的的后面不能有任何内容,包括 /

        location ~ \.php$ {
            proxy_pass http://www.ken.com; [正则表达式proxy_pass转发的地址后面什么都不能加]       <<< 正确写法
            proxy_pass http://www.ken.com:80;     <<< 正确写法
            proxy_pass http://www.ken.com/;       <<< 错误写法
            proxy_pass http://www.ken.com/img;    <<< 错误写法
        }

    此时,如果请求的url是 http://www.baidu.com/book/stu/a.php ,就会代理成 http://www.ken.com/book/stu/a.php补充三:
在location中如果有重定向的话,那么就用重定向后的uri替换掉代理节点中的uri
        location / {
            rewrite /(.*)$ /index.php?name=$1 break;
            proxy_pass http://www.baidu.com:80/img;
        }

    此时,如果请求的url是 http://www.ken.com/bajie ,就会代理成 www.baidu.com/index.php?name=bajie二、nginx实现负载均衡
概念:
1、概念
调度器:分发用户的请求到一个后端节点
上游服务器(真实服务器):每个真正用来处理用户请求的节点都是一个上游服务器
CIP:(client IP)客户端的IP地址
RIP:(real IP)真实服务器的IP地址
VIP:(virtual IP)虚拟IP,用户所看到的是也是虚拟IP
2、指令
指令:upstream
作用:定义一个上游服务器组
格式
{
上游服务器1  参数 参数;
上游服务器1  参数 参数;
上游服务器1  参数 参数;
}
3、参数
weight=#:设置服务器的权重(数字越大,权重越高)
backup:设置服务器处于备用状态(其他节点出现故障,备用节点才开始工作)
down:设置让一个节点处于离线状态(经常用在维护一个节点的情况下)
max_fails=number:设置连续几次转发失败就认为该节点出现故障,然后就不再向该节点转发用户请求了
fail_timeout=time:和上个参数组合使用,作用是设置等待上游服务器响应超时时间
实验:a.com、b.com、c.com
1.安装服务
yum -y install nginx
2.建立路径
mkdir -p /work/html_a
mkdir -p /work/html_b
echo "aaaaaaaaa" > /work/html_a/index.html
echo "bbbbbbbb" > /work/html_b/index.html
3.编辑配置文件
cd /etc/nginx/conf.d
vim ab.conf
###########################
 server {
       listen      80;
       server_name a.com;
       location / {
       root /work/html_a;
       }
}
 server {
       listen      80;
       server_name b.com;
       location / {
       root /work/html_b;
       }
}
server {
       listen      80;
       server_name c.com;
       location / {
proxy_pass http://abc;
       }
}
upstream abc {
            server     127.0.0.1:81;
            server     127.0.0.1:82;
}
server {
       listen      81;
       server_name a.com;
       location / {
       root /work/html_a;
       }
}
server {
       listen      82;
       server_name b.com;
       location / {
       root /work/html_b;
       }
}
########################
4.查错
nginx -t
5.重启服务
systemctl restart nginx
6.添加防火墙端口
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
7.设置域名静态解析
echo "192.168.10.100 a.com" >> /etc/hosts
echo "192.168.10.100 b.com" >> /etc/hosts
echo "192.168.10.100 c.com" >> /etc/hosts
8.验证
curl a.com
curl b.com
curl c.com

三、反向代理和负载均衡的区别

1、反向代理proxy_pass后跟的是一个具体的IP地址,负载均衡proxy_pass后跟的是集群名;

2、反向代理代理到的是某一个特点的服务器,而负载均衡器是根据算法调度到集群中的某个节点上;

3、反向代理和负载均衡都可以使用模式匹配(正则)。

四、nginx启动错误

  利用nginx进行反向代理的时候,我们会配置proxy_pass。在启动nginx的时候,会报如下错误:

nginx: [emerg] host not found in upstream "xxx.com" in /usr/local/nginx/conf/vhost/yq.nginx.com.conf:19

  其实nginx配置语法上没有错误的,只是系统无法解析这个域名,所以报错.
解决办法就是添加dns到/etc/resolv.conf 或者是/etc/hosts,让其能够解析到IP。类似windows下的hosts文件的修改。

  具体步骤如下:

vim /etc/hosts
 
//修改hosts文件,在hosts文件里面加上一句:
 
127.0.0.1       localhost.localdomain   xxx.com