当前位置: 首页 > news >正文

Nginx 核心功能02

目录

正向代理

编译安装 Nginx

配置正向解析 

反向代理(七层代理)

反向代理 (四层代理)

Nginx正则表达式

Rewrite语法

扩展跳转


正向代理

        正向代理(Forward Proxy)是一种位于客户端和原始服务器之间的代理服务器,其主要作用是将客户端的请求转发给目标服务器,并将响应返回给客户端Nginx 的 正向代理 充当客户端的“中间人”,代表用户访问外部资源并隐藏真实 IP。它是企业内网管控、安全审计与加速访问的核心工具。用于场景一般是:

  • 内网访问控制:限制员工访问特定网站(如社交媒体)
  • 匿名访问:通过代理服务器隐藏用户真实身份。
  • 资源缓存加速:缓存公共资源(如软件包、镜像文件),减少外网带宽消耗。

编译安装 Nginx

        安装Nginx所需要的依赖包(小实验环境,所以就没要那么多依赖包)

[root@localhost ~]# yum -y install gcc* pcre-devel zlib-devel openssl-devel

         创建运行用户、组和日志目录

[root@localhost ~]# useradd -M -s /sbin/nologin nginx    #创建用户,没有家目录,不能登录系统
[root@localhost ~]# mkdir -p /var/log/nginx                #创建nginx文件,这一步可以不用做
[root@localhost ~]# chown -R nginx:nginx /var/log/nginx    #属组的归属,这一步也可以不做 
后续的编译安装过程包含这两个

        编译安装 Nginx(将准备好的Nginx软件包通过远程的方式传上去)

        注:中间有重复的,没事的不影响所以就没删

[root@localhost ~]# tar zxf nginx-1.26.3_http_proxy.tar.gz    #解压缩
[root@localhost ~]# cd nginx-1.26.3            #切换到nginx-1.26.3[root@localhost ~]# ./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 --with-http_gzip_static_module --add-module=./ngx_http_proxy_connect_module        #编译安装[root@localhost ~]# make && make install        #这一步才是真正意义上的安装 巨慢

        各项参数说明: 

  • --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
  • --with-http_gzip_static_module       #支持gzip的压缩
  • --add-module=./ngx_http_proxy_connect_module      #支持https转发

        配置nginx的环境变量

[root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

        将Nginx添加到系统服务(让nginx可以支持systemctl的命令) 

        注:因为实验环境不需要没那么多的配置 所以配置的东西还是很少的

[root@localhost ~]# vim /lib/systemd/system/nginx.service[Unit]
Description=The nginx Server
After=network.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/sbin/nginx
ExecStop=/usr/local/sbin/nginx -s stop[Install]
WantedBy=multi-user.target

        注意: 一定要试一下 systemctl restart nginx 能不能用 虽然配置文件里面没有写 但系统是支持的 一定要尝试一下 start(启动)与 stop(停止)看看报错不报 报错的话 就重启下就好了  虚拟机环境 保存下好的快照 别的环境当我没说

配置正向解析 

vim /usr/local/nginx/conf/nginx.cnf
.....省略部分信息server {listen       8080;resolver 114.114.114.114 8.8.8.8;proxy_connect;proxy_connect_allow 80 443;proxy_connect_connect_timeout 10s;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;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

 

        重启 Nginx

 [root@localhost ~]# systemctl restart nginx

Windows 验证:

 首先,win10要开启代理可以通过此方法开启代理服务器:win+i 设置、网络与Internet、代理——使用代理服务器——将192.168.10.101:8080写入进去 ——保存 网页访问百度

Linux验证:

因Linux没有浏览器 所以只能使用此命令才可以看到效果:

[root@localhost ~]# curl -x http://192.168.10.101:8080 www.baidu.com

        效果其实并没有那么明显,我们这里可以访问 nginx的访问日志位置在:(我们刷新网站的时候 日志信息是会变的 可以看到 IP 访问的网站以及端口等内容)

[root@localhost ~]# /usr/local/nginx/logs/access.log

反向代理(七层代理)

        要准备两台WEB服务器要实现轮询的效果

        103、

[root@localhost ~]# systemctl stop firewalld        #关闭防火墙
[root@localhost ~]# dnf -y install httpd        #安装httpd
[root@localhost ~]# vim /var/www/html/index.html        #网站位置33333333333333333                    #网站内容
[root@localhost ~]# systemctl start httpd        #启动http服务

        104、 

[root@localhost ~]# systemctl stop firewalld        #关闭防火墙
[root@localhost ~]# dnf -y install httpd        #安装httpd
[root@localhost ~]# vim /var/www/html/index.html        #网站位置4444444444444444444                #网站内容
[root@localhost ~]# systemctl start httpd        #启动http服务

修改nginx.conf配置文件

vim /usr/local/nginx/conf/nginx.cof
....省略部分信息        upstream myhttp {server 192.168.10.103:80;server 192.168.10.104:80;}server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {#     root   html;#    index  index.html index.htm;proxy_pass http://myhttp;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}

        检测一下是否敲错:

[root@localhost ~]#  nginx -t

        重启

 [root@localhost ~]# systemctl restart nginx

        宿主机访问 192.168.10.101 看看有没有轮询(多刷新几次看看内容是否不一样)

反向代理 (四层代理)

vim /usr/local/nginx/conf/nginx.conf
......省略部分信息#在envents下面添加 stream
events {worker_connections  1024;
}stream {upstream myssh{server 192.168.10.102:22;}server {listen 2222;proxy_pass myssh;proxy_connect_timeout 10s;proxy_timeout 1h; }
}

        检测一下是否敲错:

[root@localhost ~]#  nginx -t

        重启nginx

 [root@localhost ~]# systemctl restart nginx

102 验证:

 

Nginx正则表达式

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf#location / {#   root   html;#   index  index.html index.htm;# }location / {return 200 "通用匹配";}location /aba {return 200 "普通的前缀匹配";}location ~ /test/bbb {return 200 "区分大小写";}location ~* /test/abc {return 200 "不区分大小写";}location ^~ /abcdef {return 200 "精确前缀匹配";}location = /abc {return 200 "精确匹配";}

        检测配置文件是否有错

 [root@localhost ~]#  nginx -t

        重启nginx

  [root@localhost ~]# systemctl restart nginx

         验证:

Rewrite语法

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
.....省略部分信息location /abc {rewrite ^/ /def last;		#注意 两个//中间是有空格的}location /def {return 200 "你好";}

        检测是否敲的有误

 [root@localhost ~]#  nginx -t

        重启nginx

   [root@localhost ~]# systemctl restart nginx

验证:

windows验证: 浏览器打开网站:192.168.10.101/abc 会自动下载一个文件 找到文件位置 打开方式为浏览器 火狐也行 打开就可以看到 /def 的内容

Linux验证:curl 192.168.10.101/abc 直接看到文件内容

扩展跳转

vim /usr/local/nginx/conf/nginx.conf# location / {#    root   html;#    index  index.html index.htm;#}location / {if ($host = 'www.kgc.com'){rewrite ^/(.*)$ http://www.baidu.com/$1 permanent;}}

        检测是否有误:

 [root@localhost ~]#  nginx -t

        重启nginx

 [root@localhost ~]# systemctl restart nginx

验证:

 Windows:用宿主机修改hosts文件 用浏览器访问域名 会自动跳转到百度

Linux:修改 /etc/hosts 文件 curl www.kgc.com 会显示301 是正常的

相关文章:

  • WGCLOUD使用 - 如何监控RabbitMQ运行参数
  • Python10天冲刺-设计模型之策略模式
  • 机器学习经典算法:用决策树原理优化新能源汽车续航能力
  • 【Redis】String详细介绍及其应用场景
  • 深度学习系统学习系列【1】之基本知识
  • npm宿主依赖、宿主环境依赖(peerDependencies)(指由宿主环境提供的依赖)
  • Ubuntu20.04如何优雅的安装ROS 1(胎教级教程)
  • Ocelot与.NETcore7.0部署(基于腾讯云)
  • loads、dumps、jsonpath使用场景
  • 上位机知识篇---二进制操作
  • 科学养生,解锁健康生活密码
  • 【信息系统项目管理师-论文真题】2023下半年论文详解(包括解题思路和写作要点)
  • 【DeepMLF】具有可学习标记的多模态语言模型,用于情感分析中的深度融合
  • npm命令介绍(Node Package Manager)
  • 2025五一杯数学建模竞赛B题 矿山数据处理 保姆级教程讲解|模型讲解
  • 2025五一杯数学建模ABC题赛题已出
  • SpringAI系列 - 升级1.0.0-M8
  • VBA数据库解决方案第二十讲:Select From Where条件表达式
  • CMake中的“包管理“模块FetchContent
  • 2025五一数学建模竞赛B题完整分析论文(共42页)(含模型、可运行代码、数据)
  • 乘客被困停滞车厢超4小时,哈尔滨铁路局客服:列车晚点,表示歉意
  • 马上评|启动最高层级医政调查,维护医学一方净土
  • 铁路上海站迎五一假期客流最高峰,今日预计发送77万人次
  • 剑指3000亿产业规模,机器人“武林大会”背后的无锡“野望”
  • 城市更新·简报│中央财政支持城市更新,倾斜超大特大城市
  • 辽宁辽阳市白塔区一饭店发生火灾,当地已启动应急响应机制