首页 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);
登录后复制

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

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

linux怎么查看nginx是否启动 linux怎么查看nginx是否启动 Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

nginx在windows中怎么配置 nginx在windows中怎么配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

怎么查看nginx是否启动 怎么查看nginx是否启动 Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

linux怎么启动nginx linux怎么启动nginx Apr 14, 2025 pm 12:51 PM

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

nginx403怎么解决 nginx403怎么解决 Apr 14, 2025 am 10:33 AM

如何解决 Nginx 403 Forbidden 错误?检查文件或目录权限;2. 检查 .htaccess 文件;3. 检查 Nginx 配置文件;4. 重启 Nginx。其他可能原因还包括防火墙规则、SELinux 设置或应用程序问题。

nginx403错误怎么解决 nginx403错误怎么解决 Apr 14, 2025 pm 12:54 PM

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

怎么启动nginx服务器 怎么启动nginx服务器 Apr 14, 2025 pm 12:27 PM

启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP

nginx304错误怎么解决 nginx304错误怎么解决 Apr 14, 2025 pm 12:45 PM

问题的答案:304 Not Modified 错误表示浏览器已缓存客户端请求的最新资源版本。解决方案:1. 清除浏览器缓存;2. 禁用浏览器缓存;3. 配置 Nginx 允许客户端缓存;4. 检查文件权限;5. 检查文件哈希;6. 禁用 CDN 或反向代理缓存;7. 重启 Nginx。

See all articles