十三、nginx的反向代理

一、反向代理的概念

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

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

反向代理:reverse proxy,可代理外网用户的请求到内部的指定web服务器,并将数据返回给用户
nginx除了可以在企业提供高性能的web服务之外,另外还可以将本身不具备的请求通过某种预定义的协议转发至其它服务器处理,不同的协议就是nginx服务器与其他服务器进行通信的一种规范
主要在不同的场景使用以下模块实现不同的功能:
ngx_http_proxy_module: 将客户端请求以http协议转发至后端服务器
ngx_http_fastcgi_module:将客户端对php请求以fastcgi协议转发至后端
ngx_http_uwsgi_module:将客户端对Python请求以uwsgi协议转发至后端
ngx_stream_proxy_module:将客户端请求以tcp协议转发至后端服务器

二、nginx反向代理作用

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

  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;

四、方向代理案例

案例1:

location和proxy_pass都不带uri路径。

代理服务器: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的发布目录文件。

案例2:

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/

案例3:

如果proxy_pass设置了uri路径,则需要注意,此时,proxy_pass指令所指定的uri会覆盖location的 uri。

代理服务器的配置:

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;
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 没有关系了!

五、获取远程客户端真实ip地址:

代理服务器配置:

location / {
proxy_pass http://192.168.0.114;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

后端服务器配置

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_real_ip"';
access_log logs/access.log main;

可以获取真是的访问ip,而非代理地址。

posted @ 2020-09-12 14:13  yaowx  阅读(224)  评论(0编辑  收藏  举报