Home php教程 PHP源码 生成nginx配置文件,反向代理google

生成nginx配置文件,反向代理google

May 23, 2016 pm 04:40 PM

生成nginx配置文件,反向代理google

生成nginx配置文件,反向代理google

google的ip地址经常改变,所以要用nslookup去更新ip

#!/bin/env php
<?php
 
define("CONF_PATH", &#39;/usr/local/nginx/conf/hosts&#39;);
 
$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[&#39;ips&#39;]) && date(&#39;Ymd&#39;, $arr[&#39;__logtime&#39;])==date(&#39;Ymd&#39;)) {
            $ips = array_merge($ips, $arr[&#39;ips&#39;]);
            $ips = array_unique($ips);
        }
    }
}
 
$data = [&#39;__logtime&#39; => time(), &#39;ips&#39; => $ips];
file_put_contents($google_ips, serialize($data));
 
$str = &#39;&#39;;
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);
Copy after login

 以上就是生成nginx配置文件,反向代理google的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Aug 24, 2024 am 06:43 AM

Google recently responded to the performance concerns about the Tensor G4 of the Pixel 9 line. The company said that the SoC wasn't designed to beat benchmarks. Instead, the team focused on making it perform well in the areas where Google wants the c

Google Pixel 9 Pro XL gets tested with desktop mode Google Pixel 9 Pro XL gets tested with desktop mode Aug 29, 2024 pm 01:09 PM

Google has introduced DisplayPort Alternate Mode with the Pixel 8 series, and it's present on the newly launched Pixel 9 lineup. While it's mainly there to let you mirror the smartphone display with a connected screen, you can also use it for desktop

Google Pixel 9 smartphones will not launch with Android 15 despite seven-year update commitment Google Pixel 9 smartphones will not launch with Android 15 despite seven-year update commitment Aug 01, 2024 pm 02:56 PM

The Pixel 9 series is almost here, having been scheduled for an August 13 release. Based on recent rumours, the Pixel 9, Pixel 9 Pro and Pixel 9 Pro XL will mirror the Pixel 8 and Pixel 8 Pro (curr. $749 on Amazon) by starting with 128 GB of storage.

New Google Pixel desktop mode showcased in fresh video as possible Motorola Ready For and Samsung DeX alternative New Google Pixel desktop mode showcased in fresh video as possible Motorola Ready For and Samsung DeX alternative Aug 08, 2024 pm 03:05 PM

A few months have passed since Android Authority demonstrated a new Android desktop mode that Google had hidden away within Android 14 QPR3 Beta 2.1. Arriving hot on the heels of Google adding DisplayPort Alt Mode support for the Pixel 8 and Pixel 8

Google opens AI Test Kitchen & Imagen 3 to most users Google opens AI Test Kitchen & Imagen 3 to most users Sep 12, 2024 pm 12:17 PM

Google's AI Test Kitchen, which includes a suite of AI design tools for users to play with, has now opened up to users in well over 100 countries worldwide. This move marks the first time that many around the world will be able to use Imagen 3, Googl

Google\'s new Chromecast \'TV Streamer\' rumoured to launch with Ethernet and Thread connectivity Google\'s new Chromecast \'TV Streamer\' rumoured to launch with Ethernet and Thread connectivity Aug 01, 2024 am 10:21 AM

Google is roughly a fortnight away from fully revealing new hardware. As usual, countless sources have leaked details about new Pixel devices, whether that be the Pixel Watch 3, Pixel Buds Pro 2 or Pixel 9 smartphones. It also seems that the company

Google Pixel 9 Pro XL ranks 2nd in DxOMark\'s \'Global\' smartphone camera ranking Google Pixel 9 Pro XL ranks 2nd in DxOMark\'s \'Global\' smartphone camera ranking Aug 23, 2024 am 06:42 AM

Google's new Pixel 9 series has introduced a new variant that wasn't present in previous lineups, the Pixel 9 Pro XL. It is essentially identical to the non-XL variant (pre-order at Amazon), but as the name suggests, it has a larger screen. The two e

Google confirms Pixel 9 Pro Fold incompatibility with official wireless charging accessories thanks to curious design Google confirms Pixel 9 Pro Fold incompatibility with official wireless charging accessories thanks to curious design Sep 01, 2024 am 06:31 AM

The Pixel 9 Pro Fold is akin to a volte-face for Google's nascent foldable division. In fact, such are the differences between thePixel 9 Pro Foldand the Pixel Fold that the former has been compared more against the OnePlus Open (curr. $1,399.99 on A

See all articles