nginx入门详解(八)- 反向代理

概念

反向代理是nginx的一个重要功能,在编译安装时会默认编译该模块。在配置文件中主要配置proxy_pass指令。

代理服务器接受客户端的请求,然后把请求转发给后端真实服务器进行处理,然后再将服务器的响应结果返给客户端。

作用:

与正向代理(正向代理主要是代理客户端的请求)相反,反向代理主要是代理服务器返回的数据,所以它的作用主要有以下两点:

  1. 可以防止内部服务器被恶意攻击(内部服务器对客户端不可见)。
  2. 为负载均衡和动静分离提供技术支持。

语法:

Syntax:	 proxy_pass URL;
Default:	—
Context: location, if in location, limit_except

代理服务器的协议,可支持http与https。

地址可以指定为域名或IP地址,以及可选端口。

例如:

proxy_pass http://localhost:9000/uri/;
proxy_pass http://192.168.0.188:8080;
proxy_pass http://192.168.0.188;

实例一:

代理服务器:192.168.0.109

后端服务器:192.168.0.114

代理服务器的简单配置:

location / {
            proxy_pass http://192.168.0.114;
        }
        
# proxy_pass  转发请求给后端服务器
后端服务器的配置:
 location / {
            echo $host;
            root   html;
            index  index.html index.htm;
        }

# echo $host  这个主要是来看下后端接收到的Host是什么。

第一次验证:

[root@localhost ~]# curl 192.168.0.109
192.168.0.114

# 获取的请求Host是后端服务器ip,去掉该指令,验证请求结果。

[root@localhost ~]# curl 192.168.0.109
                         this is 114 page

# 可以看到我们访问的是109,但是得到的结果是114的发布目录文件。

实例二:

如果proxy_pass没有设置uri路径,但是代理服务器的location 有uri,那么代理服务器将把客户端请求的地址传递给后端服务器。

代理服务器的配置:

location /document/data/ {
            proxy_pass http://192.168.0.114;
}

后端服务器的配置:

 location / {
           # echo $host;
            root   html/uri;
            index  index.html index.htm;
        }

验证:

[root@localhost ~]# mkdir -p /usr/local/nginx/html/uri/document/data/
[root@localhost ~]# echo "this is /usr/local/nginx/html/uri/document/data/ test" > /usr/local/nginx/html/uri/document/data/index.html
[root@localhost ~]# curl 192.168.0.109/document/data/
this is /usr/local/nginx/html/uri/document/data/ test

# 完整请求路径 是在后端服务器的/usr/local/nginx/html/uri 后追加客户端请求的路径 /document/data/

实例三:

如果proxy_pass设置了uri路径,则需要注意,此时,proxy_pass指令所指定的uri会覆盖后端服务器的root指令。

代理服务器的配置:

location / {
            proxy_pass http://192.168.0.114/data/;
        }
        
后端服务器的配置:

location / {
            root   html;
            index  index.html index.htm;
        }

验证:

[root@localhost ~]# mkdir -p /usr/local/nginx/html/data/
[root@localhost ~]# echo "this is /usr/local/nginx/html/data test。" > /usr/local/nginx/html/data/index.html
[root@localhost ~]# curl 192.168.0.109
this is /usr/local/nginx/html/data test。


这样看好像很正常。但是我们稍作修改。

再次验证,这次加上location的uri,后端服务器加个子目录:

代理服务器的配置:


location /document/ {
            proxy_pass http://192.168.0.114/data/;
}

后端服务器的配置:

 location / {
            #echo $host;
            root   html/uri;
            index  index.html index.htm;
        }

验证:

[root@localhost ~]# curl 192.168.0.109/document/
this is /usr/local/nginx/html/data test。

#该路径还是 proxy_pass 指定的uri路径,与location 和后端的root指令都没有关系了!

这是反代单台服务器,如果是多台服务器呢?那就涉及到负载均衡了。下一章节,我们就探讨负载均衡。

这将是nginx系列文章,可关注同名微信公众号:笨办法学linux 获取最近文章更新及精品软件,软件持续更新中。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

运维朱工

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

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

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

打赏作者

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

抵扣说明:

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

余额充值