目 录CONTENT

文章目录

nginx使用反向代理下载静态网站

ABin
2025-01-17 / 0 评论 / 0 点赞 / 51 阅读 / 0 字

需求:有一个index.html页面,放在网站根目录,打开网站请求如果遇到404 反向代理到py脚本执行下载然后返回

1.Nginx 配置

server
{
    ......
    
    #Start
    location / {
        try_files $uri $uri/ @backend;  # 如果文件不存在,则转发到 Flask 后端
    }
    
    location @backend {
        proxy_pass http://127.0.0.1:8890/fetch?uri=$request_uri;  # 转发到 Flask 后端
        proxy_set_header Host $host;
        proxy_set_header X-Original-URI $request_uri;
        proxy_intercept_errors off;  # 确保返回后端状态码
    }
    #End

    ......
}

2.python代码

from flask import Flask, request, jsonify, send_file, make_response
import requests
import os
import mimetypes

app = Flask(__name__)

#僵尸亮了
LOCAL_JS_HOST = "js.abin.cc"
LOCAL_JS_PATH = "/www/wwwroot/js.abin.cc"
LOCAL_JS_URL = f"https://file.gugudang.com/res/down/public/p_kapibala/web-mobile/jiangshiliangle"

#老铁活下去
LOCAL_LT_HOST = "lt.abin.cc"
LOCAL_LT_PATH = "/www/wwwroot/lt.abin.cc"
LOCAL_LT_URL = f"https://tos.qwpo2018.com/mp/game/web/gm/1516ddbf7f00965da9a3014f52f7d043"

#致命偷袭
LOCAL_TX_HOST = "tx.abin.cc"
LOCAL_TX_PATH = "/www/wwwroot/tx.abin.cc"
LOCAL_TX_URL = f"https://szj.txhy1699.com/web_bbzombie_bt/241230_xr"


@app.route('/fetch', methods=['GET'])
def fetch():
    uri = request.args.get('uri')  # 获取请求的 URI
    requesturi = request.headers.get('Host', 'unknown')
    if not uri:
        return jsonify({"error": "URI is missing"}), 400
    print("URL:",requesturi)
    # 请求 B 网站的资源
    if requesturi == LOCAL_JS_HOST :
        b_url = LOCAL_JS_URL+uri
        b_local = LOCAL_JS_PATH
    elif requesturi == LOCAL_LT_HOST:
        b_url = LOCAL_LT_URL+uri
        b_local = LOCAL_LT_PATH
    elif requesturi == LOCAL_TX_HOST:
        b_url = LOCAL_TX_URL+uri
        b_local = LOCAL_TX_PATH
    else :
        return jsonify({"error": 'requesturi ERR'}), 500

    try:
        response = requests.get(b_url, timeout=10)
        response.raise_for_status()  # 检查请求是否成功
    except requests.RequestException as e:
        return jsonify({"error": str(e)}), 502

    # 保存文件到本地
    local_path = os.path.join(b_local, uri.lstrip('/'))
    print(local_path)
    #自动创建路径中缺失的中间目录
    os.makedirs(os.path.dirname(local_path), exist_ok=True)

    try:
    # 保存文件内容
        with open(local_path, 'wb') as f:
            f.write(response.content)

        # 根据文件扩展名获取 MIME 类型
        mime_type, _ = mimetypes.guess_type(local_path)
        if mime_type is None:
            mime_type = 'application/octet-stream'  # 默认 MIME 类型

        # 返回文件,显式设置状态码为 200
        response = make_response(send_file(local_path, mimetype=mime_type))
        response.status_code = 200
        return response
    except Exception as e:
        return jsonify({"error": str(e), "request_host": uri}), 500

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8890)

3.运行

python3.10 down.py

4.挂在后台运行 (screen)

1.开启screen

screen

2.输入要运行的命令

python3.10 down.py

3.运行完毕后按 ctrl + A + D 把正在执行的命令放入后台

4.查看任务

screen -list
There is a screen on:
	171827.pts-0.Server	(Detached)
1 Socket in /run/screen/S-root.

4.进入任务

screen -r

0

评论区