day49Nginx负载均衡实战
集群开机顺序:
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
echo 'export PATH=/application/nginx/sbin:$PATH' >>/etc/profile
. /etc/profile
/application/nginx/sbin/nginx
netstat -lntup|grep nginx
curl 127.0.0.1
安装成功
负载均衡模板配置:
[root@lb01 conf]# egrep -v "^$|#" nginx.conf.default >nginx.conf
[root@lb01 /application/nginx/conf]# vim nginx.conf
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 负载均衡池名称
weight 分配的比例
[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=1;
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 /application/nginx/conf/extra]# cat blog.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.php index.html index.htm;
}
access_log logs/access_blog.log main;
location ~ .*.(php|php5)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
增加www负载均衡,有一个域名,就需要添加一个
[root@lb01 /application/nginx/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=1;
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;
}
}
}
默认情况浏览器请求负载均衡器,会携带host字段,但是Nginx代理向后请求节点
默认在请求头里不带host字段。
配置Nginx代理向后请求节点默认在请求头里带host字段配置参数:
proxy_set_header Host $host;就不会乱访问了
proxy_set_header X-Forwarded-For $remote_addr; 可以查到客户的真实IP,而不是负载均衡的IP
<==这是反向代理时,节点服务器获取用户真实IP的必要功能配置。
在反向代理请求后端节点服务器的请求头中增加获取的客户端IP的字段信息,然后节点后端可以通过程序或者相关的配置接收X-Forwarded-For传过来的用户真实IP的信息。
注意lb01负载均衡只这里配置了,web服务器也要配置,格式如下:
[root@web01 /application/nginx/conf]# cat nginx.conf
worker_processes 1;
user nginx nginx;
error_log logs/error_bbs.log;
error_log logs/error_blog.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main 'remote_user [request" '
'body_bytes_sent "http_user_agent" "$http_x_forwarded_for"';
include extra/*.conf;
}
注意,上边一行代码的星,在虚拟机测试负载均衡的时候,最好写成具体的域名,因为解析的域名如果识别到了星下的域名,可能会
冲突。
负载均衡模板配置:
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监测节点状态,把不好的踢出去。好的时候加进来。
for n in {1..50};do curl www.etiantian.org;sleep 1;done
在负载均衡服务器上输入,测试负载均衡访问的web服务器是否为一比一的访问
(把两台web的www.etiantian.org内容设置不一样,更容易看到效果,sleep是时间间隔。)
tail -f access_www.log 查看哪个IP访问了我的IP
下节内容:
1、NGINX调度算法
2、动静分离
程序修改实现动静分离。
NGINX 反向代理实现动静分离。
3、根据PC、手机调度负载均衡
4、nginx代理的web状态页面(淘宝开发的插件)
Keepalived高可用。
实现负载均衡易错总结:
首先要把域名对应的首页,从php模式改为html模式,然后在站带你目录下创建intex.html,记住授权nginx属主和组
举例:
[root@web01 /application/nginx/conf/extra]# cat blog.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.php index.html index.htm;
}
把index.php 删掉或者放在index.html 后边就好了。
在虚拟机测试负载均衡时,把主配置文件的星改成具体的域名最好,如下:
[root@web01 /application/nginx/conf]# vim nginx.conf
worker_processes 1;
user nginx nginx;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main 'remote_user [request" '
'body_bytes_sent "http_user_agent" "$http_x_forwarded_for"';
include extra/blog.conf;
}