反向代理的作用
1、解决网站服务器对外可见的问题、私密性、安全性
2、路由功能:根据用户请求的URI调度到对应功能的节点处理请求
3、负载均衡:将用户的请求,通过调度算法挑选一台合适的节点处理请求
4、动静分离:根据用户请求的URI进行区分,将动态资源调度到应用服务器处理,将静态资源调度到静态资源服务器处理
5、数据缓存:加速网站的访问速度,减轻web服务器的负担。如果用户请求的内容在缓存中,可以直接在代理服务器中获取,加速用户的访问速度
web-01192.168.200.120web-02192.168.200.121
web-01安装、配置
cat > /etc/yum.repos.d/nginx.repo << OK
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
OK
[root@nginx ~]# yum -y install nginx
[root@nginx ~]# cd /etc/nginx/conf.d/
[root@nginx conf.d]# mv default.conf{,.bak}
[root@nginx conf.d]# vim proxy_web.conf
server {
listen 80;
server_name www.test.org;
location / {
proxy_pass http://192.168.200.121:80;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[root@nginx ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf
[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx ~]# systemctl enable nginx
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# ps -ef | grep -v "grep" | grep nginx
root 2282 1 0 13:04 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 2283 2282 0 13:04 ? 00:00:00 nginx: worker process
web-02安装、配置
cat > /etc/yum.repos.d/nginx.repo << OK
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
OK
[root@nginx ~]# yum -y install nginx
[root@nginx ~]# cd /etc/nginx/conf.d/
[root@nginx conf.d]# mv default.conf{,.bak}
[root@nginx conf.d]# vim web01-test.conf
server {
listen 80;
server_name www.test.org;
root /code/web01;
location / {
index index.html;
}
}
[root@nginx conf.d]# mkdir -p /code/web01
[root@nginx conf.d]# echo "test-web01" >> /code/web01/index.html
[root@nginx conf.d]# chown -R www.www /code/
[root@nginx conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx conf.d]# systemctl enable nginx.service
[root@nginx conf.d]# systemctl start nginx.service
访问测试、查看web-02日志
[root@nginx conf.d]# tailf /var/log/nginx/access.log
192.168.200.120 - - [19/Jun/2023:13:12:12 +0800] "GET / HTTP/1.0" 200 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0" "192.168.200.10"
使用wireshark抓包查看详细过程
版权归原作者 梦有一把琐 所有, 如有侵权,请联系我们删除。