Nginx小总结

下载

  1. 点击进入下载地址
  2. 一般下载稳定版本
    在这里插入图片描述

Linux 安装&启动

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

# 1. 安装环境
[root@localhost ~]# yum -y install gcc pcre-devel openssl-devel zlib-devel
...
keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-46.el7 libcom_err-devel.x86_64 0:1.42.9-17.el7 libselinux-devel.x86_64 0:2.5-15.el7 libsepol-devel.x86_64 0:2.5-10.el7
libverto-devel.x86_64 0:0.2.5-4.el7

Complete!

# 2. 创建目录,进入
[root@localhost ~]# mkdir -p /app/nginx/
[root@localhost ~]# cd /app/nginx/

# 3. 下载压缩包
[root@localhost nginx]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
--2020-10-19 20:53:02-- http://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘nginx-1.18.0.tar.gz’

100%[========================================================================================================================================================================>] 1,039,530 20.6KB/s in 60s

2020-10-19 20:54:03 (16.9 KB/s) - ‘nginx-1.18.0.tar.gz’ saved [1039530/1039530]

# 4. 解压,进入
[root@localhost nginx]# tar -zxf nginx-1.18.0.tar.gz
[root@localhost nginx]# cd nginx-1.18.0/

# 5. 配置【--prefix指定的是安装目录】【这一步是为了生成Makefile文件,才能进行安装nginx】
[root@localhost nginx-1.18.0]# ./configure --prefix=/app/nginx/nginx
...
nginx path prefix: "/app/nginx/nginx"
nginx binary file: "/app/nginx/nginx/sbin/nginx"
nginx modules path: "/app/nginx/nginx/modules"
nginx configuration prefix: "/app/nginx/nginx/conf"
nginx configuration file: "/app/nginx/nginx/conf/nginx.conf"
nginx pid file: "/app/nginx/nginx/logs/nginx.pid"
nginx error log file: "/app/nginx/nginx/logs/error.log"
nginx http access log file: "/app/nginx/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

# 6. 编译,并安装
[root@localhost nginx-1.18.0]# make && make install
...

# 7. 进入安装后的目录,conf/nginx.conf 配置文件,sbin/nginx 启动程序
[root@localhost nginx-1.18.0]# cd /app/nginx/nginx/
[root@localhost nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp

# 8. 建立软链接
ln -s /app/nginx/nginx/sbin/nginx /usr/bin

# 启动
nginx
# 停止
nginx -s stop
# 重启【一般用于修改配置文件后】
nginx -s reload

# 设置开机自启动【开机系统会执行rc.local文件中的命令】【这行代码相当于在rc.local文件中追加一行/app/nginx/nginx/sbin/nginx】
echo /app/nginx/nginx/sbin/nginx >> /etc/rc.local
# 要想上一步生效,还要赋执行权限,使脚本可以运行
chmod +x /etc/rc.local

# 显示版本【大写V显示比较全】
nginx -V
# 检查配置文件是否有错误
nginx -t

# -c指定配置文件nginx.conf
nginx -c /app/nginx/nginx/conf/nginx.conf
# -p指定目录,效果同上
nginx -p /app/nginx/nginx/ -c conf/nginx.conf
# 指定配置文件检查
nginx -c /app/nginx/nginx/conf/nginx.conf -t

配置文件

配置文件地址nginx安装目录/conf/nginx.conf

  • 基本模块
    1
    2
    3
    4
    5
    6
    #工作进程数  1 ,不要超过计算机的核数,四核配置4,八核配置8
    worker_processes 1;
    #工作连接数,也就是线程,一个进程有1024个线程,
    events {
    worker_connections 1024;
    }
  • http 基本配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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开启
    sendfile on;
    # 防止网络阻塞
    #tcp_nopush on;

    #keepalive_timeout 0;
    #keepalive_timeout超时时间
    keepalive_timeout 65;

    # 开启压缩输出
    #gzip on;
  • http server配置
    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
    server {
    listen 80;#监听端口
    server_name localhost;#监听ip,或域名

    #charset koi8-r;#设置字符集

    #access_log logs/host.access.log main;# 设置访问日志

    location / { # 监听访问路径 -> localhost:80/
    root html;# root是配置网站根目录位置,默认nginx安装目录的html目录
    index index.html index.htm;# 配置文件首页文件
    }

    #error_page 404 /404.html;# 状态码404 就会访问地址 localhost:80/404.html

    error_page 500 502 503 504 /50x.html;# 将服务器错误页面重定向到静态页面 /50x.html
    location = /50x.html {# 注意:这里 = 表示精确匹配,完全相同
    root html;
    }


    #PHP 脚本请求全部转发到Apache处理
    #location ~ \.php$ {
    # proxy_pass http://127.0.0.1;
    #}

    #PHP 脚本请求全部转发到FastCGI处理
    #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;
    #}


    #禁止访问 例如: .htaccess 文件
    #location ~ /\.ht {
    # deny all;
    #}

    #配置https服务,加密传输
    #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;
    # }
    #}
    }

正&反向代理

在这里插入图片描述

1
2
3
4
5
6
7
8
9
2021-08-03 08:56:24 补

# 正向代理:输入目标地址,通过正向代理,访问到目标地址。
你 -> vpn -> github.com

# 反向代理:输入代理地址,通过反向代理,访问到目标地址(代理服务器可以控制请求到不同的目标地址)。
你 -> nginx -> tomcat-1
-> tomcat-2
-> tomcat-3

静态部署

看着图来理解:ip+端口=root root就是访问地址的根路径
在这里插入图片描述

负载均衡

实战演示

  1. http模块上加:
    1
    2
    3
    4
    upstream www.taopanfeng.top{
    server 127.0.0.1:8081 weight=3;
    server 127.0.0.1:8082 weight=1;
    }

    说明:这里的www.taopanfeng.top只是一个名称,取名abc也可以,一般用网址来代表可读性。
    server对应一个节点。
    weight表示权重,访问比率就是权重之比。权重越大,访问次数越多。一般高性能服务器权重配置大一些。
    故障检测:如果上面两个节点有一个挂了,upstream会检测到,这时候就会只访问其中一个节点。

  2. server模块上添加
    1
    2
    3
    location /myweb {
    proxy_pass http://www.taopanfeng.top;
    }

    注意:这里的www.taopanfeng.top要与上面的upstream名称对应。

  3. 演示结果
    在这里插入图片描述

简化配置

如果只有一个节点,可以简写。

1
2
3
4
5
6
7
8
9
10
11
12
方式1
upstream www.taopanfeng.top{
server 127.0.0.1:8081;
}
location /myweb {
proxy_pass http://www.taopanfeng.top;
}

简化配置
location /myweb {
proxy_pass http://127.0.0.1:8081;
}

均衡策略

策略一共四种。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1. 轮询(默认)。可以理解为听歌的列表循环。
upstream www.taopanfeng.top{
server 127.0.0.1:8081;# 节点1
server 127.0.0.1:8082;# 节点2
}
2. 权重【不说了,在上面"实战演示"已经讲到】
3. ip_hash。也叫IP绑定,每个请求安装访问的IP进行hash值分配,这样每个访问的客户端就是固定访问一个后端服务器。这样可以解决Session会话丢失的问题。
如下举例:hash("127.0.0.1")%2 结果是0就访问节点1,结果是1访问节点2
upstream www.taopanfeng.top{
ip_hash;
server 127.0.0.1:8081;# 节点1
server 127.0.0.2:8082;# 节点2
}
4. 最少连接。请求会被转发到连接数最少的服务器上
upstream www.taopanfeng.top{
least_conn;
server 127.0.0.1:8081;# 节点1
server 127.0.0.2:8082;# 节点2
}

其他配置

1
2
3
4
5
6
7
8
9
10
11
12
1. backup。其他所有非backup的节点都挂了,才会请求backup节点。
upstream www.taopanfeng.top{
least_conn;
server 127.0.0.1:8081;# 节点1
server 127.0.0.2:8082 backup;# 节点2
}
2. down。表示当前节点已经挂了,不参与负载均衡。
upstream www.taopanfeng.top{
least_conn;
server 127.0.0.1:8081;# 节点1
server 127.0.0.2:8082 down;# 节点2
}

静态代理

概念

把所有的静态资源的访问改为访问Nginx,而不是访问Tomcat。【因为Nginx更擅长于静态资源的处理,性能更换,效率更高。】
所以在实际应用中,我们将静态资源比如图片,css,html,js等交给Nginx处理,而不是由Tomcat处理。
在这里插入图片描述

实现方式1

后缀名匹配。

1
2
3
4
5
6
7
8
9
10
11
12
	location ~.*\.(js|css|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
root /app/nginx/static;
}

说明一下:
~ 表示正则匹配,也就是说后面的内容可以是正则表达式匹配
第一个点.表示任意字符【文件名没有\n所以表示匹配任意字符】
*表示零个或多个字符
\转义字符,后面跟上.点,就匹配.这个符号。
|表示或
$表示结尾
整个匹配表示 以小括号里面的这些后缀结尾的文件都让Nginx处理。

注意:放静态资源的目录要有权限,权限不足报403。赋权例如chmod 777 /app/nginx/static
正则可以参考正则表达式
在这里插入图片描述

实现方式2

目录名匹配。

1
2
3
location ~.*/(css|js|img) {
root /app/nginx/static;
}

其他都不变,只需要把上面的location进行替换就行了。
~.*/(css|js|img)表示的含义是:匹配URL中含有/css /js /img的数据就去/app/nginx/static下面去找。

动静分离

概念

Nginx的负载均衡与静态代理结合在一起,我们可以实现动静分离,这是实际应用中常见的一种场景。
动态资源,如jsp由Tomcat或其他web服务器完成。
静态资源,如图片,css,js等由nginx服务器完成。
它们各司其职,从而达到合理的架构。
在这里插入图片描述

实现

1
2
3
1. 这里我们启动三个Nginx,三个Tomcat。【再次启动nginx可以使用指定配置文件的方式】
2. 三个nginx,其中一个监听80作为主,其他两个监听81,82作为静态资源访问。
3. 在主中需要配置动静分离的负载均衡设置。
1
2
3
4
5
6
cd /app/nginx/nginx/conf
cp nginx.conf nginx81.conf
cp nginx.conf nginx82.conf

nginx -c /app/nginx/nginx/conf/nginx81.conf
nginx -c /app/nginx/nginx/conf/nginx82.conf

在这里插入图片描述
在这里插入图片描述

虚拟主机

概念

虚拟主机,就是把一台服务器划分成多个”虚拟”的服务器,这样我们的一台服务器就可以当多台服务器使用,从而可以配置多个网站。
Nginx提供虚拟主机的地址,就是为了我们不需要安装多个Nginx,就可以运行多个网站。
Nginx的虚拟主机就是通过nginx.conf中的server节点指定的,一个Server标签就是一个虚拟主机,想要设置多个虚拟主机,配置多个server节点即可。
配置主机通常有下面两种方式

端口实现

基于端口的虚拟主机配置,使用端口来区分。
浏览器使用 同一个域名+端口 或 同一个ip地址+端口访问。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 81;
server_name www.taopanfeng.top;
location / {
proxy_pass http://127.0.0.1:8081;
}
}
server {
listen 82;
server_name www.taopanfeng.top;
location / {
proxy_pass http://127.0.0.1:8082;
}
}

说明一下:
如果你访问www.taopanfeng.top:81 就会代理到 http://127.0.0.1:8081
如果你访问www.taopanfeng.top:82 就会代理到 http://127.0.0.1:8082

域名实现

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
server {
listen 80;
server_name daniu.taopanfeng.com;
location ~.*/(css|js|img) {
root /app/nginx/static;
}
location / {
proxy_pass http://127.0.0.1:8081;
}
}
server {
listen 80;
server_name erdan.taopanfeng.com;
location ~.*/(css|js|img) {
root /app/nginx/static;
}
location / {
proxy_pass http://127.0.0.1:8082;
}
}
server {
listen 80;
server_name sanlv.taopanfeng.com;
location ~.*/(css|js|img) {
root /app/nginx/static;
}
location / {
proxy_pass http://127.0.0.1:8083;
}
}

需要配置本地的hosts文件,文件位置C:\Windows\System32\drivers\etc\hosts

1
2
3
4
5
# 添加映射
# 前面是Linux的IP,后面是自定义的虚拟域名
192.168.1.3 daniu.taopanfeng.com
192.168.1.3 erdan.taopanfeng.com
192.168.1.3 sanlv.taopanfeng.com

案例

在这里插入图片描述
在这里插入图片描述

访问流程

我这上面标注了七步。这里仅是我自己理解的逻辑,并不是执行顺序。
在这里插入图片描述

简化逻辑

通过include的方式引入虚拟主机的配置。不要引入错了,这行代码要放在http模块中。
include /app/nginx/nginx/conf/myserver.conf;
文件是隔开的,配置更清晰,主文件没有那么多的server
在这里插入图片描述

完结

2020-03-14 02:26:16
终于总结完了,看着都爽。
以后有新的知识的话,还会往里面加。
对于nginx配合模块使用,图片上传,代理头设置…还不是很了解。这里都是一些简单的可以入门的。