第一章:为什么要用反向代理,有哪些软件可以使用?
反向代理软件
硬件:F5
软件:Nginx HAproxy LVS等
第二章: Nginx反向代理参数解释
官方案例:
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
解释:
proxy_pass http://172.16.1.7:8080; #将用户的请求转发给后端的nginx服务器处理
proxy_set_header Host $host; #将用户访问的地址记录到自己的HOST记录上
proxy_set_header X-Real-IP $remote_addr; #将用户访问的IP记录到自己的IP地段上
常用的代理配置参数:
proxy_set_header Host $http_host; #将用户访问的地址记录到自己的HOST记录上
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #将用户访问IP记录到自己的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;
第三章:Nginx反向代理配置
需求:
web01 8080 web01
lb 80 跳转到web01上的8080
web服务器操作:
1.配置nginx文件
[root@web01 ~]# cat /etc/nginx/conf.d/web01.conf
server {
listen 8080;
server_name www.mysun.com;
location / {
root /code;
index www.html;
}
}
2.写入测试页面
echo "web01" > /code/www.html
3.检查并重启Nginx
nginx -t
systemctl restart nginx
负载服务器操作
1.安装NGINX
yum -y install nginx #安装官方版本
2.编写代理需要的参数配置文件
[root@lb01 ~]# cat /etc/nginx/proxy_params
proxy_set_header Host $http_host;
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;
3.编写代理服务配置文件
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf
server {
listen 80;
server_name www.mysun.com;
location / {
proxy_pass http://172.16.1.7:8080;
include proxy_params;
}
}
4.检查并启动Nginx
nginx -t
systemctl start nginx
windows操作:
绑定hosts文件
10.0.0.5 www.mysun.com
第四章:反向代理结合负载均衡配置
1.地址规划
web01 www.mysun.com blog.mysun.com bbs.mysun.com
web02 www.mysun.com blog.mysun.com bbs.mysun.com
负载lb01
www.mysun.com
blog.mysun.com
bbs.mysun.com
2.配置web服务器
cat >/etc/nginx/conf.d/www.conf <<EOF
server {
listen 80;
server_name www.mysun.com;
location / {
root /code;
index www.html;
}
}
EOF
cat >/etc/nginx/conf.d/bbs.conf <<EOF
server {
listen 80;
server_name bbs.mysun.com;
location / {
root /code;
index bbs.html;
}
}
EOF
cat >/etc/nginx/conf.d/blog.conf <<EOF
server {
listen 80;
server_name blog.mysun.com;
location / {
root /code;
index blog.html;
}
}
EOF
echo "$(hostname) bbs" > /code/bbs.html
echo "$(hostname) www" > /code/www.html
echo "$(hostname) blog" > /code/blog.html
nginx -t
systemctl restart nginx
3.配置lb服务器
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf
upstream web_pools {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name www.mysun.com bbs.mysun.com blog.mysun.com;
location / {
proxy_pass http://web_pools;
include proxy_params;
}
}
nginx -t
systemctl restart nginx
4.windows操作:
10.0.0.5 www.mysun.com bbs.mysun.com blog.mysun.com