Gradio-Caddy反向代理配置方法 Gradio-Caddy Reverse Proxy Setup Guide

本指南详细介绍了如何使用 Caddy 配置 Gradio 的反向代理,为您的 Gradio 应用提供安全的 HTTPS 访问,同时解决文件上传缓慢或失败等常见问题。
This guide details how to set up a Caddy reverse proxy for Gradio to provide secure HTTPS access to your Gradio app while addressing common issues like slow or failed file uploads.


1. 启动 Gradio 应用

1. Start the Gradio App

确保 Gradio 应用正在本地运行。例如,监听端口 7860:
Make sure your Gradio app is running locally, e.g., on port 7860:

import gradio as gr

def greet(name):
    return f"Hello, {name}!"

app = gr.Interface(fn=greet, inputs="text", outputs="text")
app.launch(server_name="127.0.0.1", server_port=7860)

2. 配置 Caddy

2. Configure Caddy

Caddyfile 示例

Example Caddyfile

假设您的域名为 example.com,Gradio 应用运行在本地的 127.0.0.1:7860
Assuming your domain is example.com, and your Gradio app runs on 127.0.0.1:7860.

编辑 /etc/caddy/Caddyfile
Edit /etc/caddy/Caddyfile:

example.com www.example.com {
    reverse_proxy http://127.0.0.1:7860 {
        header_up Host {host}
        header_up Upgrade {http.request.header.Upgrade}
        header_up Connection {http.request.header.Connection}
        flush_interval -1  # 禁用缓冲 Disable buffering
        transport http {
            versions 1.1
        }
    }

    encode gzip
    tls {
        on_demand
    }
}

配置解析

Explanation of Configuration

  1. reverse_proxy:将 /gradio-demo/* 的请求代理到本地运行的 Gradio 应用。
    reverse_proxy: Proxies /gradio-demo/* requests to the locally running Gradio app.
  2. flush_interval -1:关闭响应缓冲,适用于实时文件上传。
    flush_interval -1: Disables response buffering, suitable for real-time file uploads.
  3. encode gzip:启用 Gzip 压缩,提高静态资源加载速度。
    encode gzip: Enables Gzip compression for faster static asset loading.
  4. tls on_demand:启用 HTTPS,并自动生成证书。
    tls on_demand: Enables HTTPS and automatically generates certificates.

3. 检查 Caddy 配置

3. Validate the Caddy Configuration

验证 Caddyfile 是否正确:
Validate your Caddyfile:

caddy validate --config /etc/caddy/Caddyfile

4. 重启 Caddy 服务

4. Restart the Caddy Service

sudo systemctl reload caddy

访问 https://example.com/gradio-demo/ 查看您的 Gradio 应用是否正常工作。
Visit https://example.com/gradio-demo/ to check if your Gradio app is working.


5. 如果用户使用 Nginx

5. If Using Nginx

如果您更熟悉 Nginx,可以参考 Gradio 官方的 Nginx 配置指南
If you’re more familiar with Nginx, refer to the official Nginx guide.

以下是一个 Nginx 配置示例:
Below is an example Nginx configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    location /gradio-demo/ {
        proxy_pass http://127.0.0.1:7860/;
        proxy_buffering off;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

6. 参考更多资源

6. Additional Resources


通过本指南,您可以轻松地将 Gradio 应用部署到生产环境,享受 HTTPS 的安全保障。
By following this guide, you can easily deploy your Gradio app in a production environment with HTTPS security.

遇到问题可以随时调试日志或者参考相关文档解决。
If issues arise, debug using logs or refer to the relevant documentation for solutions.

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇