= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。以xx开头
~ 开头表示区分大小写的正则匹配 以xx结尾
~* 开头表示不区分大小写的正则匹配 以xx结尾
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
一些常用的配置
1、普通的(静态的)http服务器
这样如果访问http://localhost 就会默认访问到E盘wwwroot目录下面的index.html,如果一个网站只是静态页面的话,那么就可以通过这种方式来实现部署。
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
root e:wwwroot; //思路:通过/将所有的请求,转发给root处理
index index.html;
}
}
2、反向代理
localhost的时候,就相当于访问localhost:8080了
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port; //思路:通过/,将所有的请求,转发给第3方处理
}
}
既然服务器可以直接HTTP访问,为什么要在中间加上一个反向代理,不是多此一举吗?反向代理有什么作用?
负载均衡、虚拟主机等,都基于反向代理实现,当然反向代理的功能也不仅仅是这些。
3、Redirect(重定向)语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ "^star\.igrow\.cn$" {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}