Nginx学习笔记(三、Nginx虚拟主机与access_log)

目录:

  • server_name
  • location
  • access_log

server_name

server_name处于http块中的server块,它是虚拟主机中的重要参数,可设置基于域名的虚拟主机,并且server_name支持正则表达式的配置

1、精确匹配:

1 # 来自http://yuming.test的请求会发送到该主机上
2 server_name = yuming.test;
3 
4 # 来自http://yuming.test和http://www.yuming.test的请求会发送到该主机上
5 server_name = yuming.test www.yuming.test;

2、通配符匹配:

1 # 任何以yuming.test结尾的请求会发送到该主机
2 server_name = *.yuming.test;
3 server_name = .yuming.test;
4 
5 # 以yuming.开头的请求会发送到该主机
6 server_name = yuming.*;

3、正则表达式:

1 server_name = ~^(?.+).yuming.test$;

server_name优先级:精确匹配 > 以通配符开始的字符串匹配 > 以通配符结束的字符串匹配 > 正则表达式匹配

location

同样的location页处于http块中的server块,它用于重定向客户端URI或者内部重定向访问,也支持正则表达式。

语法:location [=|~|~*|^~]uri {...}

1、精确匹配:/uri

1 # 如http://localhost/8080
2 location / {...}

2、区分大小写的正则匹配:~pattern

1 # 如http://localhost/8080/test.js
2 location ~\.(gif|jpg|png|js|css)$ {...}

3、不区分大小写的正则匹配:~*pattern

1 # 如http://localhost/8080/test.CSS
2 location ~\.css$ {...}

4、前缀匹配:^~uri

1 # 如http://localhost/8080/test.CSS
2 location ^~/static/ {...}

5、不带任何修饰符匹配(相当于前缀匹配):/uri

1 # 如http://localhost/8080/register
2 location /register {...}

6、通用匹配(未匹配到其它location会匹配到此location,相当于switch中的default):/

1 location / {...}

location优先级:精确匹配 > 正则表达式匹配 > 普通字符匹配

access_log

Nginx支持对服务日志的格式、大小、输出等进行配置,分别是access_log和log_format指令。

1、access_log:access_log path [format [buffer=size]];

path:服务器日志文件存放路径和名称。

format:可选项。自定义服务日志的格式的格式字符串,也可以使用log_format定义好的格式。

size:临时存放日志的内存缓冲区大小。

2、log_format:log_format name string...;

name:格式字符串的名字,默认是combined。

string:服务日志的格式字符串。

示例:

1 access_log logs/access_log example;
2 log_format example '$remote_addr -[$time_local] $request'
3                    '$status $body_bytes_send $http_refer'
4                    '$http_user_agent'

 

posted @ 2020-11-01 14:28  被猪附身的人  阅读(327)  评论(0编辑  收藏  举报