Nginx核心功能——nginx代理
目录
一:正向代理
二:反向代理
Nginx rewrite和正则
1,Nginx正则
2,nginx location
3, location验证
4,Rewrite语法
5,Rewrite域名跳转
一:正向代理
正向代理(Forward Proxy)是一种位于客户端和原始服务器之间的代理服务器,其主要作用是将客户端的请求转发给目标服务器,并将响应返回给客户端Nginx 的 正向代理 充当客户端的“中间人”,代表用户访问外部资源并隐藏真实 IP。它是企业内网管控、安全审计与加速访问的核心工具。用于场景一般是:
- 内网访问控制:限制员工访问特定网站(如社交媒体)
- 匿名访问:通过代理服务器隐藏用户真实身份。
- 资源缓存加速:缓存公共资源(如软件包、镜像文件),减少外网带宽消耗。
1,编译安装nignx
[root@localhost ~]# ls ##准备好ningx1.26.3的安装包
anaconda-ks.cfg nginx-1.26.3_http_proxy.tar.gz[root@localhost ~]# tar zxvf nginx-1.26.3_http_proxy.tar.gz ##并进行解压
[root@localhost ~]# useradd -M -s /sbin/nologin nginx ##创建程序用户
[root@localhost ~]# dnf -y install gcc* pcre-devel zlib-devel openssl-devel ##安装nginx依赖文件[root@localhost nginx-1.26.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=./ngx_http_proxy_connect_module
##编译安装并添加部分模块[root@localhost nginx-1.26.3]# make && make install[root@localhost nginx-1.26.3]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin ##创建软链接
[root@localhost nginx-1.26.3]# nginx ##启动nginx
[root@localhost nginx-1.26.3]# netstat -anpt |grep nginx ##查看nginx监听端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9324/nginx: master
2,编译安装参数说明
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=./ngx_http_proxy_connect_module
参数 | 说明 |
--user=nginx | 指定nginx运行用户 |
--group=nginx | 指定nginx运行组 |
-with-http_ssl_module | 配置支持https:// |
with-http_v2_module | 配置支持http版本2 |
--with-http_realip_module | 配置支持ip透传 |
--with-http_stub_status_module | 配置支持状态页面 |
with-http_gzip_static_module | 配置支持压缩 |
--with-pcre | 配置支持正则表达式 |
--with-stream | 配置支持tcp反向代理 |
--with-stream_ssl_module | 配置支持tcp的ssl加密 |
-with-stream_realip_module | 配置支持tcp的透传ip |
--add-module=./ngx_http_proxy_connect_module | 配置支持https转发(nginx默认不支持https转发,需要添加第三方模块) |
3,配置正向代理
[root@localhost nginx-1.26.3]# vim /usr/local/nginx/conf/nginx.conf ##编辑nginx配置文件server {listen 8080; ##代理监听端口resolver 114.114.114.114 8.8.8.8; ##解析域名使用的DNSproxy_connect; ##启用代理proxy_connect_allow 443 80; ##配置允许代理到443 80 端口proxy_connect_connect_timeout 10s; ##配置超时时间为10秒proxy_connect_read_timeout 10s; ##配置访问超时时间proxy_connect_send_timeout 10s; ##配置发送超时时间server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {# root html;#index index.html index.htm;##处理HTTP/HTTPS请求proxy_pass $scheme://$http_host$request_uri; ##动态协议proxy_set_header Host $http_host;#优化缓存区proxy_buffers 256 4k;proxy_max_temp_file_size 0;#保持连接proxy_http_version 1.1; proxy_set_header Connection "";
}[root@localhost nginx-1.26.3]# nginx -t ##检查nginx配置文件是否错误
[root@localhost nginx-1.26.3]# nginx -s reload ##重载nginx服务
4,验证
- 在win10客户端配置代理服务器
在win10客户机打开代理服务器
访问百度网页
在nginx服务器上查看访问状态
查看nginx日志文件可以看到win10客户机的访问情况
[root@localhost /]# cat /usr/local/nginx/logs/access.log192.168.10.10 - - [01/May/2025:10:38:55 +0800] "GET http://www.msftconnecttest.com/connecttest.txt HTTP/1.1" 200 22 "-" "Microsoft NCSI"
二:反向代理
反向代理,指的是浏览器/客户端并不知道自己要访问具体哪台目标服务器,只知道去访问代理服务器,代理服务器再通过反向代理 +负载均衡实现请求分发到应用服务器的-种代理服务。
反向代理服务的特点是代理服务器 代理的对象是应用服务器,也就是对于浏览器/客户端 来说应用服务器是隐藏的。
1,Nginx的七层(应用层)反向代理
Nginx的七层(应用层)反向代理基于HTTP/HTTPS 协议,深度解析应用层内容(如URL、Header、Cookie),将客户端请求精准转发至后端服务器。作为企业级架构的“智能调度器”,它实现了负载均衡、安全隔离与性能优化的核心能力。应用场景一般是:
- 负载均衡:将流量分发至多台后端服务器,避免单点故障。
- 动静分离:静态资源(图片、CSS/JS)由 Nginx直接响应,动态请求(PHP、API)转发>至 Apache/Tomcat.
- SSL 终端:统一处理 HTTPS 加密/解密,降低后端服务器计算压力。
- 灰度发布:根据请求特征(如IP、Header)将部分流量导向新版本服务
##准备两个后端服务器 在102 103上分别安装httpd服务
[root@localhost ~]# dnf -y install httpd##102配置网页
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# echo "这是后端主机102" > /var/www/html/index.html[root@localhost ~]# systemctl restart httpd ##重启httpd服务
[root@localhost ~]# curl 192.168.10.102 ##验证本机是否可以访问
这是后端主机102##103配置网页
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# echo "这是后端主机103" > /var/www/html/index.html[root@localhost ~]# systemctl restart httpd ##重启httpd服务
[root@localhost ~]# curl 192.168.10.103 ##验证本机是否可以访问
这是后端主机103
配置nginx七层代理转发
[root@localhost conf]# cp nginx.conf.default nginx.conf ##覆盖原有的nginx配置文件
cp: 是否覆盖 'nginx.conf'?yes[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf#gzip on;upstream backend { ##配置后端地址池 server 192.168.10.102:80;server 192.168.10.103:80;}location / {root html;index index.html index.htm;proxy_pass http://backend; ##配置请求转发proxy_set_header Host $host; ##设置转发到后端服务器的 HTTP Host 头proxy_set_header X-Real-IP $remote_addr; ##设置一个自定义头 X-Real-IP,其值为客户端的真实 IP 地址}[root@localhost /]# nginx -t ##检测配置文件是否有误
[root@localhost /]# nginx -s reload ##重载nginx服务
通过访问101主机nginx服务可以直接跳转到后端主机102,103
2,Nginx的四层(网络层)反向代理
Nginx的四层(网络层)反向代理基于 TCP/UDP 协议,直接转发原始数据流,不解析应用层内容。它专为高性能、低延迟的传输层场景设计,是数据库、游戏服务器等非 HTTP 服务的理想选择。应用场景一般是:
- 数据库代理:对外暴露统一端口,内部转发至 MySQL、Redis 集群。
- 游戏服务器:代理 UDP 协议,实现实时数据包负载均衡。
- SSH 跳板机:通过端口映射安全访问内网服务器。
- 高可用服务:TCP 服务(如MQTT)的主备切换与健康检查
##在nginx.conf配置文件新添加以下内容 注意:stream需要与http{}模块平级,不能在http{}中嵌套
stream {upstream ssh_cluster {server 192.168.10.102:22;}server {listen 2222;proxy_pass ssh_cluster;proxy_connect_timeout 5s;proxy_timeout 1h;}}[root@localhost conf]# nginx -s reload ##重载nginx[root@localhost conf]# netstat -anpt |grep 2222 ##查看nginx监听的端口
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 9552/nginx: master
验证
[root@localhost ~]# ssh root@192.168.10.101 -p2222 ##在104主机上远程101[root@localhost ~]# ifconfig ##连接成功后发现连接的是102主机
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.10.102 netmask 255.255.255.0 broadcast 192.168.10.255
Nginx rewrite和正则
在云计算与分布式架构的时代,Nginx凭借其高性能、高并发处理能力以及模块化设计,已成为现代web服务的核心组件之一。它不仅是负载均衡、反向代理的首选工具,更是实现流量调度、安全防护和动态路由的关键枢纽。而在这其中,Rewrite模块作为Nginx的“规则引擎”,扮演着至关重要的角色--它赋予开发者精准控制URL的能力,让请求的流转不再受限于物理路径,而是通过逻辑规则灵活适配业务需求。
Rewrite的应用场景:
- 路径美化:将/product/123转换为/index.php?id=123
- 旧链接迁移:将过期URL永久重定向(301)到新地址
- 强制HTTPS/域名统一:自动跳转http://到https://,或合并www与非www域名
- 动态路由:适配单页应用(SPA)、RESTfu1API路由
- 灰度发布:按规则将部分流量导向新版本服务
1,Nginx正则
字符 | 描述 |
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次。如“ol*"能匹配"o”及“ol”、"oll" |
+ | 匹配前面的字符一次或多次。如“ol+”能匹配"o!"及"o”、“o”,但不能匹配"o” |
? | 匹配前面的字符零次或一次,例如“do(es)?"能匹配“do"或者"does","?"等效于”{0,1}” |
. | 匹配除“n"之外的任何单个字符,若要匹配包括“""在内的任意字符,请使用诸如“[.\n]之类的模式 |
\ | 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n"匹配一个换行符,而“S"则匹配“$” |
\d | 匹配纯数字 |
{n} | 重复n次 |
{n.} | 重复n次或更多次 |
[c] | 匹配单个字符c |
[a-z] | 匹配 a-z 小写字母的任意一个 |
[a-zA-Z] | 匹配 a-z 小写字母或 A-Z 大写字母的任意一个 |
2,nginx location
location语法:
location [匹配模式] {
#处理逻辑 (如root, proxy_pass, rewrite等)
}
匹配模式类型
模式 | 说明 |
location /uri | 普通前缀匹配:匹配以指定路径开头的URI。 |
location = / | :匹配以指定路径开头的URI。location=精确匹配:仅匹配完全相同的URI(优先级最高)。 |
location ~ | 正则匹配:区分大小写的正则表达式匹配。 |
location ~* | 正则匹配:不区分大小写的正则表达式匹配。 |
location `~ | 精确前缀匹配:匹配前缀路径后,不再检查正则匹配(优先级高于正则) |
location / | 通用匹配:默认方式,优先级最低,其他方式匹配不到时匹配 |
location的优先级规则:
精确匹配 > 精确前缀匹配 > 正则匹配(~和~*同时存在时,文件中物理位置靠上的优先) > 普通前缀匹配 > 通用匹配
3, location验证
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf ##添加以下内容location / {#root html;#index index.html index.htm;location / {return 200 "通用匹配";}location /abc {return 200 "普通前缀匹配";}location ~ /bbb {return 200 "区分大小写";}location ~* /test/abd {return 200 "不区分大小写";}location ^~ /abcdef {return 200 "精准前缀匹配";}location = / {return 200 "精确匹配";}
}##验证测试
[root@localhost conf]# curl 192.168.10.101/abc
普通前缀匹配[root@localhost conf]# curl 192.168.10.101/bbb
区分大小写[root@localhost conf]# curl 192.168.10.101/abd
不区分大小写[root@localhost conf]# curl 192.168.10.101/test/abcdef
通用匹配[root@localhost conf]# curl 192.168.10.101/abcdef
精准前缀匹配[root@localhost conf]# curl 192.168.10.101
精确匹配[root@localhost conf]#
4,Rewrite语法
rewrite <regex> <replacement> [flag];
- regex:正则匹配URL字符串(只能对域名后边的除去传递的参数外的字符串起作用,例如http://www.kgc.com/index.php?id=1只对/index.php重写)
- replacement:重写跳转后的地址
flag类型:
- last:重写后的 URI 会重新触发1ocation 匹配,并执行新匹配到的1ocation块中的指令,是默认类型
- 中处理,
- break:重写后的 URI 不会重新匹配 location,直接在当前 location且后续的 rewrite 指令不再执行
- redirect:返回302临时重定向,浏览器地址会显示跳转后的 URL 地址,爬虫不会更新url(因为是临时)
- permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL 地址,爬虫更新url
1,rewrite flag验证
##在nginx.con的location中添加 location / {root html;index index.html index.htm;location /abc {rewrite ^/ /def last;
}location /def {return 200 "this is def";
}}nginx -s reload ##重载nginx生效
使用浏览器请求,发现相应内容是:this is def.说明1ast标记后继续向下匹配location
location / {root html;index index.html index.htm;location /abc {rewrite ^/ /def break; ##改为break
}location /def {return 200 "this is def";
}}nginx -s reload ##重载nginx生效
改成break标记,使用浏览器请求,发现请求页面未找到(因为网页代码目录确实不存在/def的内容)说明break标记使用当前结果不继续向下匹配了
5,Rewrite域名跳转
server {listen 80;server_name www.aaa.com;location / {root html;index index.html index.htm;location / {
if ($host = 'www.aaa.com') ###添加跳转if语句
{
rewrite ^/(.*)$ http://www.baidu.com/$1 permanent; ##设置跳转到baidu
}
}}nginx -s reload ##重启nginx
##在linux虚拟机验证 验证前在hosts文件中添加 192.168.10.101 www.aaa.coom
[root@localhost ~]# curl www.aaa.com
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.26.3</center>
</body>
</html>
在win10客户机进行测试
先修改hosts文件添加 192.168.10.101 www.aaa.coom
输入www.aaa.com时则会自动跳转到baidu