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

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

May 23, 2016 pm 04:40 PM

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

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

#!/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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles