nginx架构分析

1.nginx模块化

Nginx涉及到的模块分为核心模块、标准HTTP模块、可选HTTP模块、邮件服务模块以及第三方模块等五大类。

https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/module.md

[root@centos-03 objs]# ls ngx_modules.c
ngx_modules.c
[root@centos-03 objs]#

2.模块目录

[root@centos-03 objs]# cd src
[root@centos-03 src]# ls
core event http mail misc os stream
[root@centos-03 src]#

3.nginx WEB请求机制

https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/IO.md

线程是进程的一个子单元,线程比进程好的地方是可以节省更多的资源,假如我开一个进程耗费的资源是20兆,那我开10个线程也是占用20兆,这样我同样的内存可以开更多的线程出来,每个线程也是处理一个请求,线程也有弊端,需要和其他的线程共享内存,稳定性不是很好 

同步:同步、异步发生在当客户端发起请求后,服务端处理客户端的请求时。 同步机制,是指客户端发送请求后,需要等待服务端(内核)返回信息后,再继续发送下一个请求。 在同步机制中,所有的请求在服务器端得到同步,即发送方和接收方对请求的处理步调是一致的。

异步:异步机制,是指客户端发出一个请求后,不等待服务端(内核)返回信息,就继续发送下一个请求。 在异步机制中,所有来自发送方的请求形成一个队列,接收方处理完后再通知发送方。

阻塞:阻塞与非阻塞发生在IO调度中,比如内核到磁盘IO。 阻塞方式下,进程/线程在获取最终结果之前,被系统挂起了,也就是所谓的阻塞了,在阻塞过程中该进程什么都干不了, 直到最终结果反馈给它时,它才恢复运行状态。

非阻塞:非阻塞方式和阻塞相反,进程/线程在获取最终结果之前,并没有进入被挂起的状态,而是该进程可以继续执行新的任务。 当有最终结果反馈给该进程时,它再把结果交给客户端。

4.nginx事件驱动模型

https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/event.md

事件驱动模型是实现异步非阻塞的一个手段。事件驱动模型中,一个进程(线程)就可以了。 对于web服务器来说,客户端A的请求连接到服务端时,服务端的某个进程(Nginx worker process)会处理该请求, 此进程在没有返回给客户端A结果时,它又去处理了客户端B的请求。 服务端把客户端A以及客户端B发来的请求作为事件交给了“事件收集器”, 而“事件收集器”再把收集到的事件交由“事件发送器”发送给“事件处理器”进行处理。 最后“事件处理器”处理完该事件后,通知服务端进程,服务端进程再把结果返回给客户端A、客户端B。 在这个过程中,服务端进程做的事情属于用户级别的,而事件处理这部分工作属于内核级别的。 也就是说这个事件驱动模型是需要操作系统内核来作为支撑的。

select模型、poll模型、epoll模型

5.Nginx架构

Nginx服务器使用 master/worker 多进程模式。 主进程(Master process)启动后,会接收和处理外部信号; 主进程启动后通过fork() 函数产生一个或多个子进程(work process),每个子进程会进行进程初始化、 模块调用以及对事件的接收和处理等工作。

nginx虚拟主机配置

1.nginx配置文件包括三部分:events上面的是全局部分、events、http(http里面有server,每一个server就是一个虚拟主机,一个http里面有多个server,可以跑多个站点,一般我们都是在http最下面加一条include vhost/*.conf,把所有的虚拟主机配置写到这个目录里面(http里面默认的server也注释掉))

[root@centos-03 conf]# ls /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
[root@centos-03 conf]#

2.创建vhost目录,然后在vhost目录下创建.conf文件,我们这里所有的站点文件都放到data/wwwroot/目录下

[root@centos-03 conf]# mkdir vhost
[root@centos-03 conf]#
[root@centos-03 conf]# mkdir -p /data/wwwroot/
[root@centos-03 conf]#
[root@centos-03 conf]# mkdir /data/wwwroot/www.1.com
[root@centos-03 conf]#
[root@centos-03 vhost]# cd vhost/
[root@centos-03 vhost]# vim 1.conf^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
root /data/wwwroot/www.1.com;
}
[root@centos-03 vhost]#
[root@centos-03 www.1.com]# cd /data/wwwroot/www.1.com/
[root@centos-03 www.1.com]# vim index.html^C
[root@centos-03 www.1.com]# cat index.html
www.1.com
[root@centos-03 www.1.com]#

3.检查下配置文件是否有错

[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]#

4.重新加载配置文件

[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]#

5.测试(相当于我们的www.1.com解析到了127.0.0.1上面了)

[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com
www.1.com
[root@centos-03 conf]#

6.这里我们用任何域名访问nginx都指向了www.1.com,这是因为nginx有一个默认虚拟主机的说法(访问没有配置的域名会指向其中一个虚拟主机),我们需要配置一个默认虚拟主机做一个限制,拒绝访问。

[root@centos-03 conf]# curl -x127.0.0.1:80 www.a.com
www.1.com
[root@centos-03 conf]# curl -x127.0.0.1:80 www.b.com
www.1.com
[root@centos-03 conf]#
[root@centos-03 vhost]# cd vhost/^C
[root@centos-03 vhost]# cp 1.conf default.conf^C
[root@centos-03 vhost]# vim default.conf ^C
[root@centos-03 vhost]# cat default.conf
server {
listen 80 default_server;
deny all;
}
[root@centos-03 vhost]#
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.a.com
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.b.com
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com
www.1.com
[root@centos-03 vhost]#

7.泛解析server配置所有的xxx.1.com类型访问都会到1.com/index.html上

server {
listen 80;
server_name *.1.com;
root /data/wwwroot/1.com;
}

8.基于端口的虚拟主机

[root@centos-03 vhost]# cp 1.conf 2.conf
[root@centos-03 vhost]# vim 2.conf ^C
[root@centos-03 vhost]# cat 2.conf
server {
listen 8080;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com_8080;
}
[root@centos-03 vhost]#
[root@centos-03 vhost]# mkdir /data/wwwroot/www.1.com_8080
[root@centos-03 vhost]# vim /data/wwwroot/www.1.com_8080/index.html^C
[root@centos-03 vhost]# cat !$
cat /data/wwwroot/www.1.com_8080/index.html
8080
[root@centos-03 vhost]#
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]#
[root@centos-03 vhost]# curl -x127.0.0.1:8080 www.1.com
8080
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com
www.1.com
[root@centos-03 vhost]#

nginx的rewrite配置-if

https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md

1.if指令条件判断语句由Nginx内置变量、逻辑判断符号和目标字符串三部分组成。 其中,内置变量是Nginx固定的非自定义的变量,如,$request_method, $request_uri等。 逻辑判断符号,有=, !=, ~, ~*, !~, !~* !表示相反的意思,~为匹配符号,它右侧为正则表达式,区分大小写,而~*为不区分大小写匹配。 目标字符串可以是正则表达式,通常不用加引号,但表达式中有特殊符号时,比如空格、花括号、分号等,需要用单引号引起来。

if ($request_method = POST)  //当请求的方法为POST时,直接返回405状态码
{
return 405; //在该示例中并未用到rewrite规则,if中支持用return指令。
} if ($http_user_agent ~ MSIE) //user_agent带有MSIE字符的请求,直接返回403状态码
{
return 403;
} 如果想同时限制多个user_agent,还可以写成这样 if ($http_user_agent ~ "MSIE|firefox|spider")
{
return 403;
} if(!-f $request_filename) //当请求的文件不存在,将会执行下面的rewrite规则
{
rewrite 语句;
} if($request_uri ~* 'gid=\d{9,12}/') //\d表示数字,{9,12}表示数字出现的次数是9到12次,如gid=123456789/就是符合条件的。
{
rewrite 语句;
}

rewrite中的break和last

https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md

两个指令用法相同,但含义不同,需要放到rewrite规则的末尾,用来控制重写后的链接是否继续被nginx配置执行(主要是rewrite、return指令)。

示例1(连续两条rewrite规则):
server{
listen 80;
server_name test.com;
root /tmp/123.com; rewrite /1.html /2.html ;
rewrite /2.html /3.html ; }
当我们请求1.html时,最终访问到的是3.html,两条rewrite规则先后执行。

1.我们用1.conf虚拟机做实验,创建1.html、2.html、3.html

[root@centos-03 vhost]# cd /data/wwwroot/www.1.com
[root@centos-03 www.1.com]# ls
index.html
[root@centos-03 www.1.com]# touch 1.html 2.html 3.html
[root@centos-03 www.1.com]# ls
1.html 2.html 3.html index.html
[root@centos-03 www.1.com]# echo 111 > 1.html
[root@centos-03 www.1.com]# echo 222 > 2.html
[root@centos-03 www.1.com]# echo 333 > 3.html
[root@centos-03 www.1.com]#

2.配置1.conf,开启rewrite日志添加rewrite规则,通过日志记录我们查看rewrite执行过程,开启error_log notice级别

[root@centos-03 www.1.com]# cd /usr/local/nginx/conf/
[root@centos-03 conf]# vim vhost/1.conf
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
rewrite /1.html /2.html; (这样会连续执行rewrite规则)
rewrite /2.html /3.html;
}
[root@centos-03 conf]#
[root@centos-03 conf]# vim nginx.conf
#error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/1.html
333
[root@centos-03 conf]#
[root@centos-03 conf]# less ../logs/error.log
2018/07/26 17:33:21 [notice] 11371#0: *27 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 17:33:21 [notice] 11371#0: *27 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 17:33:21 [notice] 11371#0: *27 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 17:33:21 [notice] 11371#0: *27 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
(END)

3.我们想让执行完一个rewrite后就终止可以用break或last,这样匹配到222就不往下匹配了

[root@centos-03 conf]# vim vhost/1.conf
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
[root@centos-03 conf]#
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# !curl
curl -x127.0.0.1:80 www.1.com/1.html
222
[root@centos-03 conf]#
[root@centos-03 conf]# tail ../logs/error.log
2018/07/26 18:43:07 [notice] 9429#0: start worker processes
2018/07/26 18:43:07 [notice] 9429#0: start worker process 11428
2018/07/26 18:43:07 [notice] 11371#0: gracefully shutting down
2018/07/26 18:43:07 [notice] 11371#0: exiting
2018/07/26 18:43:07 [notice] 11371#0: exit
2018/07/26 18:43:07 [notice] 9429#0: signal 17 (SIGCHLD) received from 11371
2018/07/26 18:43:07 [notice] 9429#0: worker process 11371 exited with code 0
2018/07/26 18:43:07 [notice] 9429#0: signal 29 (SIGIO) received
2018/07/26 18:43:15 [notice] 11428#0: *28 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 18:43:15 [notice] 11428#0: *28 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
[root@centos-03 conf]#

4.当使用了location的时候last和break不一样,不加break和last的情况

[root@centos-03 conf]# vim vhost/1.conf ^C
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html
{
rewrite /2.html /a.html;
}
location /3.html
{
rewrite /3.html /b.html;
}
}
[root@centos-03 conf]#
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# !curl
curl -x127.0.0.1:80 www.1.com/1.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@centos-03 conf]#
[root@centos-03 conf]# !tail
tail ../logs/error.log
2018/07/26 19:24:53 [notice] 9429#0: signal 29 (SIGIO) received
2018/07/26 19:25:02 [notice] 11465#0: *29 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:25:02 [notice] 11465#0: *29 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:25:02 [notice] 11465#0: *29 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:25:02 [notice] 11465#0: *29 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:25:02 [notice] 11465#0: *29 "/3.html" matches "/3.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:25:02 [notice] 11465#0: *29 rewritten data: "/b.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:25:02 [notice] 11465#0: *29 "/1.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:25:02 [notice] 11465#0: *29 "/2.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:25:02 [error] 11465#0: *29 open() "/data/wwwroot/www.1.com/b.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request:
"GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
[root@centos-03 conf]#

5.加上break的情况

[root@centos-03 conf]# vim vhost/1.conf ^C
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html
{
rewrite /2.html /a.html;
}
location /3.html
{
rewrite /3.html /b.html;
}
}
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# !curl
curl -x127.0.0.1:80 www.1.com/1.html
222
[root@centos-03 conf]# !tail
tail ../logs/error.log
2018/07/26 19:36:11 [notice] 9429#0: start worker processes
2018/07/26 19:36:11 [notice] 9429#0: start worker process 11475
2018/07/26 19:36:11 [notice] 11465#0: gracefully shutting down
2018/07/26 19:36:11 [notice] 11465#0: exiting
2018/07/26 19:36:11 [notice] 11465#0: exit
2018/07/26 19:36:11 [notice] 9429#0: signal 17 (SIGCHLD) received from 11465
2018/07/26 19:36:11 [notice] 9429#0: worker process 11465 exited with code 0
2018/07/26 19:36:11 [notice] 9429#0: signal 29 (SIGIO) received
2018/07/26 19:36:19 [notice] 11475#0: *30 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:36:19 [notice] 11475#0: *30 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
[root@centos-03 conf]#

6.把break改成last试试

[root@centos-03 conf]# vim vhost/1.conf ^C
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html
{
rewrite /2.html /a.html;
}
location /3.html
{
rewrite /3.html /b.html;
}
}
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# !curl
curl -x127.0.0.1:80 www.1.com/1.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@centos-03 conf]# !tail
tail ../logs/error.log
2018/07/26 19:39:05 [notice] 9429#0: signal 17 (SIGCHLD) received from 11475
2018/07/26 19:39:05 [notice] 9429#0: worker process 11475 exited with code 0
2018/07/26 19:39:05 [notice] 9429#0: signal 29 (SIGIO) received
2018/07/26 19:39:12 [notice] 11484#0: *31 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:39:12 [notice] 11484#0: *31 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:39:12 [notice] 11484#0: *31 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:39:12 [notice] 11484#0: *31 rewritten data: "/a.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:39:12 [notice] 11484#0: *31 "/1.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:39:12 [notice] 11484#0: *31 "/2.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2018/07/26 19:39:12 [error] 11484#0: *31 open() "/data/wwwroot/www.1.com/a.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request:
"GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
[root@centos-03 conf]#
结论:
  • 当rewrite规则在location{}外,break和last作用一样,遇到break或last后,其后续的rewrite/return语句不再执行。但后续有location{}的话,还会近一步执行location{}里面的语句,当然前提是请求必须要匹配该location。
  • 当rewrite规则在location{}里,遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。
  • 当rewrite规则在location{}里,遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。

nginx中的return用法

https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/return.md

该指令一般用于对请求的客户端直接返回响应状态码。在该作用域内return后面的所有nginx配置都是无效的。 可以使用在server、location以及if配置中。 除了支持跟状态码,还可以跟字符串或者url链接。

1.直接返回状态码

[root@centos-03 conf]# vim vhost/default.conf ^C
[root@centos-03 conf]# cat vhost/default.conf
server {
listen 80 default_server;
return 403;
}
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# curl -x127.0.0.1:80 fjldsfjdsajfl
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@centos-03 conf]# curl -x127.0.0.1:80 fjldsfjdsajfl -I
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 12:42:18 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive [root@centos-03 conf]#

2.在if中使用return直接返回404

[root@centos-03 conf]# vim vhost/1.conf ^C
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on; if ($request_uri ~ "\.htpasswd|\.bak")
{
return 404;
rewrite /(.*) /aaa.txt; #该行配置不会被执行。
}
}
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 12:50:11 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive [root@centos-03 conf]#

3.返回字符串

[root@centos-03 conf]# vim vhost/1.conf ^C
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on; if ($request_uri ~ "\.htpasswd|\.bak")
{
return 200 "ok";
rewrite /(.*) /aaa.txt; #该行配置不会被执行。
}
}
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 12:53:53 GMT
Content-Type: application/octet-stream
Content-Length: 2
Connection: keep-alive [root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd
ok[root@centos-03 conf]#

4.返回url

[root@centos-03 conf]# vim vhost/1.conf ^C
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on; if ($request_uri ~ "\.htpasswd|\.bak")
{
return 200 "<html><script>window.location.href='//$host$request_uri';</script></html>";
rewrite /(.*) /aaa.txt; #该行配置不会被执行。
}
}
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd
<html><script>window.location.href='//www.1.com/12/.htpasswd';</script></html>[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 13:07:28 GMT
Content-Type: application/octet-stream
Content-Length: 78
Connection: keep-alive [root@centos-03 conf]#

5.301跳转

[root@centos-03 conf]# vim vhost/1.conf ^C
[root@centos-03 conf]# cat vhost/1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on; if ($request_uri ~ "\.htpasswd|\.bak")
{
return 301 http://www.baidu.com;
rewrite /(.*) /aaa.txt; #该行配置不会被执行。
}
}
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 13:12:38 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.baidu.com [root@centos-03 conf]#

rewrite规则语法

https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/rewrite_ruler.md

1.格式:rewrite regex replacement [flag]

* rewrite配置可以在server、location以及if配置段内生效 * regex是用于匹配URI的正则表达式,其不会匹配到$host(域名) * replacement是目标跳转的URI,可以以http://或者https://开头,也可以省略掉$host,直接写$request_uri部分(即请求的链接) * flag,用来设置rewrite对URI的处理行为,其中有break、last、rediect、permanent,其中break和last在前面已经介绍过, rediect和permanent的区别在于,前者为临时重定向(302),而后者是永久重定向(301),对于用户通过浏览器访问,这两者的效果是一致的。 但是,对于搜索引擎蜘蛛爬虫来说就有区别了,使用301更有利于SEO。所以,建议replacemnet是以http://或者https://开头的flag使用permanent。

例一:

location / {
rewrite /(.*) http://www.aming.com/$1 permanent;
}
说明:.*为正则表达式,用()括起来,在后面的URI中可以调用它,第一次出现的()用$1调用,第二次出现的()用$2调用,以此类推。

例二:

location / {
rewrite /.* http://www.aming.com$request_uri permanent;
}
说明:在replacement中,支持变量,这里的$request_uri就是客户端请求的链接

例三:

server{
listen 80;
server_name www.123.com;
root /tmp/123.com;
index index.html;
rewrite /(.*) /abc/$1 redirect;
}
说明:本例中的rewrite规则有问题,会造连续循环,最终会失败,解决该问题有两个方案。
关于循环次数,经测试发现,curl 会循环50次,chrome会循环80次,IE会循环120次,firefox会循环20次。
server{
listen 80;
server_name www.123.com;
root /tmp/123.com;
index index.html;
rewrite /(.*) /abc/$1 break;
}
说明:在rewrite中使用break,会避免循环。
server{
listen 80;
server_name www.123.com;
root /tmp/123.com;
index index.html;
if ($request_uri !~ '^/abc/')
{
rewrite /(.*) /abc/$1 redirect;
}
}
说明:加一个条件限制,也可以避免产生循环

nginx全局变量

https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/variable.md

nginx 常用全局变量

$args	请求中的参数,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2
$content_length HTTP请求信息里的”Content-Length”
$conten_type HTTP请求信息里的”Content-Type”
$document_root nginx虚拟主机配置文件中的root参数对应的值
$document_uri 当前请求中不包含指令的URI,如www.123.com/1.php?a=1&b=2的$document_uri就是1.php,不包含后面的参数
$host 主机头,也就是域名
$http_user_agent 客户端的详细信息,也就是浏览器的标识,用curl -A可以指定
$http_cookie 客户端的cookie信息
$limit_rate 如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
$remote_addr 客户端的公网ip
$remote_port 客户端的port
$remote_user 如果nginx有配置认证,该变量代表客户端认证的用户名
$request_body_file 做反向代理时发给后端服务器的本地资源的名称
$request_method 请求资源的方式,GET/PUT/DELETE等
$request_filename 当前请求的资源文件的路径名称,相当于是$document_root/$document_uri的组合
$request_uri 请求的链接,包括$document_uri和$args
$scheme 请求的协议,如ftp,http,https
$server_protocol 客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr 服务器IP地址
$server_name 服务器的主机名
$server_port 服务器的端口号
$uri 和$document_uri相同
$http_referer 客户端请求时的referer,通俗讲就是该请求是通过哪个链接跳过来的,用curl -e可以指定

1.$args

[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
return 200 "$args";
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.php?a=1&b=2'
a=1&b=2[root@centos-03 vhost]#

2.$content_length

[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.php?a=1&b=2' -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 13:48:13 GMT
Content-Type: application/octet-stream
Content-Length: 7
Connection: keep-alive [root@centos-03 vhost]#

3.$http_user_agent

[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
return 200 "$http_user_agent";
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.php?a=1&b=2'
curl/7.29.0[root@centos-03 vhost]# curl -A "USERANGET" -x127.0.0.1:80 'www.1.com/1.php?a=1&b=2'
USERANGET[root@centos-03 vhost]#

rewrite实战  

域名跳转(域名重定向)
[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite /(.*) http://www.baidu.com/$1 permanent;
access_log /tmp/1.log;
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/a'
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/a' -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 14:21:52 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.baidu.com/a [root@centos-03 vhost]#
示例2(带条件的):
server{
listen 80;
server_name www.aminglinux.com aminglinux.com;
if ($host != 'www.aminglinux.com')
{
rewrite /(.*) http://www.aminglinux.com/$1 permanent;
}
....... }
示例3(http跳转到https):
server{
listen 80;
server_name www.aminglinux.com;
rewrite /(.*) https://www.aminglinux.com/$1 permanent;
....... }
示例4(域名访问二级目录)
server{
listen 80;
server_name bbs.aminglinux.com;
rewrite /(.*) http://www.aminglinux.com/bbs/$1 last;
....... }
示例5(静态请求分离)
server{
listen 80;
server_name www.aminglinux.com;
location ~* ^.+.(jpg|jpeg|gif|css|png|js)$
{
rewrite /(.*) http://img.aminglinux.com/$1 permanent;
} ....... }
或者:
server{
listen 80;
server_name www.aminglinux.com;
if ( $uri ~* 'jpg|jpeg|gif|css|png|js$')
{
rewrite /(.*) http://img.aminglinux.com/$1 permanent;
} ....... }
[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
if ( $uri ~* (jpg|gif|jpeg)$){
rewrite /(.*) http://www.baidu.com/$1 permanent;
}
access_log /tmp/1.log;
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.jpg' -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 14:44:15 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.baidu.com/1.jpg [root@centos-03 vhost]#
防盗链
示例6
server{
listen 80;
server_name www.aminglinux.com;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
{
valid_referers none blocked server_names *.aminglinux.com aminglinux.com *.aming.com aming.com;
if ($invalid_referer)
{
rewrite /(.*) http://img.aminglinux.com/images/forbidden.png;
}
} ....... }
说明:*这里是通配,跟正则里面的*不是一个意思,none指的是referer不存在的情况(curl -e 测试),
blocked指的是referer头部的值被防火墙或者代理服务器删除或者伪装的情况,
该情况下,referer头部的值不以http://或者https://开头(curl -e 后面跟的referer不以http://或者https://开头)。
或者:
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
{
valid_referers none blocked server_names *.aminglinux.com *.aming.com aminglinux.com aming.com;
if ($invalid_referer)
{
return 403;
}
}

1.当我们用域名www.1.com访问时返回404正确,用www.2.com访问返回403没权限

[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
access_log /tmp/1.log;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
{
valid_referers none blocked server_names *.1.com 1.com; (白名单域名)
if ($invalid_referer)
{
return 403;
}
}
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.jpg' -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 15:13:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive [root@centos-03 vhost]# curl -x127.0.0.1:80 'www.2.com/1.jpg' -I
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 15:13:48 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive [root@centos-03 vhost]#
[root@centos-03 vhost]# curl -e "http://www.1.com" -x127.0.0.1:80 'www.1.com/1.jpg' -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 15:19:00 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive [root@centos-03 vhost]# curl -e "http://www.2.com" -x127.0.0.1:80 'www.1.com/1.jpg' -I
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 15:19:09 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive [root@centos-03 vhost]#
伪静态
示例7(discuz伪静态):
location / {
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
}
rewrite多个条件的并且
示例8:
location /{
set $rule 0;
if ($document_uri !~ '^/abc')
{
set $rule "${rule}1";
}
if ($http_user_agent ~* 'ie6|firefox')
{
set $rule "${rule}2";
}
if ($rule = "012")
{
rewrite /(.*) /abc/$1 redirect;
}
}
[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
access_log /tmp/1.log;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
{
valid_referers none blocked server_names *.1.com 1.com;
if ($invalid_referer)
{
return 403;
}
} set $a 0;
if ($request_uri !~ "^/abc/")
{
set $a "${a}1";
} if ($http_user_agent ~ 'IE|chrome')
{
set $a "${a}2";
} if ($a = "012")
{
return 406;
}
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 -A "chrome" www.1.com/1.html -I
HTTP/1.1 Not Acceptable
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 15:46:05 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive [root@centos-03 vhost]#
[root@centos-03 vhost]# curl -x127.0.0.1:80 -A "chrome" www.1.com/abc/1.html -I
HTTP/1.1 Not Found
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 15:50:34 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive [root@centos-03 vhost]#

nginx的location配置  

https://coding.net/u/aminglinux/p/nginx/git/blob/master/location/ruler.md

1.安装echo-nginx-module模块,这样我们就可以使用echo命令了,首先用git把源码克隆下来

[root@centos-03 src]# cd /usr/local/src/
[root@centos-03 src]# yum install -y git
[root@centos-03 src]# git clone https://github.com/openresty/echo-nginx-module.git
正克隆到 'echo-nginx-module'...
remote: Counting objects: 2991, done.
remote: Total 2991 (delta 0), reused 0 (delta 0), pack-reused 2991
接收对象中: 100% (2991/2991), 1.13 MiB | 152.00 KiB/s, done.
处理 delta 中: 100% (1607/1607), done.
[root@centos-03 src]#
[root@centos-03 src]# ls
echo-nginx-module filebeat-6.3.1-x86_64.rpm nginx-1.14.0 nginx-1.14.0.tar.gz
[root@centos-03 src]# cd nginx-1.14.0
[root@centos-03 nginx-1.14.0]# make clean
rm -rf Makefile objs
[root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@centos-03 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module/
[root@centos-03 nginx-1.14.0]# make && make install
[root@centos-03 nginx-1.14.0]# /etc/init.d/nginx restart
Restarting nginx (via systemctl): [ 确定 ]
[root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module/
[root@centos-03 nginx-1.14.0]#

2.测试echo命令是否能用

[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
access_log /tmp/1.log;
location /abc/
{
echo 123;
}
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/abc/www
123
[root@centos-03 vhost]#

location语法

nginx location语法规则:location [=|~|~*|^~] /uri/ { … } nginx的location匹配的变量是$uri

符号	说明
= 表示精确匹配
^~ 表示uri以指定字符或字符串开头
~ 表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配
/ 通用匹配,任何请求都会匹配到
=  高于  ^~  高于  ~* 等于 ~  高于  /
location = "/12.jpg" { ... }
如:
www.aminglinux.com/12.jpg 匹配
www.aminglinux.com/abc/12.jpg 不匹配 location ^~ "/abc/" { ... }
如:
www.aminglinux.com/abc/123.html 匹配
www.aminglinux.com/a/abc/123.jpg 不匹配 location ~ "png" { ... }
如:
www.aminglinux.com/aaa/bbb/ccc/123.png 匹配
www.aminglinux.com/aaa/png/123.html 匹配 location ~* "png" { ... }
如:
www.aminglinux.com/aaa/bbb/ccc/123.PNG 匹配
www.aminglinux.com/aaa/png/123.html 匹配 location /admin/ { ... }
如:
www.aminglinux.com/admin/aaa/1.php 匹配
www.aminglinux.com/123/admin/1.php 不匹配
有些资料上介绍location支持不匹配 !~,
如: location !~ 'png'{ ... }
这是错误的,location不支持 !~ 如果有这样的需求,可以通过if来实现,
如: if ($uri !~ 'png') { ... } 注意:location优先级小于if

1.echo 在生产环境下不用,只是用做测试  

优先级测试域名会匹配到两个location规则由于~*的优先级高于/优先级,所以最终匹配到了~*

[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
access_log /tmp/1.log;
location /abc/
{
echo "/";
} location ~* abc
{
echo "~*"; }
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/abc/www
~*
[root@centos-03 vhost]#
[root@centos-03 vhost]# vim 1.conf ^C  (^~优先级高于~*)
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
access_log /tmp/1.log;
location ^~ /abc
{
echo "^~";
} location ~* abc
{
echo "~*"; }
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/abc/www
^~
[root@centos-03 vhost]#
[root@centos-03 vhost]# vim 1.conf ^C (等于号优先级高于^~)
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
access_log /tmp/1.log;
location ^~ /abc
{
echo "^~";
} location = /abc/1.html
{
echo "="; }
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/abc/1.html
=
[root@centos-03 vhost]#

nginx实战二的更多相关文章

  1. nginx实战

    原文:http://www.cnblogs.com/yucongblog/p/6289628.html nginx实战   (一) nginx环境的搭建安装流程: 1 通过ftp将nginx-1.11 ...

  2. kubernetes实战(二十八):Kubernetes一键式资源管理平台Ratel安装及使用

    1. Ratel是什么? Ratel是一个Kubernetes资源平台,基于管理Kubernetes的资源开发,可以管理Kubernetes的Deployment.DaemonSet.Stateful ...

  3. 深入浅出Nginx实战与架构

    本文主要内容如下(让读者朋友们深入浅出地理解Nginx,有代码有示例有图): 1.Nginx是什么? 2.Nginx具有哪些功能? 3.Nginx的应用场景有哪些? 4.Nginx的衍生生态有哪些? ...

  4. coreseek实战(二):windows下mysql数据源部分配置说明

    coreseek实战(二):windows下mysql数据源部分配置说明 关于coreseek在windows使用mysql数据源的配置,以及中文分词的详细说明,请参考官方文档: mysql数据源配置 ...

  5. 【NFS项目实战二】NFS共享数据的时时同步推送备份

    [NFS项目实战二]NFS共享数据的时时同步推送备份 标签(空格分隔): Linux服务搭建-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品, ...

  6. Nginx教程(二) Nginx虚拟主机配置

    Nginx教程(二) Nginx虚拟主机配置 1 虚拟主机管理 1.1 Nginx管理虚拟主机 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主 ...

  7. chrome调试工具高级不完整使用指南(实战二)

    3.3 给页面添加测试脚本 在现实的工作中,我们往往会遇到一些问题在线上就会触发然后本地就触发不了的问题.或者是,要给某个元素写一个测试脚本.这个时候如果是浏览器有提供一个添加脚本的功能的话,那么我们 ...

  8. Nginx实战之让用户通过用户名密码认证访问web站点

    1.Nginx实战之让用户通过用户名密码认证访问web站点 [root@master ~]# vim /usr/local/nginx/conf/extra/www.conf server { lis ...

  9. Python爬虫实战二之爬取百度贴吧帖子

    大家好,上次我们实验了爬取了糗事百科的段子,那么这次我们来尝试一下爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 前言 亲爱的们,教程比较旧了,百度贴吧页面可能改版,可能代码不 ...

随机推荐

  1. Jquery UI 中Tree组件的json格式,java递归拼接demo

    数据库中表数据,如下图: 实现的需求是,如果suporgcode数据为null 的情况下,表示在一级节点 "请选择" 的二级节点,然后是如:3和36 是1的子节点,一步一步的节点延 ...

  2. MySQL 自动添加DateTime

    数据库表里有个AddDate字段,DateTime类型,不能为空,添加记录时自动添加当前时间. 以前用MSSQL 在默认值写个 getdate()  就OK! MySQL写这个报错啊,迷糊! 查了一下 ...

  3. js+Ajax,Get和Post在使用上的区别

    get和post方法最大的不同在于: 1.get方法传值参数在url里面,而post参数放send里面 2.post方法必须加上 xmlHttp.setRequestHeader("Cont ...

  4. [gevent源代码分析] 深度分析gevent执行流程

    一直对gevent执行流程比較模糊,近期看源代码略有所得.不敢独享.故分享之. gevent是一个高性能网络库,底层是libevent,1.0版本号之后是libev.核心是greenlet.geven ...

  5. win7 64位的 svchost.exe 占用内存过大的问题

    svchost.exe 是用来启动系统服务的,所以某个 svchost.exe 占用内存过大,可能就是它启动的那个服务占用内存过大,所以只要停止并禁用那个服务就行了. 一般来说占用内存最大的服务是 S ...

  6. RecyclerView.ItemDecoration 间隔线

    内容已更新到:https://www.cnblogs.com/baiqiantao/p/19762fb101659e8f4c1cea53e7acb446.html 目录一个通用分割线ItemDecor ...

  7. JS-获取图片地址

    var url=document.getElementById("a001").src; 或var url=document.getElementById("a001&q ...

  8. 转:从产品经理的角度算一算,做一个app需要花多少钱?

    http://mp.weixin.qq.com/s?__biz=MzA4NTM5MTgzNQ==&mid=400127477&idx=2&sn=6ab90b7028deed78 ...

  9. Report Studio中目录结构报表浅析

    一:场景:在一个报表中如果存在多个页面,每个页面显示不同的数据,如何通过目录控件实现对每一个报表的友好访问呢?下面我们就来看一下下面的效果,如下图1,2 图1:-------------------- ...

  10. [ACM] POJ 2524 Ubiquitous Religions (并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted:  ...