目录
正向代理
正向代理(Forward Proxy)是一种位于客户端和原始服务器之间的代理服
务器,其主要作用是将客户端的请求转发给目标服务器,并将响应返回给客户端
Nginx的正向代理充当客户端的"中间人",代表用户访问外部资源并隐藏
真实IP。它是企业内网管控、安全审计与加速访问的核心工具。
用于场景一般是:
内网访问控制:限制员工访问特定网站(如社交媒体)
匿名访问:通过代理服务器隐藏用户真实身份。
资源缓存加速:缓存公共资源(如软件包、镜像文件),减少外网带宽消耗
编译安装Nginx
Nginx的配置及运行需要pcre、zlib等软件包的支持,因此应预先安装这些软
件的开发
(devel),以便提供相应的库和头文件,确保Nginx的安装顺利完成.
安装支持软件
[root@localhost ~]# dnf install -y gcc make pcre-devel zlib-developenssl-devel perl-ExtUtils-MakeMaker git wget tar
创建运行用户,组和日志目录
[root@localhost#] useradd -M -s /sbin/nologin nginx
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost#] chown -R nginx:nginx /var/log/nginx
编译安装nginx
[root@localhost ^]# tar zxf nginx-1.26.3_http_proxy. tar.g2
[root@localhost ^]# cd nginx-1.26.3[root@localhost nginx-1.26.3]#./configure--prefix=/usr/local/nginx
[root@localhost nginx-1.26.3]# ln -s /usr/local/nginx/farmamainx/usr/local/sbin/
添加nginx系统服务
[root@localhost ^]# vi /lib/systemd/system/nginx. service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/sbin/nginx -t
ExecStart=/usr/local/sbin/nginx
ExecReload=/usr/local/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
User=root
Group=root
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ^]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx
配置正向代理
编辑主配置文件添加正向代理相关配置
server {
listen 8080;
server_name proxy.example.com;resolver 8.8.8.8 1.1.1.1;
proxy_connect;
proxy_connect_allow 443 80;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;location / {
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 -t
[root@localhost ~]nginx -s reload
反向代理
Nginx的七层(应用层)反向代理基于HTTP/HTTPS协议,深度解析应用层内容(如URL、Header、Cookie),将客户端请求精准转发至后端服务器。作为企业级架构的"智能调度器",它实现了负载均衡、安全隔离与性能优化的核心能力。应用场景一般是:
负载均衡:将流量分发至多台后端服务器,避免单点故障。动静分离:静态资源(图片、CSS/JS)由Nginx直接响应,动态请求(PHPAPI)转发至Apache/Tomcat。
SSL终端:统一处理HTTPS加密/解密,降低后端服务器订十算压力。
灰度发布:根据请求特征(如IP、Header)将部分流量导向新版本服务。
Nginx的四层(网络层)反向代理基于TCP/UDP协议,直接转发原始数居流,
不解析应用层内容。它专为高性能、低延迟的传输层场景设计,是数据库、游戏
服务器等非HTTP服务的理想选择。应用场景一般是:
数据库代理:对外暴露统一端口,内部转发至MySQL、Redis集群
游戏服务器:代理UDP协议,实现实时数据包负载均衡。
SSH跳板机:通过端口映射安全访问内网服务器。
高可用服务:TCP服务(如MQTT)的主备切换与健康检查。
配置nginx七层代理
环境安装
[root@localhost ~]#systemctl stop firewalld
[root@localhost~]#dnf install httpd -y
[root@localhost ~]#echo"这是后端主机">/var/www/html/index.html
[root@localhost ~]#systemctl start httpd
配置nginx七层代理转发
[root@localhost ^]# vi /usr/local/nginx/conf/nginx.conof
http {
upstream backend {
server 192.168.10.201:80 ;
server listen 80;
server_name example.com;
location / {
proxy_passhttp://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
location /static/ {
root/data/www;
expires 30d;}
}
}
[root@localhost~]#nginx -t
[root@localhost~}#nginx -s reload
验证转发效果
[root@localhost ~]# curl 192.168.10.202
这是后端主机
配置nginx四层代理
配置四层代理
[root@localhost~]#vi /usr/local/nginx/conf/nginx.conf
stream {
upstream ssh_cluster {
server 192.168.10.201:22;
server
listen 2222;
proxy_pass ssh_cluster;
proxy_connect_timeout 5s;
proxy_timeout lh;
[root@localhost~]#nginx -t
[root@localhost~]#nginx -s reload
[root@localhost ^]#ss -nlpt | grep 2222
LISTEN O 511 0.0.0.0:2222 0.0.0.0:*
users: (("nginx",pid=8404, fd=6), ("nginx",pid=8403, fd=6))
验证四层代理
[root@localhost~]# ssh root@192.168.10.202 -p2222
[root@localhost ~]#ifconfig
nginx缓存
缓存类型和核心原理
Nginx的缓存功能是其核心能力之一,主要用于加速内容响应和降低后端服务器负载。它的缓存功能主要基于反向代理(ProxyCache),但也可用于其他场景(如FastCGI缓存)。以下是详细解析:
缓存类型 作用场景
代理缓存 反向代理模式下缓存后端服务器(如Tomcat、Apache)的响应内容。
FastCGI缓存 缓存 PHP/Python等通过FastCGI协议处理的动态内容(需酉配合 PHP-FPM使用)。
uWSGI/SCGI 缓 类似FastCGI,用于其他后端协议。
静态资源缓存 通过expires指令设置客户端浏览器缓存(非服务端缓存)
代理缓存功能设置
第一步:客户端第一次向Nginx请求数据A;
第二步:当Nginx发现缓存中没有数据A时,会向服务端请求数据A
第三步:服务端接收到Nginx发来的请求,则返回数据A到Nginx,并且缓存在Nginx;
第四步:Nginx返回数据A给客户端应用;
第五步:客户端第二次向Nginx请求数据A;
第六步:当Nginx发现缓存中存在数据A时,则不会请求服务端;
第七步:Nginx把缓存中的数据A返回给客户端应用。
反向代理配置
[root@localhost ~]#/usr/local/nginx/conf/nginx.conf
[root@localhost ~]#nginx -t
[root@localhost ~]#nginx -s reload
设置缓存功能
[root@localhost ~]#mkdir-p/data/nginx/cache
[root@localhost ~]#chown nginx:nginx /data/nginx/cache -R
[root@localhost ~]#vim /usr/local/nginx/conf/nginx.conf
验证缓存功能
[root@localhost ~]# curl -I 192.168.10.202
[root@localhost ~]# curl -I 192.168.10.202
[root@localhost ~]#ls /data/nginx/cache
查看缓存目录发现已缓存数据
nginx rewrite和正则
在云计算与分布式架构的时代,Nginx凭借其高性能、高并发处理能力以及模块化设计,已成为现代Web服务的核心组件之一。它不仅是负载均衡、反向代理的首选工具,更是实现流量调度、安全防护和动态路由由的关键枢纽。而在这其中,Rewrite模块作为Nginx的"规则引擎",扮演着至关重要的角色--它赋予开发者精准控制URL的能力,让请求的流转不再受限于物理路经,而是通过逻辑规则灵活适配业务需求。
nginx正则
--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转发,需要添加第三方模块)
nginx location
在学习rewrite前还要了解下location,因为rewrite通常会与location结合使用,但并非绝对。二者的协作能实现更精细的路径控制,location是Nginx中用于匹配请求URI(路径:只能对域名后边的除去传递的参数外的字符串起作用。
location的语法
location[匹配模式]{
#处理逻辑(如root,proxypass,rewrite)}
location验证
[root@localhost ^]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ^]# nginx -s reload
[root@localhost ^]# curl 192.168.10.202/abc
精确匹配
rewrite
rewrite语法
rewrite <regex><replacement> [flag];
flag类型:
last:重写后的URI会重新触发location匹配,并执行新匹配到的location块中的指令,是默认类型
break:重写后的URI不会重新匹配 location,直接在当前location中处理,且后续的rewrite指令不再执行
redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url(因为是临时)
permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url。
rewrite flag验证
[root@localhost ^]# vim /usr/local/nginx/conf/nginx. conf
[root@localhost ^]# nginx -s reload
[root@localhost ^]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ^]# nginx -s reload
[root@localhost ^]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ^]# nginx -s reload
rewrite中的捕获组
在Nginx的rewrite指令中,小括号()用于定义正则表达式的捕获组(CaptureGroup),捕获的文本可以通过 $2,$3等变量在重写后的URI中引用
[root@localhost ~|# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ^]# curl http://localhost/category/tech/456
Category: tech, ID: 456
nginx中的set指令
在Nginx中,set指令用于定义变量并赋值,这些变量可以用于后续的条件判断、日志记录、重写规则等场景。它提供了灵活的动态配置能力,尤其在处理复杂的请求逻辑时非常有用。
[root@localhost ^]# vim /usr/local/nginx/conf/nginx.conf
测试防问
[root@localhost ^]# curl http://localhost/demo
Hello, Nginx!