Nginx静态资源部署优化_部署01

Nginx静态资源部署

1.nginx静态资源配置指令

listen 用于配置监听端口

指令语法

listen address[:port] [default_server] ... / listen port [default_server]...

指令文档
1
http://nginx.org/en/docs/http/ngx_http_core_module.html#listen
用例
1
2
3
4
listen 127.0.0.1:8080;
listen 127.0.0.1;
listen 8080;
listen *:8080;
default_server

默认主机标识符, 即没有匹配到对应的address:port时, 默认执行的server块

若不指定, 默认使用第一个server

1
listen 8080 default_server; # 当前监听所在server块为默认服务

server_name 用于设置虚拟主机服务名称

指令语法

server_name name1 [name2...] 中间用空格分隔

指令文档
1
http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name
匹配方式
  • 精确匹配

    1
    2
    3
    4
    server{
    listen 80;
    server_name www.host1.com www.host2.com;
    }
  • 匹配通配符

    * 可以放在域名首段或者尾段, 但不能出现在域名中间

    1
    2
    3
    4
    server{
    listen 80;
    server_name *.host1.com www.host2.*;
    }
  • 正则匹配

    ~ 为正则表达式开启标记

    1
    2
    3
    4
    5
    server{
    listen 80;
    server_name ~^www\.(\w+)\.com$;
    return 200 $1;
    }
匹配执行顺序

精确匹配 > 前通配符 > 后通配符 > 正则匹配 > 默认server

location 用于设置请求URI

指令语法

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

以匹配度最高的路由作为实际转发路由

指令文档
1
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
匹配方式
  • 以指定模式访问

    1
    2
    3
    4
    5
    6
    7
    8
    server{
    listen 80;
    server_name 127.0.0.1;
    location /abc{
    default_type text/plain;
    return 200 'access success';
    }
    }
    1
    2
    3
    4
    则如下方式均可访问
    http://127.0.0.1/abc
    http://127.0.0.1/abc?p1=test
    http://127.0.0.1/abcde
  • 以精确匹配模式访问(不含正则)

    1
    2
    3
    4
    5
    6
    7
    8
    server{
    listen 80;
    server_name 127.0.0.1;
    location =/abc{
    default_type text/plain;
    return 200 'access success';
    }
    }
    1
    2
    3
    4
    5
    6
    则以下方式可访问:
    http://127.0.0.1/abc
    http://127.0.0.1/abc?p1=test
    以下无法访问:
    http://127.0.0.1/abc/
    http://127.0.0.1/abcde
  • 以正则匹配模式访问

    ~ : 用于表示当前URI中包含正则, 且区分大小写

    ~* : 用于表示当前URI包含正则, 且不区分大小写

    1
    2
    3
    4
    5
    6
    7
    8
    9
    server{
    listen 80;
    server_name 127.0.0.1;
    # location ~*^/abc\w${ # 不区分大小写
    location ~^/abc\w${
    default_type text/plain;
    return 200 'access success';
    }
    }
    1
    2
    3
    4
    5
    6
    则以下方式可访问:
    http://127.0.0.1/abcd
    以下无法访问:
    http://127.0.0.1/abc
    http://127.0.0.1/abc/
    http://127.0.0.1/abcde
  • 匹配后停止

    ^~ : 用于不包含正则的URI前, 功能和不加符号的一致, 表示如果模式匹配, 就停止搜索其他模式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    server{
    listen 80;
    server_name 127.0.0.1;
    location ^~/abcd{
    default_type text/plain;
    return 200 'abcd access success';
    }
    location ~^/abc\w${
    default_type text/plain;
    return 200 'access success';
    }
    }
    1
    2
    http://127.0.0.1/abcd
    访问到第一个location块后匹配, 就不再向后搜索, 直接使用该URI
  • nginx内部跳转

    @ 表示在nginx中跳转到对应的location

    1
    2
    3
    4
    5
    6
    location /404 {
    error_page 404 @404_error;
    }
    location @404_error {
    ...
    }

root/alias 设置请求资源的目录

指令语法

root path 设置请求的根目录

1
root html;

path 为Nginx服务器收到请求后查找资源的根目录路径

alias path 用于更改location的URI

path 为修改后的根目录

root/alias 区别
1
2
root  处理结果为: root路径+location路径
alias 处理结果为: 使用alias路径替换location路径
1
2
3
4
5
6
7
8
9
10
11
以下三个配置功效相同
location /images {
root /var/www/test; # 结果为: /var/www/test/images
}

location /images {
alias /var/www/test/images; # 替换原有images为 /var/www/test/images
}
location /images/ {
alias /var/www/test/images/;# 替换原有images/为 /var/www/test/images/
}

index 用于设置网站的默认首页

指令语法

index file;

1
index index index.html;

index 后可以跟多个设置, 如果访问URI没有指定具体访问的资源, 则会依次查找, 直到找到第一个

指令位置
1
http / server / location

如:

1
2
3
4
5
location / {
root /var/www/test;
index index index.html index.htm;
}
访问时, 可以直接使用 http://test.project.com 访问, 后面不加任何参数

error_page 设置网站的错误页面

指令语法

error_page code ...[=[reponse]] rui;

指令位置
1
http / server / location
实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1. 具体跳转页面地址
server {
error_page 404 http://test.project.com/404.html;
}
2. 指定重定向地址
server {
error_page 404 /50x.html;
error_page 500 502 503 504 /50x.html;
location =/50x.html{
root html;
}
}
3. 使用location的@符号完成错误信息展示
server {
error_page 404 @to_error_page;
location @to_error_page{
default_type text/plain;
return 404 'Not Found Page!';
}
}
response

可选项 =[response] 用于将匹配状态码改为另一个状态码返回浏览器

1
2
3
4
5
6
server{
error_page 404 =200 /50x.html;
location =/50x.html{
...
}
}

2.nginx静态资源优化配置语法

sendfile

sendfile on|off 用于开启高效文件传输模式

image-20211118235723693

image-20211118235844001

1
使用sendfil()之后, 减少了两次copy(), 以及用户态与内核态切换的耗费

tcp_nopush

tcp_nopush on|off

用于提升网络包的传输效率, 必须在sendfile打开的状态下才会生效

tcp_nodelay

tcp_nodelay on|off

用于提高网络包传输的实时性, 必须在keep-alive 连接开启时才生效

image-20211119001043741

1
tcp_nopush和tcp_nodelay在linux 2.5.9之后可以兼容, 两者都开启可以大大加快数据传输效率