Nginx的负载均衡-域名重定向-访问控制

访问控制:

约束哪些用户可以访问到nginx,不过一般用不到,谁做Web服务器不让人访问。

[root@nginx1 ~ ]# vim /usr/local/nginx/conf/nginx.conf

    server {
        listen       80 default;
        location / {
            root   /webserver/nginx-s2;
            index  index.html index.htm;
            auth_basic  "admin";
            auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

然后下载httpd-tools

[root@localhost nginx]# yum install httpd-tools -y

设置密码:

[root@localhost nginx]# htpasswd -c /usr/local/nginx/conf/.htpasswd nginx1

在这里插入图片描述
控制列表

 server {
        listen       80 default;
        server_name  localhost;


        location / {
            #root   /usr/local/nginx/html;
            root /webserver/nginx-s2;
            index  index.html index.htm;
            #auth_basic "admin";
            #auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
            allow 172.16.12.1;
            deny all;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

上边的意思是只让172.16.12.1能访问别的都拦截。

[root@ks1 ~]# curl 172.16.12.31
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.17.6</center>
</body>
</html>
[root@ks1 ~]# 

在这里插入图片描述只拦截某个IP就直接写

deny 172.16.12.1;
域名重定向。

添加两个页面。

[root@localhost nginx-s1]# echo "<h1>i am AAAAA<h1>" >> a/index.html
[root@localhost nginx-s1]# echo "<h1>i am BBBBB<h1>" >> b/index.html

正常访问是下边的。
在这里插入图片描述

在这里插入图片描述设置重定向

    server {
        listen       80;
        server_name  www.nginx-s1.com www.nginx-s3.com;


        location / {
            root   /webserver/nginx-s1;
            index  index.html index.htm;
        }

        location /a/ {
            proxy_pass http://www.nginx-s1.com/b/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

/a/就是在地址栏域名后边加的。

然后就能访问了。访问的是/a/ 跳转到 /b/的资源。

在这里插入图片描述

注意:proxy_pass后的内容一定要是能访问的,编译时可能会报这个错误。

nginx: [emerg] host not found in upstream "www.nginx-s1.com" in /usr/local/nginx/conf/nginx.conf:49

因为nginx不认识这个域名,自然不能跳转,所以编译时报错。最简单的办法就是修改hosts文件,或者直接写IP。

proxy_pass 172.16.12.31:80/;

在这里插入图片描述不过IP 的话后边的那个就不好加了。

rewrite

    server {
        listen       80;
        server_name  www.nginx-s1.com www.nginx-s3.com;


        location / {
            root   /webserver/nginx-s1;
            index  index.html index.htm ;
        }
        location ~ [0-9]+\.html {
            rewrite ^/[0-9]+\.html$ http://www.nginx-s1.com/b/;
        }

location后可以匹配正则表达式。
rewrite的语法是将 前边的匹配项,统一转到后边的。

并且这个是转发,地址栏会改变,proxy_pass是重定向,地址栏不会变。

在这里插入图片描述
在这里插入图片描述
123 和456都被匹配了。

都会被跳转到下边这个。
在这里插入图片描述

负载均衡

负载均衡就是按照服务器的能力将请求分给他们。
有的服务器硬件能力强大,内存,CPU等等资源很猛,那就可以给它分配更多请求,让它处理,而另一个服务器的硬件条件很差,那就不要让他处理过多的请求,否则会“憋死"。

实现负载均衡可以用软件和硬件。

硬件的有F5 、深信服、Array等。但是很贵。

软件的负载均衡有nginx、LVS等等,成本低。

配置起来还是用nginx.conf。

比如我起了2个apache服务器,1个nginx。
172.16.12.60~61 apache

172.16.12.31 做nginx


upstream WebA {      //负载均衡服务器模块,通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡。
        server 172.16.12.60;
        server 172.16.12.61;
    }
//意思就是WebA这个请求是通过 这两个IP的服务器来实现的。
    
    server {
        listen       80;
        server_name  www.nginx-s1.com www.nginx-s3.com;

        location / {
            proxy_pass http://WebA;    //对应上边的upstream。
            proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;   //错误信息
            include /usr/local/nginx/conf/proxy.conf;    //加载配置文件
        }

其实最简单的负载均衡配置
这两个都可以不写,就是错误和配置文件这两个。

proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;   
include /usr/local/nginx/conf/proxy.conf; 

配置文件的内容和路径:这个配置文件也可以直接写到主配置文件中。
/usr/local/nginx/conf/proxy.conf

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_body_buffer_size 128k;
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;

然后就可以访问测试了。

在这里插入图片描述
可以看到,访问同一个域名,所经过的服务器却不同,并且1212轮流请求。

这就是nginx负载均衡策略一、轮询。

    upstream WebA {
        server 172.16.12.60 weight=10;
        server 172.16.12.61 weight=20;

这样加上weight ,weight叫权重值。越高,代表优先级越高,分配的请求就越多。

在这里插入图片描述
可以看到相互之比为1:2 正好就是权重之比。

这就是nginx负载均衡策略 之二 : 权重值。

    upstream WebA {
        ip_hash;
        server 172.16.12.60 weight=10;
        server 172.16.12.61 weight=20;
    }

加一个 ip_hash 就是IP哈希

意思是如果访问时分配到了某个服务器,那么以后的连接,都访问这个服务器。

在这里插入图片描述
nginx负载均衡策略之三:IP_hash

least_conn算法很简单,首选遍历后端集群,比较每个后端的conns/weight,选取该值最小的后端。

    upstream WebA {
        least_conn;
        server 172.16.12.60 weight=10;
        server 172.16.12.61 weight=20;
    }

nginx负载均衡策略之四:最小连接

另外的策略:

url_hash策略

该策略将前端请求的url地址进行hash操作,根据hash结果将请求定向到同一后端服务器节点上,后台服务器为缓存是比较有效。一般url_hash需要配合缓冲命中来使用。

fair策略

该策略请求转发到负载最小的后端服务器节点上。Nginx通过后端服务器节点对响应时间来判断负载情况,响应时间最短的节点负载就相对较轻,Nginx就会将前端请求转发到此后端服务器节点上。

Sticky策略

该策略在多台服务器的环境下,为了确保一个客户端只和一台服务器通讯,它会保持长连接,并在结束会话后再次选择一个服务器,保证了压力均衡。

故障检测:

    upstream WebB {
        server 172.16.12.62 max_fails=2 fail_timeout=3s;
        server 172.16.12.63 max_fails=2 fail_timeout=3s;
    }

超时的话就认定为故障,故障后就不会向故障的机器发请求。

比如杀掉一个服务器。

[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache4444444<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache4444444<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache4444444<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache4444444<h1>

正常是轮询的。

杀掉一个服务器后。

<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# 

只发送到好的那个。

修复之后。

[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache4444444<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache4444444<h1>
[root@ks1 ~]# curl www.nginx-s2.com
<h1>apache3333333<h1>

继续轮询。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值