Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
0-RTT opened this issue Mar 10, 2024 · 1 comment
Open

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

0-RTT opened this issue Mar 10, 2024 · 1 comment

Comments

@0-RTT
Copy link

0-RTT commented Mar 10, 2024

现在使用同样的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 应用的其余部分保持不变
@hunshcn
Copy link
Owner

hunshcn commented Mar 11, 2024

可以提交一个pr。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants