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

Git Push报400 Bad Request缺失Host Header问题求助

解决Git Push时出现400 Bad Request(missing required Host header)的问题

我之前处理过类似的场景,结合你提到的刚把Nginx从GitLab EE中分离这个关键背景,这个错误几乎可以确定是Nginx反向代理配置没有正确传递Host头导致的,下面是一步步的排查和解决方法:

1. 检查并修正Nginx反向代理配置

打开你的GitLab站点对应的Nginx配置文件(通常在/etc/nginx/sites-available/或/etc/nginx/conf.d/目录下),确保配置中包含正确的Host头转发规则:

server {
    listen 443 ssl;
    server_name gitlab.<mysite>.com;

    # 其他SSL相关配置...

    location / {
        proxy_pass http://<gitlab_internal_ip>:<port>; # 这里填GitLab实例的内部地址和端口
        proxy_set_header Host $host; # 这行是关键,必须加上
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
    }
}

重点确认proxy_set_header Host $host;这一行存在,它会把客户端请求的Host头正确传递给后端的GitLab服务,否则GitLab无法识别请求的域名,就会抛出"missing required Host header"错误。

2. 验证GitLab的external_url配置

登录GitLab服务器,打开/etc/gitlab/gitlab.rb配置文件,确认external_url设置和你使用的域名完全一致:

external_url 'https://gitlab.<mysite>.com'

修改后执行以下命令重启GitLab服务:

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

3. 确认Git本地远程仓库配置

再次检查本地Git仓库的remote地址是否正确,执行命令:

git remote -v

如果地址有误,重新设置:

git remote set-url origin https://gitlab.<mysite>.com/<user>/project.git

4. 验证Nginx配置并重启

执行命令检查Nginx配置是否有语法错误:

sudo nginx -t

如果提示test is successful,重启Nginx服务:

sudo systemctl restart nginx

额外排查点

如果以上步骤都完成后还是有问题,可以试试:

  • 清除Git的HTTP缓存:git config --global --unset http.proxy(如果之前设置过代理)
  • 临时关闭SSL验证测试(不推荐长期使用,建议确保证书配置正确):git config --global http.sslVerify false

你遇到的错误信息:
remote: 400 Bad Request: missing required Host header
fatal: unable to access 'https:/...

内容的提问来源于stack exchange,提问作者Slait

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:39:07