目录

[TOC]

Nginx的安装

Nginx的版本区别

nginx开源版:http://nginx.org/

nginx-plus商业版:https://www.nginx.com/

Openresty

Tengine:http://tengine.taobao.org/

安装/启动/停止

Windows停止taskkill /f /t /im nginx.exe

安装参考:https://www.cnblogs.com/makecode/articles/9996639.html

  • 将tar.gz的安装文件放入opt目录中

  • 执行解压命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 第一步:安装nginx所需的依赖(如果有依赖,则直接跳过此目录)
    yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

    # 下载nginx到opt目录下面
    wget https://nginx.org/download/nginx-1.22.1.tar.gz

    # 第三步:解压nginx压缩包,进入nginx目录,执行`configure`编译nginx看是否还有错误
    #(可以使用【--prefix=`指定目录`】命令来指定安装目录)
    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module

    # 第四步:继续执行下列命令
    make
    make install
  • 运行nginx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 第一步:进入nginx的安装目录下的sbin目录进行启动
    cd /usr/local/nginx/sbin

    # 第三步:启动nginx
    ./nginx

    # 第四步:浏览器访问
    http://ip:80/

    # 第五步:如果访问不到,关闭防火墙
    systemctl stop firewalld.service # 关闭防火墙
    systemctl disable firewalld.service # 禁止开机启动防火墙
    firewall-cmd --reload # 重启防火墙
  • 启动 / 重启 nginx

    1
    2
    3
    4
    5
    6
    7
    8
    # 第一步:进入nginx的sbin目录,【一定要是上面安装时指定的目录】
    cd /usr/local/nginx/sbin

    # 第二步:启动命令
    ./nginx # 启动
    ./nginx -s stop # 快速停止
    ./nginx -s quit # 优雅关闭,在退出前完成已经接受的连接请求
    ./nginx -s reload # 重新加载配置
  • 创建服务脚本,设置nginx的开机自启动(将nginx安装成系统服务)

    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
    # 创建服务脚本
    vim /usr/lib/systemd/system/nginx.service

    # 粘贴如下命令
    [Unit]
    Description=nginx - web server
    After=network.target remote-fs.target nss-lookup.target

    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    ExecQuit=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true

    [Install]
    WantedBy=multi-user.target


    # 上面开机启动设置后,如果启动失败,替换为下面的
    [Unit]
    Description=nginx
    After=network.target

    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target

    # 如果nginx启动了,先关闭nginx在使用systemctl启动nginx
    ps -ef | grep nginx

    # nginx如果启动,再执行下列命令
    ./nginx -s stop # 进入nginx安装目录的sbin目录下面

    # 重新加载系统服务
    systemctl daemon-reload

    # 启动nginx和设置开机启动
    systemctl start nginx.service
    systemctl enable nginx.service

Nginx的目录结构

conf

用来存放配置文件相关

html

用来存放静态文件的默认目录 html、css等

sbin

nginx的主程序

基本运行原理

image-20220614101102837

Nginx配置文件与应用场景

部分配置文件

worker_processes

worker_processes默认为1,表示开启一个业务进程

worker_connections

worker_connections 1024单个业务进程可接受连接数

include mime.types;

·include mime.types;引入http mime类型

后面可以使用include引入自定义配置,上面的mime.type文件在conf目录下面,用于标注数据是什么类型的。

例如:html会被标注为text/html,CSS会被标注为text/css,具体可以去看文件内容,也可自定义添加配置

default_type application/octet-stream;

default_type application/octet-stream;如果mime类型没匹配上,默认使用二进制流的方式传输。

sendfile on;

sendfile on;使用linux的sendfile(socket, file, len)高效网络传输,也就是数据0拷贝。建议开启

  • 未开启sendfile

    image-20220615094621013

  • 开启sendfile

    image-20220615094722800

keepalive_timeout 65;

反向代理再看

server

虚拟主机配置

1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
}

image-20220615100915368

虚拟主机和域名解析

原本一台服务器只能对应一个站点,通过虚拟主机技术可以虚拟化成多个站点同时对外提供服务

server_name匹配规则

可以在同一个server_name中匹配多个域名

1
server_name www.xiaofei.com static.xiaofei.com;

通配符匹配

1
server_name *.xiaofei.com;

通配符结束匹配

1
server_name vod.*;

正则匹配

1
server_name ~^[0-9]+\.mmban\.com$;

server_name根据端口配置虚拟主机(没有域名)

同一ip不同端口,访问不同资源

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
worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
# 此处监测127.0.0.1:80
listen 80;
server_name localhost;

location / {
root /xiaofei/nginx-study/www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
# 此处监测127.0.0.1:81
listen 81;
server_name localhost;

location / {
root /xiaofei/nginx-study/www/static;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

server_name根据域名配置虚拟主机(有域名)

相同ip,相同端口,不同域名,访问不同资源

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
worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
# 此处监测www.xiaofei.com:80
listen 80;
server_name www.xiaofei.com;
# server_name www.xiaofei.com blog.www.xiaofei.com # 匹配多个域名
# server_name *.xiaofei.com; # 通配符匹配
# server_name vod.*; # 通配符结束匹配
# server_name ~^[0-9]+\.mmban\.com$; # 正则匹配

location / {
root /xiaofei/nginx-study/www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
# 此处监测static.xiaofei.com:80
listen 80;
server_name static.xiaofei.com;

location / {
root /xiaofei/nginx-study/www/static;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

域名解析相关企业项目实战技术架构

  • 域名解析与泛域名解析实战
  • 多用户二级域名
  • 短网址
  • Http Dns

需要了解,自行百度

反向代理

网关、代理与反向代理

反向代理在系统/架构中的应用场景

Nginx的反向代理配置

基于反向代理的负载均衡器

负载均衡策略

image-20220615113050840

基本使用

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
worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
# nginx所在服务器的ip
server_name 192.168.0.10;

location / {
# 需要代理到哪个服务器
proxy_pass https://www.naste.top/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

负载均衡

注意:负载均衡需要和反向代理一起使用

负载均衡基本用法

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
worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

# upstream后面的名字自定义,随便填什么都可以
upstream httpds {
server 192.168.44.102:80;
server 192.168.43.103:80;
}

server {
listen 80;
# nginx所在服务器的ip
server_name 192.168.0.10;

location / {
# 代理到上面upstream指定的服务器
proxy_pass http://httpds/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

负载均衡策略

轮询

默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。

weight(权重)

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

  • down:表示当前的server暂时不参与负载
  • weight:默认为1.weight越大,负载的权重就越大。
  • backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。标注为备用机,其他机器宕机才启动
1
2
3
4
5
upstream httpd { 
server 127.0.0.1:8050 weight=10 down;
server 127.0.0.1:8060 weight=1;
server 127.0.0.1:8060 weight=1 backup;
}

ip_hash

根据客户端的ip地址转发同一台服务器,可以保持会话。

least_conn

最少连接访问

url_hash

根据用户访问的url定向转发请求

fair

根据后端服务器响应时间转发请求

动静分离

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
worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
# nginx所在服务器的ip
server_name 127.0.0.1;

# 配置方向代理
location / {
proxy_pass http://127.0.0.1:8080;
}
# 未使用正则表达式的方式配置动静分离
location /css{
root /usr/local/nginx/static;
index index.html index.htm;
}
# 使用正则表达式配置动静分离
location ~*/(img|js) {
root /usr/local/nginx/static;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

location前缀

/通用匹配,任何请求都会匹配到

=精准匹配,不是以指定模式开头

~正则表达式匹配,区分大小写

~*正则表达式匹配,不区分大小写

^~非正则表达式匹配,匹配以指定模式开头的location

location匹配顺序

  • 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配
  • 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)
  • 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配
  • 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)

alias与root

root用来设置根目录,而alias在接受请求的时候在路径上不会加上location。

1
2
3
4
location /css { 
alias /usr/local/nginx/static/css;
index index.html index.htm;
}
  • alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
  • root指定 的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
  • 使用 alias标签的目录块中不能使用rewrite的break(具体原因不明);另外,alias指定的目录后面必须要加上”/“符 号!!
  • alias虚拟目录配置中,location匹配的path目录如果后面不带”/“,那么访问的url地址中这个path目录后 面加不加”/“不影响访问,访问时它会自动加上”/“; 但是如果location匹配的path目录后面加上”/“,那么访问的url地 址中这个path目录必须要加上”/“,访问时它不会自动加上”/“。如果不加上”/“,访问就会失败!
  • root目录配置中,location匹配的path目录后面带不带”/“,都不会影响访问。

Nginx配置Https

https://developer.aliyun.com/article/766958