nginx常用配置和用法

nginx中root和alias的区别

访问地址:http://localhost/website
案例1:
location /website/ {
   root /var/lib/www;
   autoindex on;
}
案例2:
location /website/ {
        alias /var/lib/www;
        autoindex on;
}

如果是root,那么真实访问路径是:/var/lib/www/website/
如果是alias,那么真实的访问路径是:/var/lib/www/



2、nginx中try_file用法
它可以按顺序检查文件是否存在,如果存在就返回,

location /images/ {
    root /opt/html/;
    try_files $uri   $uri/  /images/default.gif; 
}

固定写法
$uri 是请求文件的路径
$uri/ 是请求目录的路径

意思是:在/opt/html/images/default.gif找到这个文件并返回

3、路由转发

http://localhost/apple-app-site-association
其中apple-app-site-association是一个文件,目的是返回apple-app-site-association文件--------》IOS唤醒配置

location ~(apple-app-site-association) {
   alias  /usr/local/web_project/weixin/;
   try_files $uri $uri/ apple-app-site-association;
   autoindex on;
}

http://localhost/app/link
目的是返回apple-app-site-association文件---------》IOS唤醒配置

location ^~ /app/link/ {
   root /usr/local/web_project/weixin/;
   try_files $uri $uri/ apple-app-site-association;
   autoindex on;
 }
或者如下
location  /app/link/ {
   root /usr/local/web_project/weixin/;
   try_files $uri $uri/ apple-app-site-association;
   autoindex on;
 }

http://localhost/test.txt
返回test.txt文件------》微信公众账号对网址进行授权

location  ~ \.(txt)$ {
    root /mnt/web_project/test/weixin/;
    autoindex on;
 }

4、location匹配策略

location / {
  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
  # 但是正则和最长字符串会优先匹配
}

location /documents/ {
   # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
   # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
}

location ^~ /documents/ {
   # 匹配任何以 /documents/ 开头的地址,匹配符合以后,
   # 只有后面的正则表达式匹配到时,还要继续往下搜索内容更加长的
}

 

1) 先检测匹配项的内容为非正则表达式修饰语的 location,然后再检测匹配项的内容为正则表达式修饰语的 location。

2) 匹配项的内容为正则与非正则都匹配的 location,按照匹配项的内容为正则匹配的 location 执行。

3) 所有匹配项的内容均为非正则表达式的 location,按照匹配项的内容完全匹配的内容长短进行匹配,即匹配内容多的 location 被执行。

4) 所有匹配项的内容均为正则表达式的 location,按照书写的先后顺序进行匹配,匹配后就执行,不再做后续检测。

5、location 基本语法使用情况
语法格式:location [=|~|~*|^~|@] pattern { ... }       
[=|~|~*|^~|@] 被称作 location modifier ,这会定义 Nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式

语法内容:
=    开头表示精确匹配
^~  开头表示uri以某个常规字符串开头,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)

~   开头表示区分大小写的正则匹配
~*  开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/   通用匹配,任何请求都会匹配到。


location              好比是java中的if条件,
[=|~|~*|^~|@]     好比是java中的 equals、contains等等
pattern              好比是java中的值

5.1   =
Example:
server {
    server_name localhost;
    location = /abcd {
    
    }
}

匹配情况:
http://localhost/abcd        # 正好完全匹配
http://localhost/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
http://localhost/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
http://localhost/abcd/       # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配
http://localhost/abcde       # 不匹配,因为不是完全匹配

5.2  (None) 什么都不写---------------》相当于/ 通用匹配
可以不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式

Example:
server {
    server_name localhost;
    location  /abcd {
    
    }
}

匹配情况:
http://localhost/abcd           # 正好完全匹配
http://localhost/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
http://localhost/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
http://localhost/abcd/       # 末尾存在反斜杠(trailing slash)也属于匹配范围内
http://localhost/abcde       # 仍然匹配,因为 URI 是以 pattern 开头的

5.3  ~
这个 location modifier 对大小写敏感,且 pattern 须是正则表达式

Example:
server {
    server_name localhost;
    location  ~ ^/abcd$ {
    
    }
}

匹配情况:
http://localhost/abcd        # 完全匹配
http://localhost/ABCD        # 不匹配,~ 对大小写是敏感的
http://localhost/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
http://localhost/abcd/       # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
http://localhost/abcde       # 不匹配正则表达式 ^/abcd$

5.4   ~*
与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式

Example:
server {
    server_name localhost;
    location  ~* ^/abcd$ {
    
    }
}

匹配情况:
http://localhost/abcd        # 完全匹配
http://localhost/ABCD        # 匹配,这就是它不区分大小写的特性
http://localhost/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
http://localhost/abcd/       # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
http://localhost/abcde       # 不匹配正则表达式 ^/abcd$

5.5   ^~
匹配情况类似 2. (None) 的情况,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)


5.6  @
用于定义一个 Location 块,且该块不能被外部 Client 所访问,只能被 Nginx 内部配置指令所访问,比如 try_files or error_page

实战案列:https://blog.csdn.net/qq_39291929/article/details/109260558

server {
    listen      80;
    server_name example.com;

    location / {
       root /home/example/dist;
       try_files $uri $uri/ @router; # 配置使用路由
       index  index.html index.htm;
    }
    
   # 路由配置信息
   location @router {
     rewrite ^.*$ /index.html last;
   }
}

学习网站:https://blog.csdn.net/xuexiaoyaani/article/details/80870609#21-%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%BF%85%E9%80%89%E8%A7%84%E5%88%99

6、正则表达式使用
包含五大类,如下:

(1)字符类

(2)数量限定符

(3)位置限定符

(4)特殊符号

  (5) 其他普通字符集及其替换

6.1  字符类


6.2  数量限定符


6.3  位置限定符


6.4 特殊符号


6.5  其他普通字符集及其替换


列子:
 

1.手机号码


得出结论:[3579]  只有第二位数字是3或者5或者7或者9就可以。千万不要认为匹配3479。

2.非零的正整数

3.非零开头的最多带两位小数的数字

4.由数字和26位字母组成的字符串

5.QQ号,从10000开始

6.IP地址

\d+\.\d+\.\d+\.\d+

7.判断账号是否合法

^[a-zA-Z0-9][a-zA-Z0-9_]{4,15}$

8.日期格式

^\d{4}-\d{1,2}-\d{1,2}

9.其他匹配
regex = "(^[a-z]+)([0-9]+)([A-Z]+$)";
和 regex = "([a-z]+)([0-9]+)([A-Z]+)";

匹配a1234A, 都是True.

至此就了解到正则表达式。

7、nginx flag标记有哪些
11111

猜你喜欢

转载自blog.csdn.net/qq_39291929/article/details/109358607