首先Nginx获取到时间
选取ngx_time_var模块
安装:
./configure –add-module=/path/to/ngx_http_time_var_module
echo 'year :$tm_year'; //year :2012
echo 'month :$tm_month'; //month :01
echo 'day :$tm_day'; //day :08
echo 'hour :$tm_hour'; //hour :03
echo 'minute:$tm_minute'; //minute:54
echo 'second:$tm_second'; //second:21
echo '';
echo $tm_err_log_time; //2012/01/08 03:54:21
echo $tm_http_time; //Sat, 07 Jan 2012 19:54:21 GMT
echo $tm_http_log_time; //08/Jan/2012:03:54:21 +0800
echo $tm_http_log_iso8601; //2012-01-08T03:54:21+08:00
echo '';
echo $tm_tomsec; //1325966061.521
echo $tm_tosec; 1325966061
server {
server_name www.demo.com;
listen 80;
location / {
# 配置默认ip
set $default_ip 127.0.0.1;
# 配置默认port
set $default_port 8080;
# 如果当前时间是3-5点则指定ip为10.1.1.1
if ($tm_month>=3 && $tm_month <= 5) {
set $default_ip 10.1.1.1
}
# 如果url中包含ip=x
if ($request_uri ~* ip=(.+?)(&|$)) {
set $default_ip $1;
}
# 如果url中包含port=1
if ($request_uri ~* port=(\d+?)(&|$)) {
set $default_port $1;
}
# 配置代理参数
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# 代理
proxy_pass http://$default_ip:$default_port;
}
}