You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用Nginx proxy_pass代理Google表格并实现IP限制与路径隐藏?

Nginx配置方案:代理Google表格并实现IP限制+路径隐藏

核心需求实现

  1. 仅允许特定IP访问你的域名
  2. 将https://mydomain/file1/映射到指定的Google表格链接,隐藏真实路径

完整Nginx配置示例

server {
    listen 443 ssl;
    server_name mydomain;

    # SSL证书配置(根据实际情况填写路径)
    ssl_certificate /path/to/your/cert.pem;
    ssl_certificate_key /path/to/your/key.pem;

    # 1. IP访问控制:仅允许以下IP访问
    allow 192.168.1.100; # 替换为需要放行的IP
    allow 203.0.113.5;   # 可添加多个允许IP
    deny all;

    # 2. 路径映射:将/file1/代理到目标Google表格
    location /file1/ {
        # Google服务代理必要配置
        proxy_ssl_server_name on;
        proxy_ssl_name docs.google.com;
        proxy_set_header Host docs.google.com;
        proxy_set_header Referer "https://docs.google.com/";
        proxy_set_header User-Agent $http_user_agent;

        # 代理到具体的Google表格链接,注意末尾斜杠匹配规则
        proxy_pass https://docs.google.com/spreadsheets/d/1Uv4qm0kps******XkXnTJMLNeqJmgM/edit?usp=sharing/;

        # 重定向替换:将Google域名替换为自定义域名
        proxy_redirect https://docs.google.com/spreadsheets/d/1Uv4qm0kps******XkXnTJMLNeqJmgM/edit?usp=sharing/ https://$host/file1/;

        # 关闭压缩以便sub_filter生效
        proxy_set_header Accept-Encoding "";

        # 批量替换页面内的Google链接为自定义路径
        sub_filter_once off;
        sub_filter 'https://docs.google.com/spreadsheets/d/1Uv4qm0kps******XkXnTJMLNeqJmgM' 'https://$host/file1';
        sub_filter 'https://docs.google.com' 'https://$host/file1';

        # 传递真实客户端IP
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

关键配置说明

  • IP访问控制:通过allow指定放行IP,最后deny all拦截其他所有请求,可放在server块全局生效,也可仅在location /file1/内配置,只限制该路径。
  • 路径映射:location /file1/匹配自定义短路径,proxy_pass直接指向目标Google表格的完整链接,末尾斜杠确保Nginx正确处理路径拼接。
  • 头部伪装:设置Host和Referer为Google域名,规避Google的防盗链/反爬机制,避免请求被拦截。
  • 链接替换:sub_filter批量替换页面内的所有Google域名和表格路径,确保页面内的跳转、资源加载都使用隐藏后的自定义路径。
  • 重定向处理:proxy_redirect将Google返回的重定向URL替换为自定义域名路径,防止暴露真实地址。

注意事项

  • 确认Nginx已安装ngx_http_sub_module模块(可通过nginx -V查看编译参数),否则sub_filter指令无法生效。
  • 若Google表格设置了权限,需确保服务器IP或访问用户已被授权访问该表格。

内容的提问来源于stack exchange,提问作者Malus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 12:15:01