Nginx实现反代任意网站

Nginx实现反代任意网站:

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
server {
listen 80;
server_name aa.com; #改成自己的域名
root "/www/wwwroot/aa.com"; #改自己的服务器目录

set $custom_host "https://www.baidu.com";
set $custom_host3 "/";
set $custom_host5 "/";
if ($request_uri ~* ^\/((http|https)://(www.)?(\w+(\.)?)+)(.*?)$) {
set $custom_host $1;

set $custom_host3 $6;
}

if ($custom_host3 = ""){
set $custom_host3 "/";
}
if ($custom_host3 = " "){
set $custom_host3 "/";
}
if ($request_uri ~* ^\/((http|https)://(www.)?(\w+(\.)?)+)(.+)\?(.+)$) {
set $custom_host3 $6;
}
set $custom_host2 "www.baidu.com";

if ($request_uri ~* ^\/((http|https)://)((www.)?(\w+(\.)?)+)(.*?)$) {
set $custom_host2 $3;
set $custom_host5 $1;
}
if ($request_uri ~* ^\/((http|https)://)((www.)?(\w+(\.)?)+)$) {
set $custom_host2 $3;
set $custom_host5 $1;
}
set $current "http://";
if ($scheme = https) {
set $current "https://";
}
location / {
rewrite /(.*)$ $custom_host3 break;
proxy_connect_timeout 100s;
# proxy_set_header x-forwarded-for $remote_addr;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set $ishttp "http";
if ($custom_host5 = https://) {
set $ishttp "https";
}
proxy_ssl_server_name on;
proxy_set_header X-Forwarded-Proto $ishttp;
proxy_set_header Host $custom_host2;
proxy_cache_key $host$uri$is_args$args;
proxy_headers_hash_max_size 512;

proxy_headers_hash_bucket_size 128;
proxy_buffer_size 64k;
proxy_buffers 32 64k;
proxy_busy_buffers_size 128k;
proxy_set_header Cookie $http_cookie;
proxy_cache_methods GET;
proxy_cache_methods POST;
proxy_cache_methods HEAD;
proxy_redirect off;
proxy_set_header Referer https://$custom_host2$custom_host3;
proxy_set_header User-Agent $http_user_agent;
# 防止谷歌返回压缩的内容,因为压缩的内容无法替换字符串
proxy_set_header Accept-Encoding "";
#proxy_connect_timeout 15000;
proxy_send_timeout 15000;
proxy_read_timeout 15000;
sub_filter_types *;
proxy_cache_valid 200 304 301 1s;
add_header MJJCDN-Cache "$upstream_cache_status";
# proxy_temp_file_write_size 512000k;
resolver 8.8.8.8; #改为自己想要的DNS
proxy_set_header Accept-Encoding "";
sub_filter_types *;

sub_filter_once off;
proxy_temp_file_write_size 512000k;
sub_filter "<head" '<base href="$current$host/$custom_host/" />\n<head';
sub_filter 'https://' '$current$host/https://';
sub_filter 'http://' '$current$host/https://';
sub_filter "\"//" '"$current$host/https://';
sub_filter '"/' '"$current$host/$custom_host/';
sub_filter "'//" "'$current$host/https://";
sub_filter "'/" "'$current$host/$custom_host/";
sub_filter '"/search' '"search';

sub_filter '"/images' '"images';
sub_filter ', /images' ', images';
sub_filter "http://$custom_host2" "$current$host/$custom_host";
sub_filter "https://$custom_host2" "$current$host/$custom_host";
proxy_pass $custom_host;

# # set $request_uri "qqqq";
# add_header Content-Type "text/plain;charset=utf-8";
# return 200 "Your IP Address:$custom_host2</br>$request_uri";
}

}

这个网站可能有点要注意安全性,建议给网站加个密码点击访问

使用教程:

默认打开反代是百度主页,

1
2
aa.com #默认是百度
aa.com/https://www.google.com #则打开的是Google

引用:

分享一个nginx任意反向代理配置,支持 google、等

GitHub - linwoodpendleton/nginx_proxy_conf: nginx任意反向代理配置(支持github,ip138,google)


Nginx实现反代任意网站
https://shyi.org/posts/35776/
作者
Shyi
发布于
2022年5月4日
更新于
2024年9月7日
许可协议