一、if语句中的判断条件(nginx)介绍
1、正则表达式匹配:
==:等值比较;
~:与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写;
~*:与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字符大小写;
!~:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时区分字符大小写;
!~*:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时不区分字符大小写;
2、文件及目录匹配判断:
-f, !-f:判断指定的路径是否为存在且为文件;
-d, !-d:判断指定的路径是否为存在且为目录;
-e, !-e:判断指定的路径是否存在,文件或目录均可;
-x, !-x:判断指定路径的文件是否存在且可执行;
- 实例(nginx return 302):
#allow.conf
#判断访问源IP如果不是223.223.205.*和39.*.190.*则返回302
if ($remote_addr !~ ^39\.*\.190\.*|223\.223\.205\.*) {
return 302 https://app.xiongmaocar.com/mzh5/p/at_698eada2633d2b37;
}
- 实例(定义接口json返回)
#json.conf
default_type application/json;
add_header Content-Type 'application/json;charset=utf-8';
##设置单个IP时
#if ($remote_addr != 223.223.205.*) {
##设置多个IP时
if ($remote_addr !~ ^39\.*\.190\.*|223\.223\.205\.*) {
return 200 '{"success":true,"code":500,"message":"系统正在升级,暂时无法访问,敬请谅解。","result":null}';
}
- 实例(设置js.map不允许223.223.205.*以外的IP访问)
location ~ .*\.(js.map)?$ {
set_real_ip_from 223.223.205.*; #定义可以访问的源IP
real_ip_header X-Forwarded-For; #表示从哪个header属性中获取真实IP
real_ip_recursive on; #递归检索真实IP,若从 X-Forwarded-For 中获取,则需递归检索;若像从X-Real-IP中获取,则无需递归。
proxy_set_header X-Forwarded-For $remote_addr; #$remote_addr 是客户端真实IP
#判断访问源IP如果不是223.223.205.*则返回403
##设置单个IP
#if ($remote_addr != 223.223.205.*) {
##设置多个IP
if (¥remote_addr !~ ^223\.223\.205\.*|39\.*\.190\.*) {
return 403;
}
#定义proxy1日志输出路径
access_log /data/nginx/access/$host.proxy.log proxy1;
}
然后vhost调用
location /airadar/api/ {
#引用json.conf配置
include json.conf;
proxy_pass http://autosaas-gateway-pro.****.com:11001/cpapi/;
proxy_redirect http://autosaas-gateway-pro.***.com:11001/cpapi/ http://******/a**r/api/;
client_max_body_size 100m;
}
location /bo***dar/ {
#引用allow.conf
include allow.conf;
index index.html index.htm;
alias /data/wwwroot/****/bo***dar/;
try_files $uri $uri/ /bo***dar/index.html;
if ($request_filename ~ .*\.(htm|html)$)
{
add_header Cache-Control "no-cache, no-store";
}
#引用js_map.conf
include js_map.conf;
}