Nginx - 反向代理与负载均衡
目录
一、Nginx
1.1、Nginx 下载
1.2、nginx 基础配置的认识
a)第一部分:全局块
b)第二部分:events 块
c)第三部分:http 块
http 块中 内嵌的 server 块
1.3、一些常用配置
1.3.1、location 匹配级别
a)location /
b)location =
c)location ^~
1.3.2、实现反向代理
1.3.3、nginx 配置负载均衡
a)weight 权重
b)ip_hash
c)fair
1.3.4、nginx 引入模块
一、Nginx
1.1、Nginx 下载
a)打开官方网站连接:nginx news
Ps:这里以 nginx-1.25.0 为例

b)解压缩后,进入以下文件路径.

c)打开,输入 nginx.exe,回车即可,如下:

d)打开网页,访问 localhost:80,即可看到 Nginx 页面(OpenResty 就是 Nginx)

1.2、nginx 基础配置的认识
在 nginx-1.24.0\conf\ 路径下有一个 nginx.conf 文件,打开如下:
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
这里有很多注释,删除掉以后,如下:
worker_processes 1;
events {worker_connections 1024;
}
http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name localhost;location / {root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
这里分开来看,如下:
nginx 配置文件主要有三部分组成:
a)第一部分:全局块
全局块:nginx 服务器全局生效的配置命令
worker_processes 1; # 服务器并发处理能力,值越大并发能力越强(受自身配置限制)
b)第二部分:events 块
events:影响 nginx 和用户网络的连接.
events {worker_connections 1024; #最大连接数1024个,需灵活配置
}
c)第三部分:http 块
http块:包括文件引入、MIME-TYPE 定义,日志自定义、连接超时等.
http {include mime.types; # 文件扩展名与文件类型映射表default_type application/octet-stream; # 访问到未定义的扩展名的时候,就默认为下载该文件sendfile on; # 日志自定义keepalive_timeout 65; # 超时时间
http 块中 内嵌的 server 块
和虚拟主机有关系,主要是未来节省硬件成本.
一个 http 块可以包含多个 server 块,而一个 server 块就等于一个虚拟主机.
server 块中又抱哈了 server 块 和 location 块
全局 server 块:
server {listen 80; # 目前监听的端口号server_name localhost; # 主机名称
location 块:
location / { #表示默认首页root html;index index.html index.htm;
root html 就是根路径,也就是通过 openresty-1.21.4.2-win64\html\ 路径下,去找页面(默认是 index.html 页面,也就是文章开头展示的页面).

最后是对错误页面的描述
error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
如果请求出现了 500、502、503、504 错误,就会进入到 50x.html 页面中(一般不会用这些默认的错误页,因此可以将这个配置也删除掉)。
1.3、一些常用配置
1.3.1、location 匹配级别
拿 echo 插件来举例:echo 就是会像网页上输出一些东西.
这里需要先在 server 中配置 "default_type text/html" ,否则会走默认的 http 块中的下载,如下:
server {listen 80;server_name localhost;default_type text/html;location / {echo "hello nginx";}}
在 openresty-1.21.4.2-win64\ 路径下重新打开一个 cmd 窗口,输入 nginx.exe -s reload 进行重启.

打开浏览器输入 localhost:80 即可访问.

a)location /
/ 就表示匹配以 "/" 开头的所有请求,例如 location/a、location/ajfdioabgua .......

b)location =
= 优先级最高,表示完全匹配,例如 location = /a 就表示只匹配路由 location/a,其他的都不可以.
配置如下:
server {listen 80;server_name localhost;default_type text/html;location / {echo "hello nginx";}location = /a {echo "=/a";}}
cmd 窗口,输入 nginx.exe -s reload 进行重启.


c)location ^~
^~ 优先级比 location / 高,但是匹配规则和 location / 类似. 例如 location ^~ /a 就表示匹配以 /a 开头的所有路由.
例如配置如下:
server {listen 80;server_name localhost;default_type text/html;location / {echo "hello nginx";}location = /a {echo "=/a";}location ^~ /a {echo "^~ /a";}}
重启 nginx 服务.

1.3.2、实现反向代理
修改 nginx.conf 如下:
server {listen 80;server_name localhost;default_type text/html;location / {proxy_pass "https://www.baidu.com";}}
重启 nginx
访问 localhost 即可转到 "https://www.baidu.com".

有一些额外需要注意的如下:
location /a {proxy_pass http://ip;}上述配置会导致你在浏览器中输入以下网址:
localhost/a/xxx => http://ip/a/xxx
location /a/ {proxy_pass http://ip/;}上述配置会导致你在浏览器中输入以下网址:
localhost/a/xxx => http://ip/xxx
1.3.3、nginx 配置负载均衡
通过 upstream 来创建一组需要负载均衡服务(默认是轮询策略,如果某个服务器挂了,自动剔除).
upstream group1 {server 192.168.0.12:80;server 192.168.0.12:81;}server {listen 80;server_name localhost;default_type text/html;location /a {proxy_pass "https://group1";}}
a)weight 权重
另外可以通过 weight 来控制需要负载均衡的权重. 权重越大,访问到的概率越大.
比如将权重都配置为 1,表示两者访问到的概率相同.
upstream group1 {server 192.168.0.12:80 weight=1;server 192.168.0.12:81 weight=1;}server {listen 80;server_name localhost;default_type text/html;location /a {proxy_pass "https://group1";}}
或者将 80 端口的权重改为 10,让其访问到的概率大一些.
upstream group1 {server 192.168.0.12:80 weight=10;server 192.168.0.12:81 weight=1;}server {listen 80;server_name localhost;default_type text/html;location /a {proxy_pass "https://group1";}}
b)ip_hash
每个请求按访问 ip 的hash 结果分配,这样子访客固定访问一个后端服务器,可以解决session问题
举个例子:
A用户固定ip,第一次访问到8080 tomcat,那么后面就都是访问到这台机器.
upstream myserver {ip_hash;server 127.0.0.1:8080;server 127.0.0.1:8081;
}
c)fair
根据后端响应时间来分配请求,处理时间短的优先分配.
upstream myserver {server 127.0.0.1:8080;server 127.0.0.1:8081;fair;
}
1.3.4、nginx 引入模块
在 http 块中通过 include 可以引入其他指定的 conf 配置.
也就意味着,你可以创建一个文件夹,在这个文件夹下创建很多配置文件,一并引入. 例如,引入 web 目录下的所有 conf 文件.
http {include mime.types;default_type text/html;sendfile on;keepalive_timeout 65;# 引入自定义配置文件include web/*.conf;
}

web 目录下的 conf 就可以配置反向代理、负载均衡...
upstream demo1 {server localhost:10010;
}
server {listen 8079;location / {root D:/webapps/;index login.html;}location /api {proxy_pass http://demo1;# proxy_set_header HOST $host; # 不改变源请求头的值# proxy_pass_request_body on; #开启获取请求体# proxy_pass_request_headers on; #开启获取请求头# proxy_set_header X-Real-IP $remote_addr; # 记录真实发出请求的客户端IP# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #记录代理信息}
}

相关文章:
Nginx - 反向代理与负载均衡
目录 一、Nginx 1.1、Nginx 下载 1.2、nginx 基础配置的认识 a)第一部分:全局块 b)第二部分:events 块 c)第三部分:http 块 http 块中 内嵌的 server 块 1.3、一些常用配置 1.3.1、location 匹配级…...
Linux网络编程系列之UDP组播
一、什么是UDP组播 UDP组播是指使用用户数据报协议(UDP)实现的组播方式。组播是一种数据传输方式,允许单一数据包同时传输到多个接收者。在UDP组播中,一个数据包可以被多个接收者同时接收,这样可以降低网络传输的负载和…...
设计模式~状态模式(state)-23
目录 (1)优点: (2)缺点: (3)使用场景: (4)注意事项: (5)应用实例: 代码 在状态模式(State Pattern)中,类的行为是基于它的状态改变的。这种类型的设计模式属于行为型模式。在状…...
linux环境下使用lighthouse与selenium
一、安装谷歌浏览器、谷歌浏览器驱动、lighthouse shell脚本 apt update && apt -y upgrade apt install -y curl curl -fsSL https://deb.nodesource.com/setup_18.x | bash apt install -y nodejs apt install -y npm npm install -g lighthouse apt-get install -y …...
NeuroImage | 右侧颞上回在语义规则学习中的作用:来自强化学习模型的证据
在现实生活中,许多规则的获取通常需要使用语言作为桥梁,特别是语义在信息传递中起着至关重要的作用。另外,个体使用的语言往往具有明显的奖励和惩罚元素,如赞扬和批评。一种常见的规则是寻求更多的赞扬,同时避免批评。…...
uni-app编程checkbox-group获取选中的每个checkbox的value值
uni-app编程checkbox-group获取选中的每个checkbox的value值_uniappcheckboxvalue-CSDN博客...
数组——螺旋矩阵II
文章目录 一、题目二、题解 题目顺序:代码随想录算法公开课,b站上有相应视频讲解 一、题目 59. Spiral Matrix II Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. Example 1: Input: n …...
反范式化设计
反范式化设计与范式化设计相对立。范式化设计是将数据组织成多个表,以最小化数据的冗余和提高数据一致性。相反,反范式化设计是故意增加冗余,以提高查询性能和降低复杂性。反范式化设计通常用于需要高度优化的读取密集型应用程序,…...
CCF CSP认证 历年题目自练Day31
题目一 试题编号: 202206-1 试题名称: 归一化处理 时间限制: 500ms 内存限制: 512.0MB 题目背景 在机器学习中,对数据进行归一化处理是一种常用的技术。 将数据从各种各样分布调整为平均值为 0、方差为 1的标准分布&a…...
PCL点云处理之从两片点云中获取具有匹配关系的同名点对 (二百一十八)
PCL点云处理之从两片点云中获取具有匹配关系的同名点对 (二百一十八) 一、算法介绍二、算法实现1.代码2.效果一、算法介绍 点云配准的前提是,我们知道或者预测了一些匹配对,我们认为这些匹配对就是两片点云中的同名点,同名点就是由于激光扫描存在误差的关系,导致同一地物…...
MySQL Row size too large (> 8126)
错误信息 ERROR 1118 (42000) at line 901: Row size too large (> 8126). Changing some columns to TEXT or BLOB or using ROW_FORMATDYNAMIC or ROW_FORMATCOMPRESSED may help. In current row format, BLOB prefix of 768 bytes is stored inline. 错误原因 这个问题…...
HUAWEI(26)——防火墙双机热备
一、拓扑 二、需求 PC2 ping PC1 FW1与FW2双机热备,FW1为active,FW2为Standby,抢占延时1s VRRP 三、配置 1.IP地址,防火墙接口加入区域 防火墙用户名:admin 防火墙旧密码:Admin@123 防火墙新密码:admin@123 [FW1]interface GigabitEthernet 1/0/0 [FW1-GigabitEthe…...
【ArcGIS】NDVI估算植被覆盖度FVC
NDVI估算植被覆盖度FVC NDVI计算植被覆盖度FVC计算NDVI估算植被覆盖度FVC操作步骤Step1:调出栅格计算器工具Step2:查找NDVIStep3: 参考 NDVI计算 植被覆盖度FVC计算 NDVI估算植被覆盖度FVC操作步骤 Step1:调出栅格计算器工具 1、首先打开软件&#x…...
vscode用密钥文件连接ssh:如果一直要输密码怎么办
commandshiftP:打开ssh配置文件 加上这么一段,host就是你给主机起的名字 对IdentityFile进行更改,改成相应的密钥文件 然后commandshiftP链接到主机就可以了 但是有时候它会让输入密码 这是由于你给这个IdentityFile的权限太多了…...
【AI视野·今日Robot 机器人论文速览 第五十三期】Thu, 12 Oct 2023
AI视野今日CS.Robotics 机器人学论文速览 Thu, 12 Oct 2023 Totally 25 papers 👉上期速览✈更多精彩请移步主页 Daily Robotics Papers Pixel State Value Network for Combined Prediction and Planning in Interactive Environments Authors Sascha Rosbach, St…...
【LeetCode第115场双周赛】100029. 和带限制的子多重集合的数目 | 前缀和背包 | 中等
题目内容 原题链接 给定一个长度为 n n n 的数组 n u m s nums nums 和一个区间左右端点 [ l , r ] [l,r] [l,r] 。 返回 n u m s nums nums 中子多重集合的和在闭区间 [ l , r ] [l, r] [l,r] 之间的 子多重集合的数目 。 子多重集合 指的是从数组中选出一些元素构成的 …...
ArcGIS笔记5_生成栅格文件时保存报错怎么办
本文目录 前言Step 1 直接保存到指定文件夹会报错Step 2 先保存到默认位置再数据导出到指定文件夹 前言 有时生成栅格文件时,保存在自定义指定的文件夹内会提示出错,而保存到默认位置则没有问题。因此可以通过先保存到默认位置,再数据导出到…...
YOLO目标检测——跌倒摔倒数据集【含对应voc、coco和yolo三种格式标签】
实际项目应用:公共安全监控、智能家居、工业安全等活动区域无监管情况下的人员摔倒事故数据集说明:YOLO目标检测数据集,真实场景的高质量图片数据,数据场景丰富。使用lableimg标注软件标注,标注框质量高,含…...
uniapp小程序实现绘制内容,生成海报并保存截图(Painter和Canvas两种方式举例)
一、Painter方法 Painter插件传送门 1.下载资源包 2.将资源包的如下部分 3.使用页面引入组件 ui样式 <paintercustomStyle=margin-left: 40rpx; height: 1000rpx;palette="{{palette}}"bind:imgOK="onImgOK"/>data 中数据(绘制内容,替换区域) pai…...
HTTPS双向认证及密钥总结
公钥私钥: 1)公钥加密,私钥解密:加解密 为什么不能私钥加密公钥解密? 私钥加密后,公钥是公开的都能解密,没有意义。 2)私钥签名,公钥验签:属于身份验证,防串改&#x…...
RestClient
什么是RestClient RestClient 是 Elasticsearch 官方提供的 Java 低级 REST 客户端,它允许HTTP与Elasticsearch 集群通信,而无需处理 JSON 序列化/反序列化等底层细节。它是 Elasticsearch Java API 客户端的基础。 RestClient 主要特点 轻量级ÿ…...
应用升级/灾备测试时使用guarantee 闪回点迅速回退
1.场景 应用要升级,当升级失败时,数据库回退到升级前. 要测试系统,测试完成后,数据库要回退到测试前。 相对于RMAN恢复需要很长时间, 数据库闪回只需要几分钟。 2.技术实现 数据库设置 2个db_recovery参数 创建guarantee闪回点,不需要开启数据库闪回。…...
2025年能源电力系统与流体力学国际会议 (EPSFD 2025)
2025年能源电力系统与流体力学国际会议(EPSFD 2025)将于本年度在美丽的杭州盛大召开。作为全球能源、电力系统以及流体力学领域的顶级盛会,EPSFD 2025旨在为来自世界各地的科学家、工程师和研究人员提供一个展示最新研究成果、分享实践经验及…...
.Net框架,除了EF还有很多很多......
文章目录 1. 引言2. Dapper2.1 概述与设计原理2.2 核心功能与代码示例基本查询多映射查询存储过程调用 2.3 性能优化原理2.4 适用场景 3. NHibernate3.1 概述与架构设计3.2 映射配置示例Fluent映射XML映射 3.3 查询示例HQL查询Criteria APILINQ提供程序 3.4 高级特性3.5 适用场…...
vscode(仍待补充)
写于2025 6.9 主包将加入vscode这个更权威的圈子 vscode的基本使用 侧边栏 vscode还能连接ssh? debug时使用的launch文件 1.task.json {"tasks": [{"type": "cppbuild","label": "C/C: gcc.exe 生成活动文件"…...
leetcodeSQL解题:3564. 季节性销售分析
leetcodeSQL解题:3564. 季节性销售分析 题目: 表:sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...
学习STC51单片机32(芯片为STC89C52RCRC)OLED显示屏2
每日一言 今天的每一份坚持,都是在为未来积攒底气。 案例:OLED显示一个A 这边观察到一个点,怎么雪花了就是都是乱七八糟的占满了屏幕。。 解释 : 如果代码里信号切换太快(比如 SDA 刚变,SCL 立刻变&#…...
今日学习:Spring线程池|并发修改异常|链路丢失|登录续期|VIP过期策略|数值类缓存
文章目录 优雅版线程池ThreadPoolTaskExecutor和ThreadPoolTaskExecutor的装饰器并发修改异常并发修改异常简介实现机制设计原因及意义 使用线程池造成的链路丢失问题线程池导致的链路丢失问题发生原因 常见解决方法更好的解决方法设计精妙之处 登录续期登录续期常见实现方式特…...
零基础在实践中学习网络安全-皮卡丘靶场(第九期-Unsafe Fileupload模块)(yakit方式)
本期内容并不是很难,相信大家会学的很愉快,当然对于有后端基础的朋友来说,本期内容更加容易了解,当然没有基础的也别担心,本期内容会详细解释有关内容 本期用到的软件:yakit(因为经过之前好多期…...
GitFlow 工作模式(详解)
今天再学项目的过程中遇到使用gitflow模式管理代码,因此进行学习并且发布关于gitflow的一些思考 Git与GitFlow模式 我们在写代码的时候通常会进行网上保存,无论是github还是gittee,都是一种基于git去保存代码的形式,这样保存代码…...
