nginx任意反向代理配置

之前有人分享过php的,但效率有点低。
这个是纯NGINX的任意反向代理

使用访问地址示例:
http://127.0.0.1/https://hostloc.com

我做这个的用处就是在某些不方便使用魔法的时候,下载东西,比如github里面的资源。

使用说明

自定义路径 替换HostLocMJJ 为你自己的路径即可 有两处

更新历史

1:24更新:
支持反代CF github.com
测试:
https://www.php8.ltd/http://news.baidu.com/
https://www.php8.ltd/https://github.com/ElderDrivers/EdXposed

5.4 10:32更新
支持中文维基
301跟随,有301不会再跳出

5.4 15:51更新
支持自定义路径 替换HostLocMJJ 为你自己的路径即可 有两处
修复了一些已知问题

5.4 22:54更新
修复301BUG和一些已知问题。

效果如下图:

 

配置

第一次的配置



server {
        listen        80;
        server_name  localhost; #改成自己的域名
        root   "/home/wwwroot/default"; #改自己的服务器目录
                set $custom_PATH /HostLocMJJ;
        set $custom_host "https://www.baidu.com";
        set $custom_host3 "/";
        set $custom_host5 "/";
        if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
            set $custom_host $2;

            set $custom_host3 $7;
        }

        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 ~* ^\/(.+)\/((http|https)://)((www.)?(\w+(\.)?)+)$) {
            return 301 $custom_PATH/$custom_host/;
        }
        set $custom_host2 "www.baidu.com";


        if ($custom_host ~* ^((http|https)://)((www.)?([\w-]+(\.)?)+)$) {
            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://";
        }
        proxy_hide_header content-security-policy;



        location /HostLocMJJ/ {
            # proxy_connect_timeout 100s;
            # add_header Content-Security-Policy "default-src 'self' https://$host http://$host 'unsafe-inline' 'unsafe-eval' blob: data: ;";
            # 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_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$custom_PATH/$upstream_http_location";
            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_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
            proxy_set_header Accept-Encoding "";
            sub_filter_types text/plain  text/javascript text/xml text/css application/x-javascript application/xml;

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

            sub_filter '"/images' '"images';
            sub_filter ', /images' ', images';
            sub_filter 'integrity' ', kkkk';
            sub_filter '../' '$current$host$custom_PATH/$custom_host/../';
            sub_filter "'http://$custom_host2" "'$current$host$custom_PATH/$custom_host";
            sub_filter "'https://$custom_host2" "'$current$host$custom_PATH/$custom_host";
            sub_filter "\"http://$custom_host2" "\"$current$host$custom_PATH/$custom_host";
            sub_filter "\"https://$custom_host2" "\"$current$host$custom_PATH/$custom_host";
            if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
                proxy_pass $custom_host$7$is_args$query_string;
            }


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

        location / {
            add_header Content-Type "text/plain;charset=utf-8";
            return 200 "这是个正经网站";
        }
}


 

 

第二次的配置

proxy_cache_path /home/wwwroot/www.php8.ltd/cache levels=1:2 keys_zone=my_cache_static:600m max_size=300g inactive=40960m use_temp_path=off;
server
    {
        listen 80;
        #listen [::]:80;
        server_name www.php8.ltd ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/www.php8.ltd;
        proxy_cache my_cache_static;
	return 301 https://$server_name$request_uri;
        access_log  /home/wwwlogs/www.php8.ltd.log;
    }

server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name www.php8.ltd ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/www.php8.ltd;

        ssl_certificate /usr/local/nginx/conf/ssl/www.php8.ltd/fullchain.cer;
        ssl_certificate_key /usr/local/nginx/conf/ssl/www.php8.ltd/www.php8.ltd.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
        set $custom_PATH /HostLocMJJ;
        set $custom_host "https://www.baidu.com";
        set $custom_host3 "/";
        set $custom_host5 "/";
        if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
            set $custom_host $2;

            set $custom_host3 $7;
        }

        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 ~* ^\/(.+)\/((http|https)://)((www.)?(\w+(\.)?)+)$) {
            return 301 $custom_PATH/$custom_host/;
        }
        set $custom_host2 "www.baidu.com";


        if ($custom_host ~* ^((http|https)://)((www.)?([\w-]+(\.)?)+)$) {
            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://";
        }
        proxy_hide_header content-security-policy;



        location /HostLocMJJ/ {
            # proxy_connect_timeout 100s;
            # add_header Content-Security-Policy "default-src 'self' https://$host http://$host 'unsafe-inline' 'unsafe-eval' blob: data: ;";
            # 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_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$custom_PATH/$upstream_http_location";
            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_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
            proxy_set_header Accept-Encoding "";
            sub_filter_types text/plain  text/javascript text/xml text/css application/x-javascript application/xml;

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

            sub_filter '"/images' '"images';
            sub_filter ', /images' ', images';
            sub_filter 'integrity' ', kkkk';
            sub_filter '../' '$current$host$custom_PATH/$custom_host/../';
            sub_filter "'http://$custom_host2" "'$current$host$custom_PATH/$custom_host";
            sub_filter "'https://$custom_host2" "'$current$host$custom_PATH/$custom_host";
            sub_filter "\"http://$custom_host2" "\"$current$host$custom_PATH/$custom_host";
            sub_filter "\"https://$custom_host2" "\"$current$host$custom_PATH/$custom_host";
            if ($request_uri ~* ^\/(.+)\/((http|https)://(www.)?([\w-]+(\.)?)+)(.*?)$) {
                proxy_pass $custom_host$7$is_args$query_string;
            }


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

        location / {
            add_header Content-Type "text/plain;charset=utf-8";
            return 200 "这是个正经网站";
        }

error_log /home/wwwlogs/php8.log;
  access_log  /home/wwwlogs/www.php8.ltd.log;
    }

 

 

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
nginx任意反向代理配置
之前有人分享过php的,但效率有点低。 这个是纯NGINX的任意反向代理 使用访问地址示例: http://127.0.0.1/https://hostloc.com 我做这个的用处就是在某些不……
<<上一篇
下一篇>>
文章目录
关闭
目 录