nginx负载均衡

正向代理与反向代理的区别

正向代理:你只能看到在服务端看到代理端的IP
反向代理:你只能在服务端看到客户端的IP

正向代理

1.proxy代理的是 client
2.基于proxy隐藏了真是client的信息
3.在server目标机器上看到的是 proxy的信息

基于IP:port的正向代理

正向代理的配置

192.168.23.100 test1 clien
192.168.23.101 test2 clien
192.168.23.102 test3 proxy
192.168.23.103 test4 server

server配置(test4配置)

#安装nginx
[root@test4 ~]# yum install nginx -y
#重启nginx
[root@test4 ~]# systemctl restart nginx
[root@test4 ~]# echo "i am test4 192.168.23.103" > /usr/share/nginx/html/index.html 
[root@test4 ~]# curl 192.168.23.103
i am test4 192.168.23.103

proxy配置(test3配置)

#安装nginx
[root@test3 ~]# yum install nginx -y
#修改配置文件
[root@test3 conf.d]# cat x.conf 
server {
  listen 80;
  server_name _;
  location / {
   proxy_pass http://192.168.23.103/;
} 
}
[root@test3 conf.d]# systemctl restart nginx

client访问

#分别用两个客户端进行访问
[root@test1 ~]# curl 192.168.23.102
i am test4 192.168.23.103
[root@test2 ~]# curl 192.168.23.102
i am test4 192.168.23.103

查看代理日志

### 此时代理上是显示client访问ip
[root@test3 conf.d]# tail -f /var/log/nginx/access.log 

192.168.23.101 - - [29/Jan/2024:21:31:45 -0500] "GET / HTTP/1.1" 200 26 "-" "curl/7.29.0" "-"
192.168.23.100 - - [29/Jan/2024:21:31:58 -0500] "GET / HTTP/1.1" 200 26 "-" "curl/7.29.0" "-"

### 服务端上就显示的是proxy的ip信息
[root@test4 ~]# tail -f  /var/log/nginx/access.log 

192.168.23.102 - - [29/Jan/2024:21:31:45 -0500] "GET / HTTP/1.0" 200 26 "-" "curl/7.29.0" "-"
192.168.23.102 - - [29/Jan/2024:21:31:58 -0500] "GET / HTTP/1.0" 200 26 "-" "curl/7.29.0" "-"

此时正向代理完成

你可能感兴趣的:(nginx专栏,nginx,负载均衡,运维,服务器)