Skip to content

安装

windows 安装

windows 直接下载官方二进制包,添加到环境变量

其他版本安装

官网download下面有个平台的快捷安装方法

docker 安装

需要注意的是配置转发的时候不是127.0.0.1(因为指向的是nginx容器本身)

命令大全

指定配置文件运行

nginx -f xxx.conf

检查配置

nginx -t 检查配置文件
nginx -T 检查并输出配置文件

重启、停止

nginx -s restart
nginx -s stop

systemctl 管理

systemctl start nginx
systemctl stop nginx
systemctl restart nginx

配置文件

server_name

server_name 支持精准匹配、通配符,正则匹配(^)开头。

location 语法

location [= | ^~ | ~* | ~ ] xxx

  • = 精准匹配优先级最高。
  • ^~ 前缀匹配, 按照最长的前缀匹配,如果命中,则会阻止正则,但不会组织普通路径
  • ~ 区分大小写的正则匹配,正则匹配只要命中就生效。
  • ~* 不区分大小写的正则匹配
  • 不加符号,表示前缀匹配, 采用最长匹配
  • / 兜底匹配 按照 上述顺序匹配

案例分析

conf
location /test_1 {
    return 400;
}
location ^~ /test {
    return 401;
}

如上如果path为/test_1,返回的是400,说明^~优先级并不比普通匹配高

例子2

conf
location /test_1 {
    return 400;
}
location ^~ /test {
    return 401;
}
location ~ /test {
    return 402;
}

会返回 402, 因为会先命中 400 ,然后不会阻止正则

静态服务器root、alias

conf
location /img/ {
    root some/path; # /img/image/1  => some/path/img/image/1 会将整个路径添加到 root 路径中
    index index.html index.htm;   => # /user/image/1  => some/path/user/image/1 或者1.html
}

localtion /img/ {
    alias some/path; # /img/image/1  => some/path/image/1 会舍弃命中部分,所以要求location 和alias上级目录一致,所以location一定是一个目录,一定需要在末尾添加 / 
}

proxy_pass 解析

目标地址后不带/,表示将请求的path拼接在target之后,如果带,表示将请求命中之外的拼接在target后