生成nginx配置文件,反向代理google
生成nginx配置文件,反向代理google
google的ip地址经常改变,所以要用nslookup去更新ip
#!/bin/env php
<?php
define("conf_path", '/usr/local/nginx/conf/hosts');
$google_ips = __dir__ . "/google_ips.txt"; // 保存ip地址文件
$ngx_cnf = conf_path . "/51open.conf"; // nginx配置文件
exec("nslookup google.com", $out, $ret);
$start = false;
$ips = [];
foreach($out as $line) {
// answer: 之后的是服务器对应ip
if (false !== strpos($line, "answer:")) {
$start = true;
} else if (!$start) {
continue;
}
preg_match("/address: ((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.)
{3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))/i", $line, $matches);
if (!empty($matches) && isset($matches[1])) {
$ips[] = $matches[1];
}
}
// 保存的ip地址
if (file_exists($google_ips)) {
$data = file_get_contents($google_ips);
if ($data) {
$arr = unserialize($data);
if (isset($arr['ips']) && date('ymd', $arr['__logtime'])==date('ymd')) {
$ips = array_merge($ips, $arr['ips']);
$ips = array_unique($ips);
}
}
}
$data = ['__logtime' => time(), 'ips' => $ips];
file_put_contents($google_ips, serialize($data));
$str = '';
foreach ($ips as $ip) {
$str .= sprintf("\n server %s:80 max_fails=3;", $ip);
}
if (!$str) {
exit;
}
$ngx_tpl = <<<eot
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#levels设置目录层次
#keys_zone设置缓存名字和共享内存大小
#inactive在指定时间内没人访问则被删除在这里是1天
#max_size最大缓存空间
proxy_cache_path /data/cache/nginx/51open levels=1:2 keys_zone=51open:20m inactive=1d max_size=2g;
upstream google {%s
}
server {
listen 80;
server_name g.51open.net google.51open.net;
rewrite ^(.*)\$ https://\$host\$1 permanent;
}
server {
listen 443;
server_name g.51open.net google.51open.net;
ssl on;
ssl_certificate /usr/local/nginx/conf/hosts/ssl/g.51open.net.crt;
ssl_certificate_key /usr/local/nginx/conf/hosts/ssl/g.51open.net.key;
location / {
proxy_cache 51open;
proxy_cache_key \$host\$uri\$is_args\$args;
proxy_cache_valid 200 304 301 302 3d; #哪些状态缓存多长时间
proxy_cache_valid any 1d; #其他的缓存多长时间
proxy_redirect https://www.google.com/ /;
proxy_cookie_domain google.com g.51open.net;
proxy_pass http://google;
proxy_set_header host "www.google.com";
proxy_set_header accept-encoding "";
proxy_set_header user-agent \$http_user_agent;
proxy_set_header accept-language "zh-cn";
proxy_set_header cookie "pref=id=047808f19f6de346:u=0f62f33dd8549d11:ff=2:ld=zh-cn:nw=1:
tm=1325338577:lm=1332142444:gm=1:sg=2:s=re0syjh2w1iq-maw";
# --- 关键字替换 start --- #
sub_filter_types text/css text/xml text/javascript; # 过滤类型
sub_filter ssl.gstatic.com g.51open.net; # google会加载ssl.gstatic.com的几个资源和文件
sub_filter www.google.com g.51open.net;
sub_filter_once off;
# --- 关键字替换 end --- #
}
## 反向代理ssl.gstatic.com ##
location /gb {
proxy_cache 51open;
proxy_cache_key \$host\$uri\$is_args\$args;
proxy_cache_valid 200 304 301 302 3d; #哪些状态缓存多长时间
proxy_cache_valid any 1d; #其他的缓存多长时间
proxy_pass http://ssl.gstatic.com/gb/;
proxy_cookie_domain ssl.gstatic.com g.51open.net;
proxy_set_header accept-encoding "";
proxy_set_header user-agent \$http_user_agent;
proxy_set_header accept-language "zh-cn";
}
}
eot;
$content = sprintf($ngx_tpl, $str);
file_put_contents($ngx_cnf, $content);
以上就是生成nginx配置文件,反向代理google的内容。