外网访问nginx代理的(资源宝)内网资源

场景:使用Wi-Fi连接的内网web 服务

构思&准备

  • 需要有双网卡的电脑或服务器一台
  • 连接资源宝发射的Wi-Fi,并获取当前电脑内网IP (192.168.0.144)
  • 已知资源宝Wi-Fi访问地址为固定的:192.168.88.100
  • 本机电脑连接资源宝Wi-Fi则可访问资源宝,用nginx代理则可实现局域网访问本地,间接访问到资源宝web服务地址

nginx配置

server
    {
        listen 80;
        server_name 192.168.0.144;

        location / {
            proxy_pass http://192.168.88.100/;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host:$server_port;
            proxy_cookie_domain 192.168.88.100 192.168.0.144;
            client_max_body_size 10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_read_timeout 90;
            proxy_buffer_size 4k;
            proxy_buffers 4 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
        }
    }

重启nginx后发现该web网站存在回调判断。回调端口为10001,且图片、视频资源的端口为9090

修改思路

  • 无法修改源码,则只能通过ngnix替换

用的的模块

  • http_substitutions_filter_module
    修改后nginx配置
server
    {
        listen 80;
        server_name 192.168.0.144;

        location / {
            proxy_pass http://192.168.88.100/;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host:$server_port;
            proxy_cookie_domain 192.168.88.100 192.168.0.144;
            client_max_body_size 10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_read_timeout 90;
            proxy_buffer_size 4k;
            proxy_buffers 4 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;

            #替换之前 host
            subs_filter '192.168.88.100:9090' '192.168.0.144:9090';
            subs_filter "host + '/dbStatus/'" "'http://192.168.0.144:10001/dbStatus/'";
        }
    }

其他代理配置

server
{
    listen 9090;
    server_name 192.168.0.144;
    location /{
        proxy_pass http://192.168.88.100:9090;
    }
}
server
{
    listen 10001;
    server_name 192.168.0.144;
    location /{
        proxy_pass http://192.168.88.100:10001;
    }

}

你可能感兴趣的:(外网访问nginx代理的(资源宝)内网资源)