一文讲明白Nginx

Nginx笔记

一、Nginx 简介

1、什么是NGINX
	Nginx ("engine x")是一个高性能的HTTP和反向代理服务器,特点是占有内存少,并发能
力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好
    Nginx专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率,能经受高负载
的考验,有报告表明能支持高达50000个并发连接数。
2、反向代理

a. 正向代理
在客户端(浏览器)配置代理服务器,通过代理服务器进行互联网访问

{% asset_img image-20200606144302429.png  正向代理 %}

b. 反向代理
反向代理,其实客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只
需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返
回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器
地址,隐藏了真实服务器IP地址。

反向代理

3、负载均衡

单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上,将原先
请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服
务器,也就是我们所说的负载均衡

负载均衡

4、动静分离

为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速
度。降低原来单个服务器的压力。

动静分离

二、Nginx 安装

下面的操作是以Centos7为例

1. 使用远程连接工具连接Centos7操作系统
2. 安装nginx相关依赖

gcc
pcre
openssl
zlib

① 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

$ yum install gcc-c++

② PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

$ yum install -y pcre pcre-devel

③ zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

$ yum install -y zlib zlib-devel

④ OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

$ yum install -y openssl openssl-devel

3. 安装Nginx

① 下载nginx,两种方式

a. 直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html

b. 使用wget命令下载(推荐)。确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装。

$ wget -c https://nginx.org/download/nginx-1.19.0.tar.gz

② 依然是直接命令:

$ tar -zxvf nginx-1.19.0.tar.gz
$ cd nginx-1.19.0

③ 配置:

其实在 nginx-1.12.0 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
1.使用默认配置

$ ./configure

2.自定义配置(不推荐)

$ ./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

④ 编辑安装

$ make && make install

查看版本号(使用nginx操作命令前提条件:必须进入nginx的目录/usr/local/nginx/sbin.)

$ ./nginx -v

查找安装路径:

$ whereis nginx

⑤ 启动,停止nginx

$ cd /usr/local/nginx/sbin/
$ ./nginx 
$ ./nginx -s stop
$ ./nginx -s quit
$ ./nginx -s reload

查询nginx进程:

$ ps aux|grep nginx

启动成功后,在浏览器可以看到这样的页面:

![安装成功} 可能遇到的问题: 远程访问服务器被拒绝 可能是80端口被防火墙保护起来了,不让外界访问 查看开放的端口

1

           
            
             
              
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                b 
               
              
                a 
               
              
                s 
               
              
                h 
               
              
                > 
               
              
                f 
               
              
                i 
               
              
                r 
               
              
                e 
               
              
                w 
               
              
                a 
               
              
                l 
               
              
                l 
               
              
                − 
               
              
                c 
               
              
                m 
               
              
                d 
               
              
                − 
               
              
                − 
               
              
                l 
               
              
                i 
               
              
                s 
               
              
                t 
               
              
                − 
               
              
                a 
               
              
                l 
               
              
                l 
               
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                b 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                p 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                t 
               
              
                d 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                t 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                t 
               
              
                a 
               
              
                b 
               
              
                l 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                f 
               
              
                i 
               
              
                g 
               
              
                u 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                打 
               
              
                开 
               
              
                固 
               
              
                定 
               
              
                端 
               
              
                口 
               
              
                使 
               
              
                用 
               
              
                如 
               
              
                下 
               
              
                命 
               
              
                令 
               
              
                打 
               
              
                开 
               
              
                80 
               
              
                端 
               
              
                口 
               
              
                ( 
               
              
                其 
               
              
                他 
               
              
                端 
               
              
                口 
               
              
                类 
               
              
                似 
               
              
                ) 
               
              
                < 
               
              
                f 
               
              
                i 
               
              
                g 
               
              
                u 
               
              
                r 
               
              
                e 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                h 
               
              
                i 
               
              
                g 
               
              
                h 
               
              
                l 
               
              
                i 
               
              
                g 
               
              
                h 
               
              
                t 
               
              
                s 
               
              
                h 
               
              
                e 
               
              
                l 
               
              
                l 
               
              
                > 
               
              
                < 
               
              
                t 
               
              
                a 
               
              
                b 
               
              
                l 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                t 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                t 
               
              
                d 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                g 
               
              
                u 
               
              
                t 
               
              
                t 
               
              
                e 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                p 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                l 
               
              
                i 
               
              
                n 
               
              
                e 
               
              
                > 
               
              
                1 
               
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                b 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                l 
               
              
                i 
               
              
                n 
               
              
                e 
               
              
                > 
               
              
                2 
               
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                b 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                p 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                t 
               
              
                d 
               
              
                > 
               
              
                < 
               
              
                t 
               
              
                d 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                c 
               
              
                o 
               
              
                d 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                p 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                l 
               
              
                i 
               
              
                n 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                m 
               
              
                e 
               
              
                t 
               
              
                a 
               
              
                > 
               
              
             
               </span><span class=bash> firewall-cmd --list-all</span></span><br></pre></td></tr></table></figure> 打开固定端口 使用如下命令打开80端口(其他端口类似) <figure class=highlight shell><table><tr><td class=gutter><pre><span class=line>1</span><br><span class=line>2</span><br></pre></td><td class=code><pre><span class=line><span class=meta> 
              
             
           </span><spanclass=bash>firewallcmdlistall</span></span><br></pre></td></tr></table></figure>使80<figureclass=highlightshell><table><tr><tdclass=gutter><pre><spanclass=line>1</span><br><spanclass=line>2</span><br></pre></td><tdclass=code><pre><spanclass=line><spanclass=meta> firewall-cmd --add-service=http --permanent
< / s p a n > < s p a n c l a s s = b a s h > f i r e w a l l − c m d − − p e r m a n e n t − − a d d − p o r t = 80 / t c p < / s p a n > < / s p a n > < b r > < / p r e > < / t d > < / t r > < / t a b l e > < / f i g u r e > 注 意 这 里 需 要 声 明 t c p 的 方 式 , 之 后 我 们 需 要 重 新 加 载 f i r e w a l l 的 配 置 < f i g u r e c l a s s = h i g h l i g h t s h e l l > < t a b l e > < t r > < t d c l a s s = g u t t e r > < p r e > < s p a n c l a s s = l i n e > 1 < / s p a n > < b r > < / p r e > < / t d > < t d c l a s s = c o d e > < p r e > < s p a n c l a s s = l i n e > < s p a n c l a s s = m e t a > </span><span class=bash> firewall-cmd --permanent --add-port=80/tcp</span></span><br></pre></td></tr></table></figure> 注意这里需要声明tcp的方式,之后我们需要重新加载firewall的配置 <figure class=highlight shell><table><tr><td class=gutter><pre><span class=line>1</span><br></pre></td><td class=code><pre><span class=line><span class=meta> </span><spanclass=bash>firewallcmdpermanentaddport=80/tcp</span></span><br></pre></td></tr></table></figure>tcpfirewall<figureclass=highlightshell><table><tr><tdclass=gutter><pre><spanclass=line>1</span><br></pre></td><tdclass=code><pre><spanclass=line><spanclass=meta> firewall-cmd --reload
⑥ 开机自启动 即在 rc.local增加启动代码就可以了。
1

           
            
             
              
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                b 
               
              
                a 
               
              
                s 
               
              
                h 
               
              
                > 
               
              
                v 
               
              
                i 
               
              
                / 
               
              
                e 
               
              
                t 
               
              
                c 
               
              
                / 
               
              
                r 
               
              
                c 
               
              
                . 
               
              
                l 
               
              
                o 
               
              
                c 
               
              
                a 
               
              
                l 
               
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                b 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                p 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                t 
               
              
                d 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                t 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                t 
               
              
                a 
               
              
                b 
               
              
                l 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                f 
               
              
                i 
               
              
                g 
               
              
                u 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                增 
               
              
                加 
               
              
                一 
               
              
                行 
               
              
                ‘ 
               
              
                / 
               
              
                u 
               
              
                s 
               
              
                r 
               
              
                / 
               
              
                l 
               
              
                o 
               
              
                c 
               
              
                a 
               
              
                l 
               
              
                / 
               
              
                n 
               
              
                g 
               
              
                i 
               
              
                n 
               
              
                x 
               
              
                / 
               
              
                s 
               
              
                b 
               
              
                i 
               
              
                n 
               
              
                / 
               
              
                n 
               
              
                g 
               
              
                i 
               
              
                n 
               
              
                x 
               
              
                ‘ 
               
              
                设 
               
              
                置 
               
              
                执 
               
              
                行 
               
              
                权 
               
              
                限 
               
              
                : 
               
              
                < 
               
              
                f 
               
              
                i 
               
              
                g 
               
              
                u 
               
              
                r 
               
              
                e 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                h 
               
              
                i 
               
              
                g 
               
              
                h 
               
              
                l 
               
              
                i 
               
              
                g 
               
              
                h 
               
              
                t 
               
              
                s 
               
              
                h 
               
              
                e 
               
              
                l 
               
              
                l 
               
              
                > 
               
              
                < 
               
              
                t 
               
              
                a 
               
              
                b 
               
              
                l 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                t 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                t 
               
              
                d 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                g 
               
              
                u 
               
              
                t 
               
              
                t 
               
              
                e 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                p 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                l 
               
              
                i 
               
              
                n 
               
              
                e 
               
              
                > 
               
              
                1 
               
              
                < 
               
              
                / 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                > 
               
              
                < 
               
              
                b 
               
              
                r 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                p 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                / 
               
              
                t 
               
              
                d 
               
              
                > 
               
              
                < 
               
              
                t 
               
              
                d 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                c 
               
              
                o 
               
              
                d 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                p 
               
              
                r 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                l 
               
              
                i 
               
              
                n 
               
              
                e 
               
              
                > 
               
              
                < 
               
              
                s 
               
              
                p 
               
              
                a 
               
              
                n 
               
              
                c 
               
              
                l 
               
              
                a 
               
              
                s 
               
              
                s 
               
              
                = 
               
              
                m 
               
              
                e 
               
              
                t 
               
              
                a 
               
              
                > 
               
              
             
               </span><span class=bash> vi /etc/rc.local</span></span><br></pre></td></tr></table></figure> 增加一行 `/usr/local/nginx/sbin/nginx` 设置执行权限: <figure class=highlight shell><table><tr><td class=gutter><pre><span class=line>1</span><br></pre></td><td class=code><pre><span class=line><span class=meta> 
              
             
           </span><spanclass=bash>vi/etc/rc.local</span></span><br></pre></td></tr></table></figure>/usr/local/nginx/sbin/nginx<figureclass=highlightshell><table><tr><tdclass=gutter><pre><spanclass=line>1</span><br></pre></td><tdclass=code><pre><spanclass=line><spanclass=meta> chmod 755 rc.local
#### Nginx配置文件结构 nginx配置文件地址 > /usr/local/nginx/conf/nginx.conf 下面是默认config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#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 - r e m o t e u s e r [ remote_user [ remoteuser[time_local] “KaTeX parse error: Expected 'EOF', got '#' at position 40: …an class=line> #̲ 'status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer” ’
# ‘“ h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""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 /scriptsKaTeX parse error: Expected 'EOF', got '#' at position 50: …an class=line> #̲ include fastcg…remote_addr– r e m o t e u s e r [ remote_user [ remoteuser[time_local] $request $status $body_bytes_sent $http_referer $http_user_agent KaTeX parse error: Double subscript at position 7: http_x_̲forwarded_for';… { #请求的url过滤,正则匹配,为区分大小写,*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}
上面是nginx的基本配置,需要注意的有以下几点: - 第一点 - r e m o t e a d d r 与 remote_addr 与 remoteaddrhttp_x_forwarded_for 用以记录客户端的ip地址; - $remote_user :用来记录客户端用户名称; - $time_local : 用来记录访问时间与时区; - $request : 用来记录请求的url与http协议; - $status : 用来记录请求状态;成功是200, - $body_bytes_s ent :记录发送给客户端文件主体内容大小; - $http_referer :用来记录从那个页面链接访问过来的; - KaTeX parse error: Expected 'EOF', got '#' at position 164: …1208860.html** #̲## 三、配置实例1 反向代理… ./usr/src/apache-tomcat-8.5.55/bin/start.sh
# 查看tomcat 日志
KaTeX parse error: Expected 'EOF', got '#' at position 134: …pan class=meta>#̲</span><span cl… firewall-cmd --permanent --add-port=8080/tcp
$ firewall-cmd --reload
准备工作结束 {% asset_img image-20200607094155776.png 访问过程](https://gitee.com/fu_heng/picture/raw/master/img/image-20200606173451847.png)

实现工作

修改nginx配置文件, nginx.conf

改动部分

例2 反向代理

1、实现效果

使用nginx反向代理,根据访问的路径跳转到不同端口的服务中。
nginx.监听端口为9001,。
访问http://127.0.0.1:9001/edu/ 直接跳转到127.0.0.1:8081
访问http://127.0.0.1:9001/vod/ 直接跳转到127.0.0.1:8082

2、准备工作

(1) 准备两个tomcat服务器,一个8080端口,一个8081端口

(2) 创建文件夹和测试页面。

3、具体nginx配置

$ vi /usr/local/nginx/conf/nginx.conf

nginx配置

(1) 找到nginx配置文件,进行反向代理配置。

(2) 开放对外访问的端口号9001

(3) 重启nginx服务器,使配置文件生效

4、最终测试

edu请求示例

5、补充location部分

location指令说明。
该指令用于匹配URL。。
语法如下:

location [ = | ~ | ~* | ^~] uri {

}
1=: 用于不含正则表达式的uri前,要求请求字符串与uri严格匹配,如果匹配成功,
	就停止继续向下搜索并立即处理该请求
2~: 用于表示uri包含正则表达式,并且区分大小写
3~*: 用于表示uri包含正则表达式,并且不区分大小写
4^~: 用于不含正则表达式的uri前,要求Nginx服务器找到标识uri和请求字
	符串匹配度最高的location后,立即使用此location处理请求,而不再使用location
	块中的正则uri和请求字符串做匹配
注意: 如果uri包含正则表达式,则必须要有~或者~*标识。

四、配置实例2 负载均衡

1、实现效果

(1) 浏览器地址栏输入地址http://192.168.xxx.xxx/edu/index.html, 负载均衡效果,平均到8080
和8081端口中,

2、准备工作

(1) 准备两台tomcat服务器,一 台8080, 一台8081

(2) 在两台tomcat里面webapps目录中,创建名称是edu文件夹,在edu文件夹中创建
页面index.html,用于测试。

3、在nginx的配置文件中进行负载均衡的配置

负载均衡配置

4、效果

负载均衡 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SNpkljXv-1625040368957)(D:\360downloads\nut\我的坚果云\image-20200607124843736.png)]

负载分配策略

在linux下有Nginx、LVS、 Haproxy 等等服务可以提供负载均衡服务,而且Nginx提供了几种分配方式(策略):。

  • 1、轮询(默认)

    每个请求按时间顺序逐一分配到不 同的后端服务器,如果后端服务器down掉,能自动剔除

  • 2、weight
    weight代表权重默认为1,权重越高被分配的客户端越多。
    指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。例如: 。

  • 3、ip hash

    每个请求按访问ip的hash结果分配, 这样每个访客固定访问一个后端服务器,可以解诀session的问题。例如:

    upstream server pool{
      ip_ hash
      server 192.168.5.21:80
      server 192.168.5.22:80
    }
    
  • 4、fair (第三方)
    按后端服务器的响应时间来分配请求,响应时间短的优先分配

    upstream server_pool 
    	server 192.168.5.21:80;
    	server 192.168.5.22:80;
    	fair;
    }
    

五、配置实例3 动静分离

动静分离

通过location指定不同的后缀名实现不同的请求转发。通过expires参数设置,可以使浏
览器缓存过期时间,减少与服务器之前的请求和流量。具体Expires定义: 是给一个资源
设定一个过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,
所以不会产生额外的流量。此种方法非常适合不经常变动的资源。(如果经常更新的文件,
不建议使用Expires来缓存),如果设置3d, 表示在这3天之内访问这个URL, 发送一
个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码304,
如果有修改,则直接从服务器重新下载,返回状态码200。。

2、准备工作

(1) 在liunx系统中准备静态资源,用于进行访问

/data/image 图片文件夹

/data/www html文件夹

3、具体配置

(1) 在nginx配置文件中进行配置

动静分离

4、实际测试

http://192.168.1.112/www/index.html
http://192.168.1.112/image/1.jpg

img

上图是因为autoindex on这个设置生效的

六、Nginx配置高可用集群

1、什么是nginx高可用

img

(1) 需要两台nginx服务器。
(2) 需要keepalived
(3) 需要虚拟ip

2、配置高可用的准备工作

(1) 需要两台服务器192.168.17.129 和192.168.17.1314
(2) 在两台服务器安装nginx.
(3) 在两合服务器安装keepalived.

3、在两台服务器安装keepalived
使用yum命令进行安装

$ yum install keepalived
$ rpm -q -a keepalived    #查看是否已经安装上

默认安装路径: /etc/keepalived

安装之后,在etc里面生成目录keepalived, 有配置文件keepalived.conf

4、完成高可用配置(主从配置)

(1)修改keepalived的配置文件keepalived.conf为:

global_defs {
	notification_email {
	  acassen@firewall.loc
	  failover@firewall.loc
	  sysadmin@firewall.loc
	}
	notification_email_from Alexandre.Cassen@firewall.loc
	smtp_ server 192.168.17.129
	smtp_connect_timeout 30
	router_id LVS_DEVEL	# LVS_DEVEL这字段在/etc/hosts文件中看;通过它访问到主机
}

vrrp_script chk_http_ port {
	script "/usr/local/src/nginx_check.sh"
	interval 2   # (检测脚本执行的间隔)2s
	weight 2  #权重,如果这个脚本检测为真,服务器权重+2
}

vrrp_instance VI_1 {
	state BACKUP   # 备份服务器上将MASTER 改为BACKUP
	interface ens33 //网卡名称
	virtual_router_id 51 # 主、备机的virtual_router_id必须相同
	priority 100   #主、备机取不同的优先级,主机值较大,备份机值较小
	advert_int 1	#每隔1s发送一次心跳
	authentication {	# 校验方式, 类型是密码,密码1111
        auth type PASS
        auth pass 1111
    }
	virtual_ipaddress { # 虛拟ip
		192.168.17.50 // VRRP H虛拟ip地址
	}
}

(2)在路径/usr/local/src/ 下新建检测脚本 nginx_check.sh

nginx_check.sh

#! /bin/bash
A=`ps -C nginx -no-header | wc - 1`
if [ $A -eq 0];then
	/usr/local/nginx/sbin/nginx
	sleep 2
	if [`ps -C nginx --no-header| wc -1` -eq 0 ];then
		killall keepalived
	fi
fi

(3) 把两台服务器上nginx和keepalived启动

$ systemctl start keepalived.service		#keepalived启动
$ ps -ef I grep keepalived		#查看keepalived是否启动

5、最终测试

(1) 在浏览器地址栏输入虚拟ip地址192.168.17.50

(2) 把主服务器(192.168.17.129) nginx和keealived停止,再输入192.168.17.50.

$ systemctl stop keepalived.service  #keepalived停止

七、Nginx原理解析

1、master和worker

img

2、worker如何进行工作的

img

3、一个master和多个woker的好处
(1) 可以使用nginx -s reload热部署。

首先,对于每个worker进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销,
同时在编程以及问题查找时,也会方便很多。其次,采用独立的进程,可以让互相之间不会
影响,一个进程退出后,其它进程还在工作,服务不会中断,master进程则很快启动新的
worker进程。当然,worker进程的异常退出,肯定是程序有bug了,异常退出,会导致当
前worker.上的所有请求失败,不过不会影响到所有请求,所以降低了风险。

4、设置多少个woker合适

Nginx同redis类似都采用了io多路复用机制,每个worker都是一个独立的进程, 但每个进
程里只有一个主线程,通过异步非阻塞的方式来处理请求,即使是 千上万个请求也不在话
下。每个worker的线程可以把一个cpu的性能发挥到极致。所以worker数和服务器的cpu
数相等是最为适宜的。设少了会浪费cpu,设多了会造成cpu频繁切换上下文带来的损耗。
# 设置worker数量
worker.processes 4 

# work绑定cpu(4work绑定4cpu)
worker_cpu_affinity 0001 0010 0100 1000

# work绑定cpu (4work绑定8cpu中的4)
worker_cpu_affinity 0000001 00000010 00000100 00001000

5、连接数worker_ connection

这个值是表示每个worker进程所能建立连接的最大值,所以,一个nginx 能建立的最大连接数,应该是worker.connections * worker processes。当然,这里说的是最大连接数,对于HTTP 请求本地资源来说,能够支持的最大并发数量是worker.connections * worker processes,如果是支持http1.1的浏览器每次访问要占两个连接,所以普通的静态访问最大并发数是: worker.connections * worker.processes / 2, 而如果是HTTP作为反向代理来说,最大并发数量应该是worker.connections * worker_proceses/4. 因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接.

第一个: 发送请求,占用了woker的几个连接数?
答案: 2或者4个。

第二个: nginx有一个master,有四个woker,每个woker支持最大的连接数1024,支持的最大并发数是多少?
答案:普通的静态访问最大并发数是: worker connections * worker processes /2,
rker processes。当然,这里说的是最大连接数,对于HTTP 请求本地资源来说,能够支持的最大并发数量是worker.connections * worker processes,如果是支持http1.1的浏览器每次访问要占两个连接,所以普通的静态访问最大并发数是: worker.connections * worker.processes / 2, 而如果是HTTP作为反向代理来说,最大并发数量应该是worker.connections * worker_proceses/4. 因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接.


> 第一个: 发送请求,占用了woker的几个连接数?
> 答案: 2或者4个。
>
> 第二个: nginx有一个master,有四个woker,每个woker支持最大的连接数1024,支持的最大并发数是多少?
> 答案:普通的静态访问最大并发数是: worker connections * worker processes /2,
> 而如果是HTTP作为反向代理来说,最大并发数量应该是worker connections * worker processes/4
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值