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

server {
        listen        80;
        server_name  localhost; #改成自己的域名
        root   "/home/wwwroot/default"; #改自己的服务器目录
        set $custom_PATH /HostLocMJJ;
        set $custom_host "pass";
        set $custom_host3 "/";
        set $custom_host5 "/";
        set $unopengzip 0; #对于强制开启压缩的网站开启替换 0 关, 1 开
        if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
            set $custom_host $2;
            set $custom_host3 $7;
        }


        #允许域名 域名白名单
        set $banDomain 1;
        if ($custom_host = "https://hostloc.com") {
            set $banDomain 0;
        }

        if ($custom_host = "https://www.baidu.com") {
            set $banDomain 0;
        }

        if ($custom_host = "https://github.com") {
            set $banDomain 0;
        }

        if ($custom_host ~* "googleusercontent") {
            set $banDomain 0;
        }
        if ($custom_host ~* "drive.google.com") {
            set $banDomain 0;
        }
        if ($custom_host ~* "docs.google.com") {
            set $banDomain 0;
        }



        set $port "";

        if ($server_port !~ (443|80)$) {
            set $port $server_port;
        }


        if ($custom_host3 = ""){
            set $custom_host3 "/";
        }
        if ($custom_host3 = " "){
            set $custom_host3 "/";
        }
        if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.+)\?(.+)$) {
            set $custom_host3 $7;
        }
        if ($request_uri ~* ^\/(.+)\/((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*)$) {
            return 301 $custom_PATH/$custom_host/;
        }
        set $custom_host2 "www.baidu.com";
        set $custom_host8 "www.baidu.com";
        if ($custom_host ~* ^((http|https)://)((www.)?([\w-]+(\.)?)+)$) {
            set $custom_host8 $3;
            set $custom_host2 $3;
            set $custom_host5 $1;
        }
        set $referer $custom_host;
        if ($http_referer ~* ^((http|https)://)(.*?)\/(.+)\/((http|https)://)(.*?)$) {
            set $referer $5$7;
        }
        set $current "http://";
        if ($scheme = https) {
            set $current "https://";
        }
        set $referer2 "";
        if ($referer ~* ^((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
            set $referer2 $1;
        }
        set $root_domain ".$myroot_domain";
        set $myroot_domain ".$myroot_domain";
        if ($referer2 ~* (?:\w+\.)*?(\w*\.(.+))[\\\/]*) {
            set $root_domain $2;
        }
        if ($host ~* (?:\w+\.)*?(\w*\.(.+))[\\\/]*) {
            set $myroot_domain $2;
        }

        if ($root_domain = ".$myroot_domain"){
            set $root_domain "${custom_host2}";
        }
        if ($host ~* "(((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?))") {
            set $myroot_domain $host;
        }
        add_header X-Frame-Options $myroot_domain;
        proxy_hide_header X-Frame-Options;
        proxy_hide_header content-security-policy;


        # location /redirect/ {
        #     include enable-php-pathinfo.conf;

        # }
        location /HostLocMJJ/ {
            if ($banDomain = 1) {
                return 404  ;
            }
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }
            if ($request_method = 'POST') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            }
            if ($request_method = 'GET') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            }
            proxy_set_header x-forwarded-for "";
            proxy_set_header X-Real-IP "";
            proxy_set_header X-Forwarded-For "";
            proxy_set_header cf-connecting-ip "";
            proxy_set_header True-Client-IP "";
            set $ishttp "http";
            if ($custom_host5 = https://) {
                set $ishttp "https";
            }
            proxy_ssl_server_name on;
            proxy_set_header X-Forwarded-Proto $ishttp;
            add_header Proxy-Cache2 $root_domain;


            if ($unopengzip = 1) {
                set $custom_host2 $host;
            }
            proxy_set_header Host $custom_host2;
            proxy_cache_key    $host$uri$is_args$args;
            proxy_headers_hash_max_size 512;
            proxy_pass_header Server;
            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 $upstream_http_location "$current$host:$port$custom_PATH/$upstream_http_location";
            if ($unopengzip = 1) {
                set $referer $http_referer;
            }
            proxy_set_header  Referer           $referer;
            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;
            proxy_set_header Upgrade   $http_upgrade;
            proxy_cookie_domain $root_domain $myroot_domain;
            proxy_cookie_domain $custom_host8 $myroot_domain;
            proxy_cookie_path ~\/(.*) $custom_PATH/$custom_host/$1;
            proxy_max_temp_file_size 10m;
            proxy_set_header Connection "upgrade";
            proxy_cache_valid 200 304 301 1s;
            add_header MJJCDN-Cache "$upstream_cache_status";
            # proxy_temp_file_write_size 512000k;
            resolver 1.1.1.1; #改为自己想要的DNS
            sub_filter_types text/plain  text/javascript text/xml text/css application/x-javascript application/xml application/javascript application/json;

            sub_filter_once off;
            proxy_temp_file_write_size 512000k;
            sub_filter '".$root_domain' '".$myroot_domain';
            sub_filter '"$root_domain' '"$myroot_domain';
            sub_filter "url(http" "url($current$host:$port$custom_PATH/http";
            set $head  '<base href=\'$current$host:$port$custom_PATH/$custom_host/\' /><head';
            set $html2 'kkkkkkk';
            set $html3 'kkkkkkk';
            if ($custom_host = "https://www.gstatic.com") {
                set $head  '<base href=\"$current$host:$port$custom_PATH/$custom_host/\" /><head';
                set $html2 "M(\"<html>";
                set $html3  "<head>\"";
            }
            if ($referer ~ "www\.google\.com") {
                set $head  '<base href=\"$current$host:$port$custom_PATH/$custom_host/\" /><head';
            }
            if ($request_uri ~ "accounts\.google\.com") {
                set $head  '<base href=\"$current$host:$port$custom_PATH/$custom_host/\" /><head';
            }
            if ($request_uri ~ "www\.google\.com") {
                set $head  '<base href=\"$current$host:$port$custom_PATH/$custom_host/\" /><head';
            }
            sub_filter  $html2 "M('<html>";
            sub_filter  $html3 "<head>\'";
            sub_filter "<head" $head;
            sub_filter '\'https://' '\'$current$host:$port$custom_PATH/https://';
            sub_filter '\'http://' '\'$current$host:$port$custom_PATH/https://';
            sub_filter '\"https://' '\"$current$host:$port$custom_PATH/https://';
            sub_filter '\"http://' '\"$current$host:$port$custom_PATH/https://';
            sub_filter "src=\"//" 'src="$current$host:$port$custom_PATH/https://';
            sub_filter 'src="/' 'src="$current$host:$port$custom_PATH/$custom_host/';
            sub_filter "src='//" "src='$current$host:$port$custom_PATH/https://";
            sub_filter "src='/" "src='$current$host:$port$custom_PATH/$custom_host/";
            sub_filter "href=\"//" 'href="$current$host:$port$custom_PATH/https://';
            sub_filter 'href="/' 'href="$current$host:$port$custom_PATH/$custom_host/';
            sub_filter "href='//" "href='$current$host:$port$custom_PATH/https://";
            sub_filter 'action="/' 'action="$current$host:$port$custom_PATH/$custom_host/';
            sub_filter "href='/" "href='$current$host:$port$custom_PATH/$custom_host/";
            sub_filter "\"//" "\"$current$host:$port$custom_PATH/https://";
            sub_filter "\'//" "\'$current$host:$port$custom_PATH/https://";

            sub_filter '"/search' '"search';
            sub_filter '/youtubei/v1/' 'youtubei/v1/';
            sub_filter ':\"/' ':\"$current$host:$port$custom_PATH/$custom_host/';
            sub_filter "https:\\/\\/accounts.google.com" "$current$host:$port$custom_PATH/https:\\/\\/accounts.google.com";


            sub_filter '"/images' '"images';
            sub_filter ', /images' ', images';
            sub_filter 'integrity' ', kkkk';

            sub_filter '../' '$current$host:$port$custom_PATH/$custom_host/../';
            sub_filter "'http://$custom_host2" "'$current$host:$port$custom_PATH/$custom_host";
            sub_filter "'https://$custom_host2" "'$current$host:$port$custom_PATH/$custom_host";
            sub_filter "\"http://$custom_host2" "\"$current$host:$port$custom_PATH/$custom_host";
            sub_filter "\"https://$custom_host2" "\"$current$host:$port$custom_PATH/$custom_host";
            set $myurl1 "";
            set $myurl2 "";
            # add_header Proxy-Cache2 "1";


            if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
                set $myurl2 "${current}127.0.0.1${custom_PATH}/${custom_PATH}/${custom_host}";
            }
            if ($unopengzip = 0) {
                set  $myurl1 $custom_host;
            }

            if ($unopengzip = 1) {
                set  $myurl1 $myurl2;
            }
            if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
                proxy_pass $myurl1$7;
            }


        }
        location =/generate_204 {
            return 204;
        }
        location /HostLocMJJ/HostLocMJJ/ {
            if ($banDomain = 1) {
                return 404  ;
            }
            proxy_set_header x-forwarded-for "";
            proxy_set_header X-Real-IP "";
            proxy_set_header X-Forwarded-For "";
            proxy_set_header cf-connecting-ip "";
            proxy_set_header True-Client-IP "";
            set $ishttp "http";
            if ($custom_host5 = https://) {
                set $ishttp "https";
            }
            gzip on;
            gzip_http_version 1.0;
            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_pass_header Server;
            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           $referer;
            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;
            proxy_max_temp_file_size 10m;
            proxy_set_header Upgrade   $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_cache_valid 200 304 301 1s;
            add_header MJJCDN-Cache "$upstream_cache_status";
            # add_header Proxy-Cache "1";
            # proxy_temp_file_write_size 512000k;
            resolver 1.1.1.1; #改为自己想要的DNS

            proxy_temp_file_write_size 512000k;
            if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
                proxy_pass $custom_host$7;
            }




        }
        location / {
                set $custom www.qq.com;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto https;
                proxy_set_header Host $custom ;
                proxy_temp_file_write_size 512k;

                proxy_cache_key    $host$uri$is_args$args$http_accept_language;
                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";
                proxy_connect_timeout      240;
                proxy_send_timeout         240;
                proxy_read_timeout         240;
                proxy_cache_valid 200 304 365d;
                # proxy_redirect ~^http://([^:]+)(:\d+)?(.*)$ https://www.php8.ltd$3;
                sub_filter_types text/plain  text/javascript text/xml text/css application/x-javascript application/xml;
                sub_filter_once off;
                sub_filter $custom $host;
                proxy_redirect ~^https://$custom/(.*)   https://$custom$1;

                resolver 1.1.1.1; #改为自己想要的DNS
                # proxy_cache my_cache_static;
                proxy_pass https://$custom;

        }

}
Last modification:November 15, 2023
如果觉得我的文章对你有用,请随意赞赏