第一章-Nginx概述

学习视频1小时:https://www.bilibili.com/video/BV1W54y1z7GM?p=4

学习视频4小时:https://www.bilibili.com/video/BV1zJ411w7SV?from=search&seid=1241391484271225136

1什么是Nginx

Nginx 是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性: 在连接高并发的情况下,Nginx是Apache服务不错的替代品:。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

三个主要概念:反向代理,负载均衡,动静分离。

配置文件,主要由6个部分组成:

main:用于进行nginx全局信息的配置 events:用于nginx工作模式的配置 http:用于进行http协议信息的一些配置 server:用于进行服务器访问信息的配置 location:用于进行访问路由的配置 upstream:用于进行负载均衡的配置

解决问题:

问题1:客户端到底要将请求发送给哪台服务器。 问题2:如果所有客户端的请求都发送给了服务器1。 问题3:客户端发送的请求可能是申请动态资源的,也有申请静态资源。

image-20200712180545729

2linux安装Nginx

搜索:docker search nginx

下载:docker pull nginx

启动

-d: 后台运行 -p: 端口映射 冒号前是本机端口,冒号后是容器端口

==docker run -d -p 8787:80 –name nginx-8787 nginx== 启动容器 -name可选 nginx-8787是名称

若出现IPv4 forwarding is disabled. Networking will not work_ 按照以下步骤解决: 1、编辑 vi /etc/sysctl.conf 2、添加 net.ipv4.ip_forward=1 3、重启network服务 systemctl restart network

然后访问ip+8787出现Wecome to nginx就说明安装成功

3Nginx事例

在服务器中创建以下结构的目录

1
2
3
4
5
/home
    |---mutou
           |----nginx
                  |----conf.d
                  |----html

** 2. 在conf.d文件夹下新建default.conf文件,内容如下:**

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
server {
    listen       80; 
    server_name  localhost;
    # 原来的配置,匹配根路径
    #location / {
    #    root   /usr/share/nginx/html;
    #    index  index.html index.htm;
    #}
    # 更该配置,匹配/路径,修改index.html的名字,用于区分该配置文件替换了容器中的配置文件
    location / {
        root   /usr/share/nginx/html;
        index  index-test.html index.htm;
    }
}

3.在html中编写index-test.html,用以判断文件夹映射成功,内容如下:

1
2
3
4
5
<html>
  <body>
    <h2>it is html1</h2>
  </body>
</html>

接着开启一个nginx容器

1
docker run -d -p 8787:80 -v /home/mutou/nginx/conf.d:/etc/nginx/conf.d  -v /home/mutou/nginx/html:/usr/share/nginx/html nginx
1
2
3
4
5
6
#-v数据卷绑定将宿主机的配置文件与容器内配置文件进行绑定
docker run -d -p 8787:80 
--name xxxnginx-8787  
-v /home/mutou/nginx/conf.d:/etc/nginx/conf.d  
-v /home/mutou/nginx/html:/usr/share/nginx/html  
nginx

4Nginx配置文件

最新版的nginx中配置文件在conf.d下的default.conf中

配置文件有三部分组成,全局块,events块,http块

全局块

从配置文件开始到events块之间的内容,主要会设置一些影响nginx服务器整体运行的配置指令。 比如:woker_processes 2 worker角色的工作进程的个数,master进程是接收并分配请求给worker处理,为cpu核心数甚至其两倍。

events块

events块涉及的指令主要影响Nginx服务器和用户的网络连接, 如: worker_connections 2048 支持的最大连接数

http全局块

是配置中最频繁的部分,代理,缓存和日志等。 也可以包括http全局块,server块(虚拟主机配置)

http全局块

server块

每个server又可以分位全局server,location部分。一个server可配置多个location块

1
2
3
4
5
6
7
8
9
server {
    listen       80;   #表示监听的端口
    server_name  localhost; #本机名称
   location / {   
        root   /usr/share/nginx/html;
     		proxy_pass http://134.175.236.42:8000/store/commodity/showCommodity;
      #这是上面例中的,此时index不会生效,而是上面生效 index  index-test.html index.htm;
    }
}

常用命令

使用nginx命令前提是进入nginx目录

命令 功能
./nginx -t 检查配置文件是否正确
./nginx -s reload 重新加载nginx配置

第二章-主要技术

1反向代理

反向代理案例(一)

image-20200426171433040

修改C:\Windows\System32\drivers\etc下的host文件 加上134.175.236.42 www.store.com,然后访问www.store.com:8787就可以访问到134.175.236.42:8787了。

1
2
3
4
5
6
7
8
9
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
	proxy_pass http://134.175.236.42:8000/store/commodity/showCommodity;
        #index  index-test.html index.htm;
    }
}

反向代理案例(二)

1、实现效果 使用nginx反向代理,根据访问的路径跳转到不同端口的服务中 nginx监听端口为9001,

访问http://134.175.236.42/:8787/store直接跳转到xxx134.175.236.42/:8010/store 访问http://134.175.236.42/:8787/home直接跳转到xxx134.175.236.42/:8000/index

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
server {
    listen       80;   #表示监听的端口
    server_name  localhost; #本机名称
  location =/lsl {   
        root   /usr/share/nginx/html;
     	proxy_pass http://134.175.236.42:8000/store/commodity/showCommodity;
    }
     location /{   
        root   /usr/share/nginx/html;
     #	proxy_pass http://134.175.236.42:8000/store/commodity/showCommodity;
      #这是上面例中的,此时index不会生效,而是上面生效 
      index index-test.html index.htm;
    }
}
修饰符 功能(用在location = /lsl 中的等号位置)
= 精确匹配
~ 正则表达式模式匹配,区分大小写
~* 正则表达式模式匹配,不区分大小写
^~ 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式

去掉端口号

增加文件

1
2
3
4
5
6
/home
    |---mutou
           |----nginx
                  |----conf.d
                  |----html
                  |----conf.d2

我们在conf.d2中配置另一个nginx容器的配置文件,文件内容如下:

1
2
3
4
5
6
7
8
9
server {
    listen       80;
    server_name  localhost;
    location /mutou {
        # 在该位置配置反向代理,将ip/mutou请求拦截,发送给8080端口,如果不是本机请使用公网ip
        proxy_pass  http://134.175.236.42:8787/lsl;
    }
}

然后再启动一个nginx,用nginx服务器代理80端口

1
2
3
4
docker run -d -p 80:80 \
--name nginx-80 \
-v /home/mutou/nginx/conf.d2:/etc/nginx/conf.d  \
nginx

2负载均衡

3动静分离