2019-05-9笔记

Day49

课堂笔记

2019年5月9日

编译安装负载均衡


集群开机顺序:

1、从后往前开。

编译安装nginx负载均衡

下载:

mkdir -p /server/tools

cd /server/tools

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


#安装依赖。

yum install pcre pcre-devel -y

yum install openssl openssl-devel -y  #https加密用他。


#编译安装步骤

tar xf nginx-1.16.0.tar.gz

cd nginx-1.16.0/

useradd -u 1111 -s /sbin/nologin nginx -M

id nginx

./configure  --user=nginx --group=nginx --prefix=/application/nginx-1.16.0/ --with-http_stub_status_module  --with-http_ssl_module --with-pcre

make

make install

ln -s /application/nginx-1.16.0/ /application/nginx

/application/nginx/sbin/nginx

netstat -lntup|grep nginx

curl 127.0.0.1



负载均衡模板配置:

upstream backend {

    server 10.0.0.7:80  weight=1;

    server 10.0.0.8:80  weight=1;

}


server {

    listen       80;

    server_name  blog.etiantian.org;

    location / {

        proxy_pass http://backend;

    }

}

upstream模块 负载均衡池。

backend负载均衡池名称


[root@lb01 conf]# egrep -v "^$|#" nginx.conf.default >nginx.conf



正式配置:

[root@lb01 conf]# cat nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream backend {

        server 10.0.0.7:80  weight=3;

        server 10.0.0.8:80  weight=1;

    }


    server {

        listen       80;

        server_name  www.etiantian.org;

        location / {

            proxy_pass http://backend;

            proxy_set_header Host  $host;

        }

   }

}

别忘了,提前配置web服务器的www虚拟主机

WEB01:

[root@web01 /etc/nginx/conf.d]# cat www.conf

server {

        listen       80;

        server_name  www.etiantian.org;

        location / {

    root   /usr/share/nginx/html/www;

            index  index.html;

        }

    }


[root@web01 /etc/nginx/conf.d]# cat /usr/share/nginx/html/www/index.html

www7



WEB02:

[root@web02 /application/nginx/html/www]# cd /application/nginx/conf/extra/

[root@web02 /application/nginx/conf/extra]# cat 01_www.conf

    server {

        listen       80;

        server_name  etiantian.org;

rewrite ^/(.*) http://www.etiantian.org/$1 permanent;

    }

    server {

        listen       80;

        server_name  www.etiantian.org;

        location / {

            root   html/www;

            index  index.html index.htm;

        }


autoindex on;

access_log  logs/access_www.log  main;

    }

[root@web02 /application/nginx/conf/extra]# cat ../../html/www/index.html

www8


增加blog负载均衡


[root@lb01 conf]# cat nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream backend {

        server 10.0.0.7:80  weight=3;

        server 10.0.0.8:80  weight=2;

    }


    server {

        listen       80;

        server_name  www.etiantian.org;

        location / {

            proxy_pass http://backend;

            proxy_set_header Host  $host;

        }

   }


 server {

        listen       80;

        server_name  blog.etiantian.org;

        location / {

            proxy_pass http://backend;

            proxy_set_header Host  $host;

        }

   }

}



默认情况浏览器请求负载均衡器,会携带host字段,但是Nginx代理向后请求节点

默认在请求头里不带host字段。

配置Nginx代理向后请求节点默认在请求头里带host字段配置参数:

proxy_set_header Host  $host;




proxy_set_header X-Forwarded-For $remote_addr;

#<==这是反向代理时,节点服务器获取用户真实IP的必要功能配置。

在反向代理请求后端节点服务器的请求头中增加获取的客户端IP的字段信息,然后节点后端可以通过程序或者相关的配置接收X-Forwarded-For传过来的用户真实IP的信息。




负载均衡模板配置:

upstream backend {

    server 10.0.0.7:80  weight=1;

    server 10.0.0.8:80  weight=1;

}



upstream blog_server_pool {

server   10.0.10.6; #<==这一行标签和下一行是等价的。

   server   10.0.10.6:80 weight=1 max_fails=1 fail_timeout=10s;

nginx upstream监测节点状态,把不好的踢出去。好的时候加进来。

你可能感兴趣的