Nginx反向代理功能-实现HTTP反向代理客户端IP透传案例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.Nginx反向代理概述
1>.什么是反向代理
反向代理也叫reverse proxy,指的是代理外网用户的请求到内部的指定web服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。
2>.Nginx支持的反向代理
Nginx除了可以在企业提供高性能的web服务之外,另外还可以将本身不具备的请求通过某种预定义的协议转发至其它服务器处理,不同的协议就是Nginx服务器与其他服务器进行通信的一种规范,主要在不同的场景使用以下模块实现不同的功能:
ngx_http_proxy_module:
将客户端的请求以http协议转发至指定服务器进行处理。
ngx_stream_proxy_module:
将客户端的请求以tcp协议转发至指定服务器处理,很少有人使用Nginx来跑TCP的反向代理,因为它的这种功能出来的比较晚,一般TCP反向代理使用Haproxy或者LVS的居多。
ngx_http_fastcgi_module:
将客户端对php的请求以fastcgi协议转发至指定服务器助理,比如使用Laravel,Yii Framework等Web开发工具开发的程序就可以使用这种方式部署LNMP
ngx_http_uwsgi_module:
将客户端对Python的请求以uwsgi协议转发至指定服务器处理,比如使用Django等Web开发的程序就可以使用这种方式部署LNMP。
博主推荐阅读:
https://nginx.org/en/docs/http/ngx_http_proxy_module.html
3>.Nginx逻辑调用关系如下图所示
二.试验环境说明
1>.虚拟机操作系统配置
[root@node101.yinzhengjie.org.cn ~]# uname -r 3.10.0-957.el7.x86_64 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# uname -m x86_64 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# free -h total used free shared buff/cache available Mem: 7.6G 152M 6.7G 9.7M 851M 7.2G Swap: 7.9G 0B 7.9G [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# hostname -i 172.30.1.101 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.30.1.101 node101.yinzhengjie.org.cn node101.yinzhengjie.com 172.30.1.102 node102.yinzhengjie.org.cn 172.30.1.103 node103.yinzhengjie.org.cn 172.30.1.104 node104.yinzhengjie.org.cn 172.30.1.105 node105.yinzhengjie.org.cn 172.30.1.106 node106.yinzhengjie.org.cn 172.30.1.107 node107.yinzhengjie.org.cn 172.30.1.108 node108.yinzhengjie.org.cn [root@node101.yinzhengjie.org.cn ~]#
2>.反向代理说明
node101.yinzhengjie.org.cn:
Nginx反向代理服务器
node108.yinzhengjie.org.cn:
Apache httpd的web服务器
三.Apache httpd服务器部署
1>.安装Apache httpd服务
[root@node108.yinzhengjie.org.cn ~]# yum -y install httpd Loaded plugins: fastestmirror Determining fastest mirrors * base: mirrors.neusoft.edu.cn * extras: mirror.bit.edu.cn * updates: mirror.bit.edu.cn base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/4): extras/7/x86_64/primary_db | 153 kB 00:00:00 (2/4): base/7/x86_64/primary_db | 6.0 MB 00:00:01 (3/4): updates/7/x86_64/primary_db | 5.9 MB 00:00:02 (4/4): base/7/x86_64/group_gz | 165 kB 00:00:04 Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed --> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.4.8-5.el7 will be installed ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed ---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================ Package Arch Version Repository Size ============================================================================================================================================================================ Installing: httpd x86_64 2.4.6-90.el7.centos base 2.7 M Installing for dependencies: apr x86_64 1.4.8-5.el7 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k httpd-tools x86_64 2.4.6-90.el7.centos base 91 k Transaction Summary ============================================================================================================================================================================ Install 1 Package (+3 Dependent packages) Total download size: 3.0 M Installed size: 9.9 M Downloading packages: (1/4): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:00 (2/4): httpd-2.4.6-90.el7.centos.x86_64.rpm | 2.7 MB 00:00:00 (3/4): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm | 91 kB 00:00:00 (4/4): apr-1.4.8-5.el7.x86_64.rpm | 103 kB 00:00:05 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 505 kB/s | 3.0 MB 00:00:06 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.4.8-5.el7.x86_64 1/4 Installing : apr-util-1.5.2-6.el7.x86_64 2/4 Installing : httpd-tools-2.4.6-90.el7.centos.x86_64 3/4 Installing : httpd-2.4.6-90.el7.centos.x86_64 4/4 Verifying : apr-1.4.8-5.el7.x86_64 1/4 Verifying : httpd-tools-2.4.6-90.el7.centos.x86_64 2/4 Verifying : apr-util-1.5.2-6.el7.x86_64 3/4 Verifying : httpd-2.4.6-90.el7.centos.x86_64 4/4 Installed: httpd.x86_64 0:2.4.6-90.el7.centos Dependency Installed: apr.x86_64 0:1.4.8-5.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-90.el7.centos Complete! [root@node108.yinzhengjie.org.cn ~]#