V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
ivito
V2EX  ›  NGINX

Nginx 反代 Google 返回 HTTP 301

  •  
  •   ivito ·
    fireboyvt · 2015-07-03 19:52:52 +08:00 · 4184 次点击
    这是一个创建于 3465 天前的主题,其中的信息可能已经有所发展或是发生改变。
    配置如下
    server {
    listen 80;
    server_name g.mydomain.com;
    location / {
    sub_filter www.google.com g.mydomain.com;
    proxy_pass http://g.mydomain.com;
    proxy_set_header X-Real_IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Referer http//$host;
    }
    }
    19 条回复    2015-07-04 15:37:02 +08:00
    cnly1987
        1
    cnly1987  
       2015-07-03 19:55:24 +08:00 via Android
    用ssl吧。
    wico77
        2
    wico77  
       2015-07-03 19:56:16 +08:00
    proxy_pass https://www.google.com;
    proxy_set_header Referer https://www.google.com;
    ivito
        3
    ivito  
    OP
       2015-07-03 20:01:50 +08:00
    @wico77 效果相同,依然返回 301 Moved Permanently (from cache)
    ivito
        4
    ivito  
    OP
       2015-07-03 20:02:15 +08:00
    @cnly1987 ssl很麻烦吧,有永久免费的吗
    wico77
        5
    wico77  
       2015-07-03 20:10:44 +08:00
    要用SSL
    yeyeye
        6
    yeyeye  
       2015-07-03 20:18:54 +08:00
    你没带上cookie当然会跳转
    ltm
        7
    ltm  
       2015-07-03 20:22:18 +08:00 via Android
    @yeyeye 请教怎么带cookie
    Orzzzz
        8
    Orzzzz  
       2015-07-03 22:33:34 +08:00
    ltm
        9
    ltm  
       2015-07-03 22:51:33 +08:00
    @Orzzzz 但是Cookie是哪里来的呢?
    我这样尝试过,用curl请求google,获取cookie,再用curl 带刚获取的cookie请求,google返回错误界面。
    yeyeye
        10
    yeyeye  
       2015-07-03 23:26:34 +08:00
    @ltm 应该可以用本机的吧
    ivito
        11
    ivito  
    OP
       2015-07-03 23:31:23 +08:00
    @ltm server {
    listen 443;
    server_name g.mydomain.com;
    ssl on;
    ssl_certificate /ssl/g.mydomain.com.crt;
    ssl_certificate_key /ssl/g.mydomain.com.key;
    location / {
    proxy_cookie_domain www.google.com $host;
    sub_filter www.google.com $host;
    sub_filter_once off;
    proxy_pass http://google;
    proxy_redirect https://www.google.com/ /;
    proxy_set_header HOST "www.google.com";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    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=047818f19f6de346:U=0f622f33dd8549d11:FF=25:LD=zh-CN:NW=1:TM=1325238577:LM=1332342444:GM=5:SG=1:S=rE01SyJh2w1IQ-Maw";
    }
    }
    参照博客内容配置如上,依然不可以,依然返回301
    Orzzzz
        12
    Orzzzz  
       2015-07-04 00:07:35 +08:00
    @ltm head 里面set的cookie啊,你本地curl的cookie好像有问题,具体我也说不上,那个博客里面的cookie是好的,你可以试一下那里面设置的cookie,语言啊啥的都能调
    Orzzzz
        13
    Orzzzz  
       2015-07-04 00:08:30 +08:00
    @ivito upstream复制全了么- -
    ivito
        14
    ivito  
    OP
       2015-07-04 02:09:48 +08:00 via iPhone
    @Orzzzz 复制全了,肯定能连上服务器,要不也不会返回301
    hiboshi
        15
    hiboshi  
       2015-07-04 08:50:18 +08:00
    log_format g.yh.gs '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" $http_x_forwarded_for';
    server
    {
    listen 443;
    server_name g.yh.gs;
    #rewrite ^/(.*) https://$server_name$1 permanent;
    ssl on;
    ssl_certificate /usr/local/nginx/ssl/1_g.yh.gs_bundle.crt;
    ssl_certificate_key /usr/local/nginx/ssl/2_g.yh.gs.key;
    # ssl_protocols SSLv2 SSLv3 TLSv1;
    # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    # ssl_prefer_server_ciphers on;
    #listen 80;
    location / {
    proxy_redirect off;
    proxy_cookie_domain google.com xxx.xxx;
    proxy_pass https://www.google.co.jp/;
    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";
    }
    }


    server {
    listen 80;
    server_name g.yh.gs;
    rewrite ^(.*) https://$server_name$1 permanent;
    }


    参考我的
    ivito
        16
    ivito  
    OP
       2015-07-04 09:35:46 +08:00
    @Orzzzz
    @hiboshi
    昨天还不行,今天起床一看什么都好了,完全莫名其妙
    Orzzzz
        17
    Orzzzz  
       2015-07-04 13:00:57 +08:00
    @ivito 记得保存配置副本- -
    ivito
        18
    ivito  
    OP
       2015-07-04 13:41:37 +08:00 via iPhone
    @Orzzzz 嗯,做了个备份
    hambut
        19
    hambut  
       2015-07-04 15:37:02 +08:00
    ngx_http_google_filter_module

    google on;
    google_scholar on;
    google_language zh-CN;

    说这么多应该够了,嗯。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2698 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 05:46 · PVG 13:46 · LAX 21:46 · JFK 00:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.