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


一、Nginx


1.1、Nginx 下载

a)打开官方网站连接:OpenResty - 下载

选择根据电脑型号自行选择,这里演示 64位 Windows 版,进行下载 zip 文件.

Ps:OpenResty 就是 Nginx.

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

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

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

1.2、nginx 基础配置的认识

在 openresty-1.21.4.2-win64\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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/104045.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java正则表达式 及应用场景爬虫,捕获分组非捕获分组

正则表达式 通常用于校验 比如说qq号 看输入的是否符合规则就可以用这个 public class regex {public static void main(String[] args) {//正则表达式判断qq号是否正确//规则 6位及20位以内 0不能再开头 必须全是数子String qq"1234567890";System.out.println(qq…

【机器学习】sklearn特征选择(feature selection)

文章目录 特征工程过滤法(Filter)方差过滤相关性过滤卡方过滤F验表互信息法小结 嵌入法(Embedded)包装法(Wrapper) 特征工程 特征提取(feature extraction)特征创造(feature creation)特征选择(feature se…

【软件设计师-下午题总结】

目录 下午题之总结于学习记录:题一、数据流图:1、熟悉相关的图形2、实体名称3、数据存储4、补充缺失的数据流和起点终点5、用结构化语言描述6、描述,找加工逻辑的时候7、如何保持数据流平衡 题二:实体联系图:1、常用图…

Django Test

Django--Laboratory drug management and early warning system-CSDN博客 创建项目doinglms django-admin startproject doinglms python manage.py runserver 运行开发服务器(Development Server) 创建一个自定义 App,名称为 lms: python manage.py startapp lms

minio桶命名规则

一、背景 今天做项目需要上传图片到minio,上传失败,查看错误是桶未创建成功。 minio桶的创建具有自己的命名规则,不符合则无法创建。 二、命名规则 1、存储桶名称的长度必须介于 3(最小)到 63(最大&…

【数据结构】二叉树--堆排序

目录 一 降序(建小堆) 二 升序 (建大堆) ​三 优化(以升序为例) 四 TOP-K问题 一 降序(建小堆) void Swap(int* x, int* y) {int tmp *x;*x *y;*y tmp; }//降序 建小堆 void AdjustUp(int* a, int child) {int parent (child - 1) / 2;while (child > 0){if (a[chil…

Ubuntu 22.04.3 LTS单机私有化部署sealos

推荐使用奇数台 Master 节点和若干 Node 节点操作系统 :Ubuntu 22.04 LTS内核版本 :5.4 及以上配置推荐 :CPU 4 核 , 内存 8GB, 存储空间 100GB 以上最小配置 :CPU 2 核 , 内存 4GB, 存储空间 60GB 这里采用的Ubuntu 22.04.3 LTS 版本,Ubuntu 20.04.4 LTS这个版本…

Eclipse插件安装版本不兼容问题解决方案——Papyrus插件为例

项目场景: Eclipse Papyrus安装后,没有新建Papyrus工程选项,也没有新建Papyrus Model的选项。 打开Papyrus Model会报错 问题描述 同样的,安装其他插件也是。可能某个插件之前安装是好用的,结果Eclipse的版本更新了,就再也安装不好用了 原因分析: 根本原因是因为包之…

Xcode 15下,包含个推的项目运行时崩溃的处理办法

升级到Xcode15后,部分包含个推的项目在iOS17以下的系统版本运行时,会出现崩溃,由于崩溃在个推Framework内部,无法定位到具体代码,经过和个推官方沟通,确认问题是项目支持的最低版本问题。 需要将项目的最低…

十七、【渐变工具组】

文章目录 渐变工具油漆桶工具 渐变工具 渐变样式有5种,分别是线性渐变,径向渐变,角度渐变,对称渐变,菱形渐变 另外渐变工具的颜色可以进行编辑,需要先打开渐变编辑工具: 如何使用渐变编辑工…

与HTTP相关的各种概念

网络世界 网络世界中最重要的一个名词就是互联网(Internet),它以TCP/IP协议族为基础,构建成了一望无际的信息传输网络。而我们通常所说的“上网”,主要就是访问互联网的一个子集——万维网(World Wide Web&#xff09…

如何使用CSS和JavaScript实施暗模式?

近年来,暗模式作为用户界面选项备受追捧。它提供了更暗的背景和更亮的文本,不仅可以减轻眼睛疲劳,还可以节省电池续航时间,尤其是在OLED屏幕上。 不妨了解如何结合使用CSS和JavaScript为网站和Web应用程序添加暗模式选项。 了解暗…

JWT的原理及实际应用

前言: 定义:JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案 JWT官网 由于HTTP协议是无状态的,这意味着如果我们想判定一个接口是否被认证后访问,就需要借助cookie或者session会话机制进行判定&…

保姆级手把手记录Android studio BottomNavigationView +FrameLayout暴力切换Fragment

开发环境: 效果图: 《《《代码在底部》》》 1,新建项目 2,新建若干Fragment,内容一样,改一下显示出来的Text,名字分别为test1Fragment,test2Fragment,test3Fragment,默认TextView的Text属性分别…

交通 | python网络爬虫:“多线程并行 + 多线程异步协程

推文作者:Amiee 编者按: 常规爬虫都是爬完一个网页接着爬下一个网页,不适应数据量大的网页,本文介绍了多线程处理同时爬取多个网页的内容,提升爬虫效率。 1.引言​ 一般而言,常规爬虫都是爬完一个网页接着…

代码随想录算法训练营第五十二天 | 123.买卖股票的最佳时机III、188.买卖股票的最佳时机IV

123.买卖股票的最佳时机III 视频讲解:动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III_哔哩哔哩_bilibili 代码随想录 (1)代码 188.买卖股票的最佳时机IV 视频讲解&a…

文字雨特效

效果展示 CSS 知识点 简易实现云朵技巧text-shadow 属性的灵活运用filter 属性实现元素自动变色 实现页面布局 <div class"container"><div class"cloud"><h2>Data Clouds Rain</h2></div> </div>实现云朵 实现云…

从基础到卷积神经网络(第12天)

1. PyTorch 神经网络基础 1.1 模型构造 1. 块和层 首先&#xff0c;回顾一下多层感知机 import torch from torch import nn from torch.nn import functional as Fnet nn.Sequential(nn.Linear(20, 256), nn.ReLU(), nn.Linear(256, 10))X torch.rand(2, 20) # 生成随机…

<图像处理> Fast角点检测

Fast角点检测 基本原理是使用圆周长为N个像素的圆来判定其圆心像素P是否为角点&#xff0c;如下图所示为圆周长为16个像素的圆&#xff08;半径为3&#xff09;&#xff1b;OpenCV还提供圆周长为12和8个像素的圆来检测角点。 相对中心像素的位置信息 //圆周长为16 static c…

Lumen/Laravel - 事件机制原理与工作流程 - 探究

1.应用场景 主要用于学习与探究Lumen/Laravel的事件机制原理与工作流程。 2.学习/操作 1.文档阅读 chatgpt & 其他资料 2.整理输出 2.1 是什么 TBD 2.2 为什么需要「应用场景」 TBD 2.3 什么时候出现「历史发展」 TBD 2.4 怎么实践 TBD 截图 后续补充 ... 3.问题…