使用nginx为移动浏览器通过Youtube的代理 目标

使用nginx为移动浏览器通过Youtube的代理 目标,nginx,youtube,Nginx,Youtube,使用nginx代理服务器代理www.youtube.com,修复github.io站点中丢失的CORS头,无论是桌面还是移动环境,无需在移动设备上使用桌面模式 问题 使用桌面浏览器或启用桌面模式的移动浏览器代理可以正常工作 但在移动浏览器中,302被重定向到m.youtube.com,并触发丢失的CORS头,即使删除了请求中所有可疑的头。遗憾的是,4小时的搜索没有产生任何点击率 细节 客户端是静态的,html运行brython。调用代理的brython代码如下所示: 来自urllib导入

使用nginx代理服务器代理
www.youtube.com
,修复github.io站点中丢失的CORS头,无论是桌面还是移动环境,无需在移动设备上使用桌面模式


问题 使用桌面浏览器或启用桌面模式的移动浏览器代理可以正常工作

但在移动浏览器中,302被重定向到
m.youtube.com
,并触发丢失的CORS头,即使删除了请求中所有可疑的头。遗憾的是,4小时的搜索没有产生任何点击率


细节
  • 客户端是静态的,html运行brython。调用代理的brython代码如下所示:
来自urllib导入请求
def get_html(查询:str)->str:
"""
从代理获取HTML
:param query:url不包括youtube域。
:return:html数据的字符串
"""
尝试:
req=request.urlopen(f“[redact]/yt_proxy/{query}”)
除异常作为错误外:
尝试:
req=request.urlopen(f“[redact]/yt_proxy_m/{query}”)
除作为错误的例外情况外:
打印(“致命故障!”)
从err中升起err
html=请求读取()
#若服务器有大脑,那个么它必须是utf8。
尝试:
数据=html解码(“utf8”)
除属性错误外:
#已经解码了!
数据=html_
返回数据
此代码段按原样完成,因此您可以访问任何在线brython交互控制台并复制粘贴,但编辑的域除外。这是我用过的,如果你需要的话

因为我假设这与python没有太大关系,所以我不会在这个问题上添加python标记


Nginx配置已尝试
yt\u proxy
块是
get\u html()
第一次尝试的内容。在移动浏览器上,它被重定向到移动youtube页面,因此得到missiong CORS头

在此之后,
get_html()
函数将尝试
yt_proxy_m
块。此块尝试删除所有头,然后添加假的
user-agent
sec-ch-ua-mobile

不幸的是,它仍然将手机重定向到
m.youtube.com
,我没有选择做什么。这是nginx重定向而不是代理传递时的响应头

事先很抱歉,下面的这些中没有一个有任何工作语法突出显示

server {
        listen 80;
        server_name [REDACTED];
        return 301 https://$server_name:443$request_uri;
}

server {
        listen 443 ssl http2;
        server_name [REDACTED];

        ssl_certificate [REDACTED];
        ssl_certificate_key [REDACTED];

        add_header Strict-Transport-Security "max-age=15552000;includeSubDomains" always;

        location /yt_proxy/ {
                proxy_pass https://www.youtube.com/;

                proxy_redirect off;
                add_header Access-Control-Allow-Origin "*";

        }
        location /yt_proxy_m/ {
                proxy_set_header HEADER "";

                proxy_set_header User-Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
                proxy_set_header Sec-Ch-Ua-Mobile: "?0";

                proxy_pass https://www.youtube.com/;

                proxy_redirect off;
                add_header Access-Control-Allow-Origin "*";
        }
}

请求/响应头
  • 移动(桌面chrome移动仿真)
对于移动设备,第一个请求/响应总是失败,因此跳过它

手机上的第二个请求-我不知道为什么请求已经
m.youtube.com

:authority: m.youtube.com
:method: GET
:path: /channel/[REDACTED]
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6,zh;q=0.5
cache-control: no-cache
origin: null
pragma: no-cache
referer: http://localhost:63342/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
sec-ch-ua-mobile: ?1
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
来自移动设备上nginx服务器的第二次响应-代理传递失败,已重定向

access-control-allow-origin: *
alt-svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-length: 0
content-type: application/binary
date: Sat, 22 May 2021 13:39:08 GMT
expires: Mon, 01 Jan 1990 00:00:00 GMT
location: https://m.youtube.com/channel/[REDACTED]
p3p: CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=ko for more info."
permissions-policy: ch-ua-full-version=*, ch-ua-platform=*, ch-ua-platform-version=*, ch-ua-arch=*, ch-ua-model=*
pragma: no-cache
server: nginx/1.18.0 (Ubuntu)
set-cookie: YSC=XYdUlJNIxRI; Domain=.youtube.com; Path=/; Secure; HttpOnly; SameSite=none
set-cookie: VISITOR_INFO1_LIVE=zf--KazWSeo; Domain=.youtube.com; Expires=Thu, 18-Nov-2021 13:39:08 GMT; Path=/; Secure; HttpOnly; SameSite=none
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 0

桌面上的请求

:authority: [REDACTED]
:method: GET
:path: /yt_proxy/channel/[REDACTED]
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6,zh;q=0.5
cache-control: no-cache
origin: http://localhost:63342
pragma: no-cache
referer: http://localhost:63342/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
sec-ch-ua-mobile: ?0
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
桌面上的nginx服务器响应成功,已传递代理

access-control-allow-origin: *
alt-svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-encoding: br
content-type: text/html; charset=utf-8
date: Sat, 22 May 2021 13:43:55 GMT
expires: Mon, 01 Jan 1990 00:00:00 GMT
p3p: CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=ko for more info."
permissions-policy: ch-ua-full-version=*, ch-ua-platform=*, ch-ua-platform-version=*, ch-ua-arch=*, ch-ua-model=*
pragma: no-cache
server: nginx/1.18.0 (Ubuntu)
set-cookie: GPS=1; Domain=.youtube.com; Expires=Sat, 22-May-2021 14:13:55 GMT; Path=/; Secure; HttpOnly
set-cookie: YSC=R0ZBkiVJusU; Domain=.youtube.com; Path=/; Secure; HttpOnly; SameSite=none
set-cookie: VISITOR_INFO1_LIVE=T-LTouGIuwU; Domain=.youtube.com; Expires=Thu, 18-Nov-2021 13:43:55 GMT; Path=/; Secure; HttpOnly; SameSite=none
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 0