You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地Gitea服务器部署Flutter/Dart包:Pub令牌与HTTP兼容问题求助

解决本地Gitea作为Pub私有仓库的HTTPS/HTTP兼容问题

方案一:为本地Gitea配置SSL证书(推荐)

本地环境可使用自签名证书实现HTTPS,同时让Pub信任该证书,从根源解决令牌和发布的兼容问题:

  • 生成自签名SSL证书
    执行以下openssl命令生成证书文件(替换路径为实际存储位置):

    openssl req -x509 -newkey rsa:4096 -keyout /path/to/git.ourname.local.key -out /path/to/git.ourname.local.crt -days 365 -nodes -subj "/CN=git.ourname.local"
    
  • 配置Gitea启用HTTPS
    打开Gitea的配置文件(通常位于custom/conf/app.ini),修改[server]段的配置:

    [server]
    PROTOCOL = https
    HTTP_PORT = 443
    CERT_FILE = /path/to/git.ourname.local.crt
    KEY_FILE = /path/to/git.ourname.local.key
    ROOT_URL = https://git.ourname.local/
    

    保存后重启Gitea服务。

  • 信任自签名证书
    由于是自签名证书,需要让系统和Pub信任它:

    • Windows:双击.crt文件,选择“安装证书”,将其导入到“本地计算机”的“受信任的根证书颁发机构”
    • macOS:打开钥匙串访问,导入.crt文件,右键点击证书选择“显示简介”,将“使用此证书时”设置为“始终信任”
    • Linux:将.crt文件复制到/usr/local/share/ca-certificates/,执行sudo update-ca-certificates更新证书缓存

    完成后,即可正常添加令牌并发布Pub包。

方案二:让Pub接受HTTP地址(仅临时测试用)

Pub默认强制HTTPS,可通过配置绕过该限制,但会降低安全性,仅适合完全可控的本地环境:

  • 临时生效方式
    在执行发布命令前,设置环境变量允许指定HTTP主机:

    # Linux/macOS终端
    export PUB_ALLOW_UNSECURE_HOSTS=git.ourname.local
    # Windows CMD
    set PUB_ALLOW_UNSECURE_HOSTS=git.ourname.local
    # Windows PowerShell
    $env:PUB_ALLOW_UNSECURE_HOSTS = "git.ourname.local"
    

    随后执行发布命令:

    dart pub publish --server=http://git.ourname.local/api/packages/{你的用户名}/pub
    
  • 永久生效方式
    编辑Pub的全局配置文件~/.pub-cache/config.json(Windows路径为%APPDATA%\Pub\Cache\config.json),添加允许的主机:

    {
      "allow_unsecure_hosts": ["git.ourname.local"]
    }
    

方案三:使用反向代理提供HTTPS(灵活适配场景)

通过Nginx等反向代理工具处理HTTPS请求,后端Gitea保持HTTP运行,兼顾兼容性和配置灵活性:

  • 配置Nginx反向代理
    创建或修改Nginx配置文件,添加以下内容(替换端口和证书路径为实际值):

    server {
        listen 443 ssl;
        server_name git.ourname.local;
    
        ssl_certificate /path/to/git.ourname.local.crt;
        ssl_certificate_key /path/to/git.ourname.local.key;
    
        location / {
            proxy_pass http://localhost:3000; # 替换为Gitea实际运行的HTTP端口
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    
  • 调整Gitea配置
    修改Gitea的app.ini,确保后端使用HTTP,并设置正确的外部访问地址:

    [server]
    PROTOCOL = http
    HTTP_PORT = 3000
    ROOT_URL = https://git.ourname.local/
    

    重启Nginx和Gitea服务后,即可通过HTTPS访问Gitea,正常添加令牌和发布包。

内容的提问来源于stack exchange,提问作者F.M.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 20:09:30