Nginx-反向代理(配置教程)

 反向代理的作用

1、解决网站服务器对外可见的问题、私密性、安全性

2、路由功能:根据用户请求的URI调度到对应功能的节点处理请求

3、负载均衡:将用户的请求,通过调度算法挑选一台合适的节点处理请求

4、动静分离:根据用户请求的URI进行区分,将动态资源调度到应用服务器处理,将静态资源调度到静态资源服务器处理

5、数据缓存:加速网站的访问速度,减轻web服务器的负担。如果用户请求的内容在缓存中,可以直接在代理服务器中获取,加速用户的访问速度

web-01192.168.200.120
web-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抓包查看详细过程

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦有一把琐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值