第一章 Nginx介绍

Nginx是什么

Nginx 是一个开源且高性能、可靠的 Http Web 服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定

我们为什么选择 Nginx 服务

Nginx 非常轻量
功能模块少 (源代码仅保留 http 与核心模块代码,其余不够核心代码会作为插件来安装)
代码模块化 (易读,便于二次开发,对于开发人员非常友好)
互联网公司都选择 Nginx
1.Nginx 技术成熟,具备的功能是企业最常使用而且最需要的
2.适合当前主流架构趋势, 微服务、云架构、中间层
3.统一技术栈, 降低维护成本, 降低技术更新成本。

Nginx重要特性

Nginx 采用 Epool 网络模型, Apache 采用 Select 模型
Select: 当用户发起一次请求, select 模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起请求, epool 模型会直接进行处理,效率高效,并无连接限制

Nginx应用场景

第二章 Nginx安装部署

Nginx分为几种
1.源码编译(1.版本随意 2.安装复杂 3.升级繁琐)
2.epel仓库(1.版本较低 2.安装简单 3.配置不易读)
3.官方仓库(1.版本较新 2.安装简单 3.配置易读,推荐)
下面分别介绍编译安装和yum安装方法

1.编译安装方法

创建www用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -s /sbin/nologin -M -u 666 -g 666
[root@web01 ~]# id www
uid=666(www) gid=666(www) 组=666(www)

安装依赖包

yum install openssl-devel pcre-devel -y

下载解压软件包

mkdir /data/soft -p
cd /data/soft/
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar zxvf nginx-1.16.0.tar.gz 

配置编译参数

[root@web01 ~]# cd /data/soft/nginx-1.16.0/
[root@web01 /data/soft/nginx-1.16.0]# ./configure --help
[root@web01 /data/soft/nginx-1.16.0]# ./configure --user=www --group=www --prefix=/opt/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module --with-pcre

编译安装

[root@web01 /data/soft/nginx-1.16.0]# make && make install

创建软链接

[root@web01 /data/soft/nginx-1.16.0]# ln -s /opt/nginx-1.16.0/ /opt/nginx
[root@web01 /data/soft/nginx-1.16.0]# ls -lh /opt/
总用量 4.0K
lrwxrwxrwx  1 root root   18 7月  29 20:27 nginx -> /opt/nginx-1.16.0/
drwxr-xr-x 11 1001 1001 4.0K 7月  29 20:26 nginx-1.16.0

检查语法

[root@web01 /opt/nginx]# /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful

启动nginx

[root@web01 /opt/nginx]# /opt/nginx/sbin/nginx

检查测试

[root@web01 /opt/nginx]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12828/nginx: master
[root@web01 /opt/nginx]# curl 10.0.1.7

比喻

下载                              买菜
./configure                       切菜.洗菜,做什么菜
make                              炒菜,抄完之后,菜还在锅里
make install                      把菜端出来
/opt/nginx/sbin/nginx -t          尝一尝味道
/opt/nginx/sbin/nginx             吃菜
/opt/nginx/sbin/nginx -s reload   重新加载/加菜
/opt/nginx/sbin/nginx -s stop     收拾餐盘

2.YUM安装方法

安装依赖包

[root@web01 ~]# yum install openssl-devel pcre-devel -y

配置官方yum源

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

安装nginx服务

[root@web01 ~]# yum install nginx -y

启动服务并配置开机自启动

[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

测试访问

[root@web01 ~]# curl 10.0.1.7

Nginx启动方式说明

编译安装启动管理方式

nginx -t
nginx
nginx -s reload
nginx -s stop

yum安装启动管理方法

nginx -t
systemctl start nginx
systemctl reload nginx
systemctl restart nginx
systemctl stop  nginx

第三章 Nginx重要配置文件说明

查看配置文件

[root@web01 ~]# rpm -ql nginx
...................................................
/etc/logrotate.d/nginx                     #nginx日志切割的配置文件
/etc/nginx/nginx.conf                      #nginx主配置文件
/etc/nginx/conf.d                          #子配置文件
/etc/nginx/conf.d/default.conf             #默认展示的页面一样
/etc/nginx/mime.types                      #媒体类型 (http协议中的文件类型)
/etc/sysconfig/nginx                       #systemctl 管理 nginx的使用的文件
/usr/lib/systemd/system/nginx.service      #systemctl 管理nginx(开 关 重启 reload)配置文件
/usr/sbin/nginx                            #nginx命令
/usr/share/nginx/html                      #站点目录 网站的根目录
/var/log/nginx                             #nginx日志 access.log 访问日志
...................................................                     

查看已经编译的模块

[root@web01 ~]# nginx -V

配置文件注解

Nginx 主配置文件/etc/nginx/nginx.conf 是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。
Nginx 主配置文件整体分为三块进行学习,分别是
CoreModule(核心模块)
EventModule(事件驱动模块)
HttpCoreModule(http 内核模块)

第一部分:配置文件主区域配置

user  nginx;                    #定义运行nginx进程的用户
worker_processes  1;            #Nginx运行的work进程数量(建议与CPU数量一致或 auto)error_log  /var/log/nginx/error.log warn;             #nginx错误日志
pid        /var/run/nginx.pid;                        #nginx运行pid

第二部分:配置文件事件区域

events {worker_connections  1024;  #每个 worker 进程支持的最大连接数
}

第三部分:配置http区域

http {include       /etc/nginx/mime.types;          #Nginx支持的媒体类型库文件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  /var/log/nginx/access.log  main;    #访问日志保存路径sendfile        on;                             #开启高效传输模式#tcp_nopush     on;                       keepalive_timeout  65;                          #连接超时时间#gzip  on;                                      #开启压缩include /etc/nginx/conf.d/*.conf;               #包含子配置文件
}    

第四部分:子配置文件内容

[root@web01 ~]# egrep -v "#|^$" /etc/nginx/conf.d/default.conf
server {listen       80;             #指定监听端口server_name  localhost;      #指定监听的域名location / {              root   /usr/share/nginx/html;     #定义站点的目录index  index.html index.htm;      #定义首页文件}error_page   500 502 503 504  /50x.html;    #优雅显示页面信息location = /50x.html {root   /usr/share/nginx/html;}
}

http server location 扩展了解项
http{}层下允许有多个 Server{}层,一个 Server{}层下又允许有多个 Location
http{} 标签主要用来解决用户的请求与响应。
server{} 标签主要用来响应具体的某一个网站。
location{} 标签主要用于匹配网站具体 URL 路径

第四章 Nginx虚拟主机配置实战

基于域名的虚拟主机

[root@web01 ~]# cat /etc/nginx/nginx.conf    user  nginx;
worker_processes  1;error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/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  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;#include /etc/nginx/conf.d/*.conf;server   {listen       80;server_name  www.oldboy.com;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;}}server   {listen       80;server_name  blog.oldboy.com;location / {root   /usr/share/nginx/html/blog;index  index.html index.htm;}}
}

基于端口的虚拟主机

端口号优先级比域名要高

[root@web01 ~]# cat /etc/nginx/nginx.conf    user  nginx;
worker_processes  1;error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/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  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;#include /etc/nginx/conf.d/*.conf;server   {listen       81;server_name  www.oldboy.com;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;}}server   {listen       82;server_name  blog.oldboy.com;location / {root   /usr/share/nginx/html/blog;index  index.html index.htm;}}
}

基于IP的虚拟主机

添加第二IP

ip addr add 10.0.0.11/24 dev eth0

配置文件

[root@web01 ~]# cat /etc/nginx/nginx.conf    user  nginx;
worker_processes  1;error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/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  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;#include /etc/nginx/conf.d/*.conf;server   {listen       10.0.1.7:81;server_name  www.oldboy.com;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;}}server   {listen       10.0.1.11:82;server_name  blog.oldboy.com;location / {root   /usr/share/nginx/html/blog;index  index.html index.htm;}}
}

第五章 Nginx虚拟主机配置优化

所有配置都写入一个配置文件维护起来比较麻烦,如果修改错了,影响所有的页面,所以我们应该拆分nginx的配置文件为各个子配置

Nginx主配置文件

[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/nginx.conf user  nginx;
worker_processes  1;error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;events {worker_connections  1024;
}http {include       /etc/nginx/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  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;include /etc/nginx/conf.d/*.conf;
}

子配置文件www

[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/01-www.conf
server   {listen       80;server_name  www.oldboy.com;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;}
}

子配置文件blog

[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/02-blog.conf
server   {listen       80;server_name  blog.oldboy.com;location / {root   /usr/share/nginx/html/blog;index  index.html index.htm;}
}

创建代码目录及首页

[root@web01 /etc/nginx/conf.d]# mkdir /usr/share/nginx/html/{www,blog}
[root@web01 /etc/nginx/conf.d]# echo "www" > /usr/share/nginx/html/www/index.html
[root@web01 /etc/nginx/conf.d]# echo "blog" > /usr/share/nginx/html/blog/index.html 

检查语法重启服务

[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx

访问测试

[root@web01 ~]# tail -1 /etc/hosts
10.0.1.7 www.oldboy.com blog.oldboy.com
[root@web01 ~]# curl www.oldboy.com
www
[root@web01 ~]# curl blog.oldboy.com
blog

第六章 Nginx状态模块

nginx状态模块: --with-http_stub_status_module

状态模块配置文件

[root@web01 /etc/nginx/conf.d]# cat status.conf
server {listen 80;server_name  status.oldboy.com;stub_status on;access_log off;
}
[root@web01 /etc/nginx/conf.d]# tail -1 /etc/hosts
10.0.1.7 www.oldboy.com blog.oldboy.com status.oldboy.com
[root@web01 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]# systemctl restart nginx
[root@web01 /etc/nginx/conf.d]# curl status.oldboy.com
Active connections: 1
server accepts handled requests1 1 1
Reading: 0 Writing: 1 Waiting: 0 

第七章 Nginx日志

日志字段解释

$remote_addr # 记录客户端 IP 地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录 ISO8601 标准格式下的本地时间
$request # 记录请求的方法以及请求的 http 协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数
$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端 IP 地址
$request_length # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
# 注:如果 Nginx 位于负载均衡器, nginx 反向代理之后, web 服务器无法直接获取到客 户端真实的 IP 地址。
# $remote_addr 获取的是反向代理的 IP 地址。 反向代理服务器在转发请求的 http 头信息中,
# 增加 X-Forwarded-For 信息,用来记录客户端 IP 地址和客户端请求的服务器地址。

第八章 nginx连环坑

1.如果是yum安装,启动关闭命令推荐使用systemctl。不要混着nginx -s这样用。

2.相同域名相同端口会报冲突,比如都是0.0.0.0:80

3.没有首页会报403而不是404

4.端口优先级高于域名

5.ip+端口的优先级是最高的

6.所有域名都匹配不上的时候,默认转发到根据ASCII码排序优先的配置文件

7.可以添加参数指定默认匹配的页面,这样就无需修改文件名了

server {listen       80 default_server;server_name  www.mysun.com;location / {root   /code/www;index  index.html index.htm;}
}

第九章 日志切割

[root@web01 /var/log/nginx]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {dailymissingokrotate 52compressdelaycompressdateextnotifemptycreate 640 nginx admsharedscriptspostrotateif [ -f /var/run/nginx.pid ]; thenkill -USR1 `cat /var/run/nginx.pid`fiendscript
}

第十章 Nginx常用模块

目录索引

1.1 应用场景

可以使用nginx作为简易的文件下载服务器

1.2 参数说明

Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location# autoindex 常用参数
autoindex_exact_size off;
默认为 on, 显示出文件的确切大小,单位是 bytes。
修改为 off,显示出文件的大概大小,单位是 kB 或者 MB 或者 GB。autoindex_localtime on;
默认为 off,显示的文件时间为 GMT 时间。
修改为 on, 显示的文件时间为文件的服务器时间。charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码

1.3 配置文件

[root@web01 /usr/share/nginx/html]# cat /etc/nginx/conf.d/download.conf
server {listen 80;server_name download.oldzhang.com;location / {root /usr/share/nginx/html/download;charset utf-8,gbk;autoindex on;autoindex_localtime on;autoindex_exact_size off;}
}[root@web01 /usr/share/nginx/html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /usr/share/nginx/html]# systemctl restart nginx

1.4 创建测试数据

touch /usr/share/nginx/html/download/{1..10}.txt

1.5 访问页面

第十一章 状态监控

2.1 状态字段解释

Active connections # 当前活动的连接数
accepts # 当前的总连接数 TCP
handled # 成功的连接数 TCP
requests # 总的 http 请求数
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了 keepalive
# 注意, 一次 TCP 的连接,可以发起多次 http 的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s 没有活动则断开连接

2.2 配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/status.conf
server {listen 80;server_name  status.oldboy.com;stub_status on;access_log off;
}

2.3 访问测试

[root@web01 ~]# curl status.oldboy.com
Active connections: 1
server accepts handled requests4 4 6
Reading: 0 Writing: 1 Waiting: 0 

第十二章 访问控制

3.1 基于IP的访问控制

3.1.1 配置语法

#允许配置语法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except#拒绝配置语法
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except

3.1.2 配置案例1:拒绝windwos访问www域名

[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf
server   {listen       80;server_name  www.oldboy.com;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;deny 10.0.1.1;allow all;}
}

3.1.3 windows访问测试403

3.1.4 使用curl访问测试ok

[root@web01 ~]# curl www.oldzhang.com
www

3.1.5 配置案例2:只允许windows访问,其他全部拒绝

[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf
server   {listen       80;server_name  www.oldboy.com;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;allow 10.0.1.1;deny all;}
}

3.1.6 windows访问测试ok

3.1.7 curl访问测试403 

[root@web01 ~]# curl www.oldzhang.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.0</center>
</body>
</html>

3.2 基于用户认证的访问控制

3.2.1 配置语法

#访问提示字符串
Syntax: auth_basic string| off;
Default: auth_basic off;
Context: http, server, location, limit_except#账户密码文件
Syntax: auth_basic_user_file file;
Default: -
Context: http, server, location, limit_except

3.2.2 配置文件

#1.需要安装 httpd-tools,该包中携带了 htpasswd 命令
[root@web01 ~]# yum install httpd-tools -y
#2.创建新的密码文件, -c 创建新文件 -b 允许命令行输入密码
[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf oldzhang oldzhang
Adding password for user oldzhang
#3.nginx 配置调用
[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf
server   {listen       80;server_name  www.oldboy.com;location / {auth_basic "Auth access Blog Input your Passwd!";auth_basic_user_file auth_conf;root   /usr/share/nginx/html/www;index  index.html index.htm;}
}

3.2.3 访问测试

第四章 访问限制

经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个 ip 的连接数,请求数、进行限制。
ngx_http_limit_conn_module 模块可以根据定义的 key 来限制每个键值的连接数,如同一个 IP 来源的连接数。
limit_conn_module 连接频率限制
limit_req_module 请求频率限制

4.1请求限制

4.1.1 配置语法

#模块名 ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location

 4.1.2 配置文件

http {limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
}
[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf
server   {listen       80;server_name  www.oldzhang.com;limit_req zone=req_zone burst=3 nodelay;access_log  /var/log/nginx/www.access.log  main;location / {root   /usr/share/nginx/html/www;index  index.html index.htm;}
}

4.1.3 访问测试

[root@web01 ~]# yum install httpd-tools -y
[root@web01 ~]# ab -n 20 -c 2 http://www.oldzhang.com/

4.1.4 查看访问日志

[root@web01 ~]# tail -f /var/log/nginx/www.access.log
10.0.1.7 - - [30/Jul/2019:19:34:48 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [30/Jul/2019:19:34:48 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [30/Jul/2019:19:34:48 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [30/Jul/2019:19:34:48 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [30/Jul/2019:19:34:48 +0800] "GET / HTTP/1.0" 503 197 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [30/Jul/2019:19:34:48 +0800] "GET / HTTP/1.0" 503 197 "-" "ApacheBench/2.3" "-"

4.1.5 查看错误日志

[root@web01 ~]# tail -f /var/log/nginx/error.log
2019/07/30 19:34:48 [error] 17380#17380: *32 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.1.7, server: www.oldzhang.com, request: "GET / HTTP/1.0", host: "www.oldzhang.com"
2019/07/30 19:34:48 [error] 17380#17380: *33 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.1.7, server: www.oldzhang.com, request: "GET / HTTP/1.0", host: "www.oldzhang.com"
2019/07/30 19:34:48 [error] 17380#17380: *34 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.1.7, server: www.oldzhang.com, request: "GET / HTTP/1.0", host: "www.oldzhang.com"
2019/07/30 19:34:48 [error] 17380#17380: *35 limiting requests, excess: 3.997 by zone "req_zone", client: 10.0.1.7, server: www.oldzhang.com, request: "GET / HTTP/1.0", host: "www.oldzhang.com"

4.3 为什么限制请求的效果更好

我们先来回顾一下 http 协议的连接与请求,首先 HTTP 是建立在 TCP 基础之上, 在完成 HTTP 请求需要先建立TCP 三次握手(称为 TCP 连接) ,在连接的基础上在完成 HTTP 的请求。
所以多个 HTTP 请求可以建立在一次 TCP 连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效,因为同一时刻只允许一个 TCP 连接进入, 但是同一时刻多个 HTTP 请求可以通过一个 TCP 连接进入。所以针对 HTTP 的请求限制才是比较优的解决方案。

第五章 location

使用 Nginx Location 可以控制访问网站的路径, 但一个 server 可以有多个 location 配置, 多个 location 的优先级该如何区分

5.1 location语法介绍

location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}

5.2 location语法优先级

5.3 配置location匹配规则实战 

[root@web01 ~]# cat /etc/nginx/conf.d/01-www.conf
server {listen       80;server_name  www.oldzhang.com;root   /usr/share/nginx/html/www;location / {return 200  "location / \n";}location = / {return 200 "location = \n";}location /documents/ {return 200 "location /documents/ \n";}location ^~ /images/ {return 200 "location ^~ /images/ \n";}location ~* \.(gif|jpg|jpeg)$ {return 200 "location ~* \.(gif|jpg|jpeg) \n";}access_log off;
}

5.4 测试location匹配规则

#精确匹配=/
[root@web01 ~]# curl www.oldzhang.com
location =
#没有满足的请求,所以匹配了/
[root@web01 ~]# curl www.oldzhang.com/oldzhang.html
location /
#匹配了/documents
[root@web01 ~]# curl www.oldzhang.com/documents/oldboy.html
location /documents/
#没有满足的条件,匹配/
[root@web01 ~]# curl www.oldzhang.com/oldboy/documents/oldboy.html
location /
#正则匹配了文件名
[root@web01 ~]# curl www.oldzhang.com/oldboy.jpg
location ~* \.(gif|jpg|jpeg)
#~*匹配正则不区分大小写优先于/documents
[root@web01 ~]# curl www.oldzhang.com/documents/oldboy.jpg
location ~* \.(gif|jpg|jpeg)
#^~优先匹配于~*
[root@web01 ~]# curl www.oldzhang.com/images/oldboy.jpg
location ^~ /images/ 

Nginx服务详细篇从基础到反向代理和负载均衡相关推荐

  1. Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例

    场景 Nginx入门简介和反向代理.负载均衡.动静分离理解 https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102790862 Ub ...

  2. Nginx安装配置和常用使用(反向代理与负载均衡)

    文章目录 1.Ubuntu服务器中的nginx 2.Mac中使用nginx 3.配置不同的域名访问项目不需要输入端口方式 4.ssl配置 5.nginx 禁止 ip 允许和阻止访问 6.自定义403等 ...

  3. 使用Nginx实现服务器反向代理和负载均衡

    前言 同事总问我Nginx做反向代理负载均衡的问题,因此特意留下一篇扫盲贴! 直接部署服务器的风险 假设,我开发了一个网站,然后买了一台Web服务器和一台数据库服务器,直接部署到公共网络上.如下图,网 ...

  4. nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全

    文章目录 前言 一.nginx简介 1. 什么是 nginx 和可以做什么事情 2.Nginx 作为 web 服务器 3. 正向代理 4. 反向代理 5. 负载均衡 6.动静分离 二.Nginx 的安 ...

  5. nginx一篇入门:安装、静态网站部署、反向代理、负载均衡

    前言: 本文章的nginx和tomcat是在Linux中,使用docker来安装和讲解 本人刚学完nginx,如有不对地方,欢迎指正 目录 ⼀.Nginx的安装与启动 1.什么是Nginx Nginx ...

  6. [转]nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件

    文章目录 前言 一.nginx简介 1. 什么是 nginx 和可以做什么事情 2.Nginx 作为 web 服务器 3. 正向代理 4. 反向代理 5. 负载均衡 6.动静分离 二.Nginx 的安 ...

  7. Nginx总结(反向代理、负载均衡、动静分离)篇

    一.Nginx简介 什么是Nginx Nginx ("engine x")是一个高性能的HTTP和反向代理服务器,特点是占有内存少,并发能力强,事实上ngimx,的并发能力确实在同 ...

  8. nginx做为反向代理实现负载均衡的例子 .

    我们介绍了nginx这个轻量级的高性能server主要可以干的两件事情: >直接作为http server(代替apache,对PHP需要FastCGI处理器支持,这个我们之后介绍): > ...

  9. 从0开始,在Linux中配置Nginx反向代理、负载均衡、session共享、动静分离

    写这篇文章花费了我近一周的时间,参考网上许多优秀的博客文章,我不敢说写的很好,至少很全很详细.本文先介绍原理部分,然后再进行实战操作,我认为这样才会有更深的理解,不过这也导致了文章篇幅很长.但是,如果 ...

  10. 介绍下Nginx 反向代理与负载均衡

    Nginx的代理功能与负载均衡功能是最常被用到的,这篇就开门见山,先描述一些关于代理功能的配置,再说明负载均衡详细. Nginx 代理服务的配置说明 1.设置 404 页面导向地址 error_pag ...

最新文章

  1. STM32 USB转串口驱动安装不成功出现黄色感叹号解决方法!
  2. 皮一皮:杯子不要买太奇怪的...
  3. Python学习day5作业
  4. 获取inout框中未改变值的数据_数据分析10——数据分析的基本过程(02)
  5. element ui分页怎么做_Vue Element分页器
  6. 面向对象设计模式纵横谈:Prototype 原型模式(笔记记录)
  7. 利用ClustrMaps | GoStats | 51la | Google Analytics统计和分析访问量
  8. 新发布GoldenGate 12c版本中的主要特性
  9. c语言电子时钟课程设计报告,模拟电子时钟c语言课程设计设计报告.doc
  10. 如何软件项目电子投标
  11. SSM框架常用jar包下载
  12. 极大似然法python例子
  13. 服务器XP系统打印机共享设置,WinXP系统网络打印机怎么设置?WinXP打印机共享设置方法...
  14. ADNI数据集相关知识整理
  15. 计算机音频视频格式名,MP3/MP4播放器固件知识常见问题解决
  16. [渝粤教育] 西北农林科技大学 国际贸易实务 参考 资料
  17. [弱校联萌2016]2016弱校联盟十一专场10.3
  18. kotlin “${ }”是什么意思?
  19. 【samba】Ubuntu samba的安装及使用方法
  20. 使用rufus 制作 windows / CentOS /ubuntu 系统盘 [ U 盘 ]

热门文章

  1. 是非人生 — 一个菜鸟程序员的5年职场路 第24节
  2. 一文搞懂MySQL索引(清晰明了)
  3. 强化学习 原理与Python实现(四)
  4. 2019/04/15 在调试https://blog.csdn.net/hjw2719/article/details/83473199中程序时出现的问题
  5. Xcode6 打包报错 ITMS-90096
  6. iOS常用的功能(打电话、发短信、发邮件等)
  7. 嵌入式软件未来发展趋势
  8. 【渝粤教育】国家开放大学2018年春季 7389-21T劳动与社会保障法 参考试题
  9. 公众号采集,公众号批量自动采集,微信公众号免费文章采集
  10. 学习 React.js 需要了解的一些概念