gh-proxy icon indicating copy to clipboard operation
gh-proxy copied to clipboard

使用nginx反代,clone仓库提示502

Open 0-RTT opened this issue 11 months ago • 1 comments

现在使用同样的nginx配置,clone时不会提示502

nginx反代配置如下

location / {
    proxy_pass http://127.0.0.1:59740/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Range $http_range;
    proxy_set_header If-Range $http_if_range;
    proxy_set_header Connection '';
}

chatgpt修改main.py之后测试正常

from flask import make_response

# 代理函数中的修改部分
def proxy(u, allow_redirects=False):
    headers = {}
    r_headers = dict(request.headers)
    if 'Host' in r_headers:
        r_headers.pop('Host')
    try:
        url = u + request.url.replace(request.base_url, '', 1)
        if url.startswith('https:/') and not url.startswith('https://'):
            url = 'https://' + url[7:]
        r = requests.request(method=request.method, url=url, data=request.data, headers=r_headers, stream=True, allow_redirects=allow_redirects)
        headers = dict(r.headers)

        if 'Content-length' in r.headers and int(r.headers['Content-length']) > size_limit:
            return redirect(u + request.url.replace(request.base_url, '', 1))

        def generate():
            for chunk in iter_content(r, chunk_size=CHUNK_SIZE):
                yield chunk

        response = Response(generate(), status=r.status_code)
        # 移除可能导致问题的头部
        headers.pop('Transfer-Encoding', None)
        # 添加头部到响应中
        for key, value in headers.items():
            response.headers[key] = value

        if 'Location' in headers:
            _location = headers.get('Location')
            if check_url(_location):
                response.headers['Location'] = '/' + _location
            else:
                # 这是一个递归代理调用,考虑到可能的循环重定向,应当谨慎使用
                return proxy(_location, True)

        return response
    except Exception as e:
        return Response(f'server error {str(e)}', status=500)

# Flask 应用的其余部分保持不变

0-RTT avatar Mar 10 '24 23:03 0-RTT

可以提交一个pr。

hunshcn avatar Mar 11 '24 01:03 hunshcn