# Nginx静态资源的配置指令 ## 1. listen指令 - listen:用来配置监听端口。 | 语法 | listen address[:port] [default_server]...;
listen port [default_server]...; | |-----|---------------------------------------------------------------------------------| | 默认值 | listen *:80 \*:8000 | | 位置 | server | - 配置方式 ```editorconfig listen 127.0.0.1:8000; // listen localhost:8000 监听指定的IP和端口 listen 127.0.0.1; 监听指定IP的所有端口 listen 8000; 监听指定端口上的连接 listen *:8000; 监听指定端口上的连接, 和上面没有什么区别 ``` - **default_server属性是标识符,用来将此虚拟主机设置成默认主机**。 - 所谓的默认主机指的是**如果没有匹配到对应的address:port,则会默认执行的**。 - 如果不指定默认使用的是**第一个server**。 ```editorconfig server{ listen 8080; server_name 127.0.0.1; location /{ root html; index index.html; } } server{ listen 8080 default_server; server_name localhost; default_type text/plain; return 444 'This is a error request'; } ``` ## 2. server_name指令 - server_name:用来设置虚拟主机服务名称。 - 127.0.0.1 、 localhost 、域名[www.baidu.com | www.jd.com] | 语法 | server_name name ...;
name可以提供多个中间用空格分隔 | |-----|----------------------------------------------| | 默认值 | server_name ""; | | 位置 | server | - 关于server_name的配置方式有三种,分别是: - 精确匹配 - 通配符匹配 - 正则表达式匹配 - 配置方式一:精确匹配 ```editorconfig server { listen 80; server_name www.itcast.cn www.itheima.cn; ... } ``` - 配置方式二:使用通配符配置 - server_name中支持通配符"*",但需要注意的是**通配符不能出现在域名的中间**,只能出现在首段或尾段,如: ```editorconfig server { listen 80; server_name *.itcast.cn www.itheima.*; # www.itcast.cn abc.itcast.cn www.itheima.cn www.itheima.com ... } ``` - 配置三:使用正则表达式配置 - server_name中可以使用正则表达式,并且使用`~`作为正则表达式字符串的开始标记。 | 代码 | 说明 | |-------|--------------------------------------------| | ^ | 匹配搜索字符串开始位置 | | $ | 匹配搜索字符串结束位置 | | . | 匹配除换行符\n之外的任何单个字符 | | \ | 转义字符,将下一个字符标记为特殊字符 | | [xyz] | 字符集,与任意一个指定字符匹配 | | [a-z] | 字符范围,匹配指定范围内的任何字符 | | \w | 与以下任意字符匹配 A-Z a-z 0-9 和下划线,等效于[A-Za-z0-9_] | | \d | 数字字符匹配,等效于[0-9] | | {n} | 正好匹配n次 | | {n,} | 至少匹配n次 | | {n,m} | 匹配至少n次至多m次 | | * | 零次或多次,等效于{0,} | | + | 一次或多次,等效于{1,} | | ? | 零次或一次,等效于{0,1} | - 配置如下: ```editorconfig server{ listen 80; server_name ~^www\.(\w+)\.com$; default_type text/plain; return 200 $1 $2 ..; } 注意 ~后面不能加空格,括号可以取值 ``` - 匹配执行顺序 - 由于server_name指令支持通配符和正则表达式,因此在包含多个虚拟主机的配置文件中, **可能会出现一个名称被多个虚拟主机的server_name匹配成功**,当遇到这种情况,当前的请求交给谁来处理呢? ```editorconfig server{ listen 80; server_name ~^www\.\w+\.com$; default_type text/plain; return 200 'regex_success'; } server{ listen 80; server_name www.itheima.*; default_type text/plain; return 200 'wildcard_after_success'; } server{ listen 80; server_name *.itheima.com; default_type text/plain; return 200 'wildcard_before_success'; } server{ listen 80; server_name www.itheima.com; default_type text/plain; return 200 'exact_success'; } server{ listen 80 default_server; server_name _; default_type text/plain; return 444 'default_server not found server'; } ``` - 结论: ```txt exact_success wildcard_before_success wildcard_after_success regex_success default_server not found server!! ``` - No1:准确匹配server_name - No2:通配符在开始时匹配server_name成功 - No3:通配符在结束时匹配server_name成功 - No4:正则表达式匹配server_name成功 - No5:被默认的default_server处理,如果没有指定默认找第一个server - location指令 ```editorconfig server{ listen 80; server_name localhost; location / { } location /abc{ } ... } ``` - location:用来设置请求的URI | 语法 | location [ = \ ~ \ ~* \ ^~ \@ ] uri{...} | |-----|------------------------------------------------------| | 默认值 | — | | 位置 | server,location | - uri变量是待匹配的请求字符串,可以不包含正则表达式,也可以包含正则表达式,那么nginx服务器在搜索匹配location的时候, - 是先使用不包含正则表达式进行匹配,找到一个匹配度最高的一个,然后在通过包含正则表达式的进行匹配,如果能匹配到直接访问, - 匹配不到,就使用刚才匹配度最高的那个location来处理请求。 - 不带符号,要求必须以指定模式开始 ```editorconfig server { listen 80; server_name 127.0.0.1; location /abc{ default_type text/plain; return 200 "access success"; } } 以下访问都是正确的 http://192.168.200.133/abc http://192.168.200.133/abc?p1=TOM http://192.168.200.133/abc/ http://192.168.200.133/abcdef ``` - = : 用于不包含正则表达式的uri前,必须与指定的模式精确匹配 ```editorconfig server { listen 80; server_name 127.0.0.1; location =/abc{ default_type text/plain; return 200 "access success"; } } 可以匹配到 http://192.168.200.133/abc http://192.168.200.133/abc?p1=TOM 匹配不到 http://192.168.200.133/abc/ http://192.168.200.133/abcdef ``` - ~ : 用于表示当前uri中包含了正则表达式,并且区分大小写 - ~*: 用于表示当前uri中包含了正则表达式,并且不区分大小写 - 换句话说,如果uri包含了正则表达式,需要用上述两个符合来标识 ```editorconfig server { listen 80; server_name 127.0.0.1; location ~^/abc\w${ default_type text/plain; return 200 "access success"; } } server { listen 80; server_name 127.0.0.1; location ~*^/abc\w${ default_type text/plain; return 200 "access success"; } } ``` - ^~: 用于不包含正则表达式的uri前,功能和不加符号的一致,唯一不同的是,如果模式匹配,那么就停止搜索其他模式了。 ```editorconfig server { listen 80; server_name 127.0.0.1; location ^~/abc{ default_type text/plain; return 200 "access success"; } } ``` - **设置请求资源的目录root / alias** - root:设置请求的根目录 | 语法 | root path; | |-----|----------------------| | 默认值 | root html; | | 位置 | http、server、location | - path为Nginx服务器接收到请求以后查找资源的根目录路径。 - alias:用来更改location的URI | 语法 | alias path; | |-----|-------------| | 默认值 | — | | 位置 | location | - path为修改后的根路径。 - 以上两个指令都可以来指定访问资源的路径,那么这两者之间的区别是什么? - 举例说明: - (1)在`/usr/local/nginx/html`目录下创建一个 images目录,并在目录下放入一张图片`mv.png`图片 ```editorconfig location /images { root /usr/local/nginx/html; } ``` - 访问图片的路径为: http://192.168.200.133/images/mv.png - (2)如果把root改为alias ```editorconfig location /images { alias /usr/local/nginx/html; } ``` - 再次访问上述地址,页面会出现404的错误,查看错误日志会发现是因为地址不对,所以验证了: ```shell root的处理结果是: root路径+location路径 /usr/local/nginx/html/images/mv.png alias的处理结果是:使用alias路径替换location路径 /usr/local/nginx/html/images ``` - 需要在alias后面路径改为 ```editorconfig location /images { alias /usr/local/nginx/html/images; } ``` - (3)如果location路径是以/结尾,则alias也必须是以/结尾,root没有要求 - 将上述配置修改为 ```editorconfig location /images/ { alias /usr/local/nginx/html/images; } ``` - 访问就会出问题,查看错误日志还是路径不对,所以需要把alias后面加上 / - root的处理结果是: root路径+location路径 - alias的处理结果是:使用alias路径替换location路径 - alias是一个目录别名的定义,root则是最上层目录的含义。 - 如果location路径是以/结尾,则alias也必须是以/结尾, - **index指令** - index:设置网站的默认首页 | 语法 | index file ...; | |-----|----------------------| | 默认值 | index index.html; | | 位置 | http、server、location | - index后面可以跟多个设置,如果访问的时候没有指定具体访问的资源,则会依次进行查找,找到第一个为止。 - 举例说明: ```editorconfig location / { root /usr/local/nginx/html; index index.html index.htm; } 访问该location的时候,可以通过 http://ip:port/,地址后面如果不添加任何内容,则默认依次访问index.html和index.htm,找到第一个来进行返回 ``` - **error_page指令** - error_page:设置网站的错误页面 | 语法 | error_page code ... [=[response]] uri; | |-----|----------------------------------------| | 默认值 | — | | 位置 | http、server、location...... | - 当出现对应的响应code后,如何来处理。 - 举例说明: - (1)可以指定具体跳转的地址 ```editorconfig server { error_page 404 http://www.itcast.cn; } ``` - (2)可以指定重定向地址 ```editorconfig server{ error_page 404 /50x.html; error_page 500 502 503 504 /50x.html; location =/50x.html{ root html; } } ``` - (3)使用location的@符合完成错误信息展示 ```editorconfig server{ error_page 404 @jump_to_error; location @jump_to_error { default_type text/plain; return 404 'Not Found Page...'; } } ``` - 可选项`=[response]`的作用是用来将相应代码更改为另外一个 ```editorconfig server{ error_page 404 =200 /50x.html; location =/50x.html{ root html; } } 这样的话,当返回404找不到对应的资源的时候,在浏览器上可以看到,最终返回的状态码是200,这块需要注意下,编写error_page后面的内容,404后面需要加空格,200前面不能加空格 ```