立Flag 学习Ng - Location&Rewrite进阶
- Location&Rewrite进阶
- ng 解决浏览器跨域问题
Location&Rewrite进阶
这里要先明确一个概念:
nginx运行阶段:
- rewrite阶段(location、rewrite)
- access阶段
- content阶段
这里要注意的是 ng
是按照阶段执行的,而不是按照配置文件的代码顺序(就是配置文件配置的顺序)
location = / {
set $a 32;
echo $a;
set $a 64;
echo $a;
}
上面代码中使用 set和echo命令。要知道set是属于access
阶段 echo属于content
阶段。所以这里最后输出的都是64
。
Nginx执行过程
以下面的url为例:
http://127.0.0.1:8081/jimmy/learnNignx.html?a=0
可以拆分为如下内容:
ip/域名+端口+path+param
127.0.0.1 + 8081 + /jimmy/learnNignx.html + a=0
匹配过程:
- 域名(ip)+端口 定位虚拟主机(server)
- path与location部分匹配, path = 匹配path(path1) + 剩余path(path2)
- 当遇到
root
则在目录里找path1+path2路径,遇到alias
则在目录里找path2路径。如果 url是以/
结尾则执行index(ng认为url指向的是一个文件夹) - 当遇到反向代理
proxy_pass
则需要判断 proxy_pass 设置的值- 当 proxy_pass= ip:port 则 转发ip+端口+path1+path2路径
- 当 proxy_pass= ip:port/ 则 转发ip+端口+path2路径
- 当然如果遇到rewirte则根据正则匹配如果命中则执行rewrite指令
root&alias & index 命令
- root指文件根目录,没有指定的时候默认是
ng
目录下的 html/ - alias 玩过数据库的同学大概应该能理解,其实就是将 location匹配的路径进行替换
- index指的是当url传递来的是一个文件夹的情况则默认访问的index
location /a.html {
root html/static/;
}
location /b.html {
root html/static/;
}
location / {
root html/static;
index index.html index.htm;
}
location ^~ /cors {
alias html/cors;
index cors.html;
}
if指令
if (condition) {...}
当表达式只是一个变量时,如果值为空或任何以0开头的字符串都会当做false
直接比较变量和内容时,使用=或!=
~ 正则表达式匹配
~* 不区分大小写的匹配
!~ 区分大小写的不匹配
-f和!-f 用来判断是否存在文件
-d和!-d 用来判断是否存在目录
-e和!-e 用来判断是否存在文件或目录
-x和!-x 用来判断文件是否可执行
###--==-0-==-0-=0=-###
# 如果user_agent包含"MSIE",rewrite请求到/msid/目录下
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
# 如果cookie匹配正则,设置变量$id等于正则引用部分
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
}
# 如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302
if ($request_method = POST) {
return 405;
}
# 限速,$slow可以通过 set 指令设置
if ($slow) {
limit_rate 10k;
}
# 如果请求的文件名不存在,则反向代理到localhost 。这里的break也是停止rewrite检查
if (!-f $request_filename){
break;
proxy_pass http://127.0.0.1;
}
# 如果query string中包含"post=140",永久重定向到example.com
if ($args ~ post=140){
rewrite ^ http://example.com/ permanent;
}
#禁止chrome访问
if ($http_user_agent ~ Chrome) {
return 503;
}
# 防盗链
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.jim.com www.jimmy.com;
if ($invalid_referer) {
return 404;
}
}
常见的全局变量
-
KaTeX parse error: Expected 'EOF', got '#' at position 8: args : #̲这个变量等于请求行中的参数,同query_string
-
$content_length : 请求头中的Content-length字段。
-
$content_type : 请求头中的Content-Type字段。
-
$document_root : 当前请求在root指令中指定的值。
-
$host : 请求主机头字段,否则为服务器名称。
-
$http_user_agent : 客户端agent信息
-
$http_cookie : 客户端cookie信息
-
$limit_rate : 这个变量可以限制连接速率。
-
$request_method : 客户端请求的动作,通常为GET或POST。
-
$remote_addr : 客户端的IP地址。
-
$remote_port : 客户端的端口。
-
$remote_user : 已经经过Auth Basic Module验证的用户名。
-
$request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。
-
$scheme : HTTP方法(如http,https)。
-
$server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
-
$server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。
-
$server_name : 服务器名称。
-
$server_port : 请求到达服务器的端口号。
-
$request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
-
u r i : 不 带 请 求 参 数 的 当 前 U R I , uri : 不带请求参数的当前URI, uri:不带请求参数的当前URI,uri不包含主机名,如”/foo/bar.html”。
-
d o c u m e n t u r i : 与 document_uri : 与 documenturi:与uri相同。
栗子:
http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test2/test.php
这里啰嗦下,ng
是没有 else语句的,只有if语句,且条件为正则表达式,命中则进入,未命中则不进入。
ng 解决浏览器跨域问题
什么是浏览器跨域? TP去知乎看
配置如下:
server {
listen 80;
server_name static.enjoy.com;
#是否允许请求带有验证信息
add_header Access-Control-Allow-Credentials true;
#允许跨域访问的域名,可以是一个域的列表,也可以是通配符*
add_header Access-Control-Allow-Origin *;
#允许脚本访问的返回头
add_header Access-Control-Allow-Headers 'x-requested-with,content-type,Cache-Control,Pragma,Date,x-timestamp';
#允许使用的请求方法,以逗号隔开
add_header Access-Control-Allow-Methods 'POST,GET,OPTIONS,PUT,DELETE';
#允许自定义的头部,以逗号隔开,大小写不敏感
add_header Access-Control-Expose-Headers 'WWW-Authenticate,Server-Authorization';
#P3P支持跨域cookie操作
add_header P3P 'policyref="/w3c/p3p.xml", CP="NOI DSP PSAa OUR BUS IND ONL UNI COM NAV INT LOC"';
location / {
root html/static;
index index.html index.htm;
}
location /cors {
root html/cors;
index cors.html;
}
}