nginx代理
代理和负载均衡的区别
代理:负责把连接请求直接转发到后台某个web节点
负载均衡:负责把请求使用某种调度算法分散发布给后台所有web节点
环境
192.168.1.20 nginx1
192.168.1.22 web1
1.配置nginx代理服务器lb1
[root@nginx1 ~]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@nginx1 ~]# yum -y install nginx
[root@nginx1 ~]# systemctl start nginx
[root@nginx1 ~]# systemctl enable nginx
2.创建代理配置文件,添加优化项
vim /etc/nginx/conf.d/lb1.conf
添加:
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://192.168.1.102;
proxy_set_header Host $http_host; #转发请求时,包含头部“HOST”信息
proxy_set_header X-Real-IP $remote_addr; #和下行一起,共同实现追踪客户端原ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30; #代理和后端服务器连接超时时间
proxy_send_timeout 60; #后端服务器传回代理的超时时间
proxy_read_timeout 60; #代理等待后端服务器的响应时间
proxy_buffering on; #启用缓存,后端返回内容先缓存,再给客户端,收到多少转多少
proxy_buffer_size 32k; #代理缓存用户头信息的缓存区大小
proxy_buffers 4 128k; #缓存区的设置
}
}
server {
listen 80;
server_name zh.benet.com;
location / {
proxy_pass http://192.168.1.102;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
保存退出重启nginx服务
[root@nginx ~]# systemctl restart nginx
3.搭建web
web
[root@web1 ~]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@web1 ~]# yum -y install nginx
备份
[root@web1 ~]# cd /etc/nginx/conf.d/
[root@web1 conf.d]# ls
default.conf
[root@web1 conf.d]# mv default.conf default.conf.bak
创建配置文件
[root@web1 conf.d]# vim web1.conf
添加:
server {
listen 80;
server_name web1.benet.com;
location / {
root /www;
index index.html;
}
}
启动ngi