在Ubuntu内网环境中为Gogs配置HTTPS访问(通过Apache反向代理使用IP地址)

一、准备工作

  1. 确保已安装Gogs并运行在HTTP模式(默认端口3000)

  2. 确认服务器内网IP地址(如192.168.1.100)

二、安装Apache和必要模块

复制代码
sudo apt update
sudo apt install apache2 -y
sudo a2enmod ssl proxy proxy_http rewrite headers

三、创建SSL证书

1. 创建证书存储目录

复制代码
sudo mkdir -p /etc/apache2/ssl/gogs
sudo chmod 700 /etc/apache2/ssl

2. 生成自签名证书(使用IP地址)

复制代码
sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \
    -keyout /etc/apache2/ssl/gogs/gogs.key \
    -out /etc/apache2/ssl/gogs/gogs.crt \
    -subj "/CN=192.168.1.100" -addext "subjectAltName=IP:192.168.1.100"

请将192.168.1.100替换为你的实际内网IP地址

四、配置Apache虚拟主机

1. 创建IP访问专用配置文件

复制代码
sudo nano /etc/apache2/sites-available/gogs-ip-ssl.conf

2. 添加以下配置内容

复制代码
<VirtualHost *:80>
    ServerName 192.168.1.100
    Redirect permanent / https://192.168.1.100/
</VirtualHost>

<VirtualHost *:443>
    ServerName 192.168.1.100
    
    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/gogs/gogs.crt
    SSLCertificateKeyFile /etc/apache2/ssl/gogs/gogs.key
    
    # 安全配置
    SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite HIGH:!aNULL:!MD5
    SSLHonorCipherOrder on
    
    # 反向代理配置
    ProxyPreserveHost On
    ProxyRequests off
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    
    # 传递HTTPS信息
    RequestHeader set X-Forwarded-Proto "https"
    
    # 提高上传限制(50MB)
    LimitRequestBody 52428800
    
    # 日志配置
    ErrorLog ${APACHE_LOG_DIR}/gogs-error.log
    CustomLog ${APACHE_LOG_DIR}/gogs-access.log combined
    
    # WebSocket支持
    RewriteEngine on
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteCond %{HTTP:Connection} upgrade [NC]
    RewriteRule ^/?(.*) "ws://localhost:3000/$1" [P,L]
</VirtualHost>

3. 启用配置并重启Apache

复制代码
sudo a2ensite gogs-ip-ssl
sudo systemctl restart apache2

五、配置Gogs

1. 编辑Gogs配置文件

复制代码
sudo nano /home/git/gogs/custom/conf/app.ini

2. 修改以下配置项

复制代码
[server]
DOMAIN           = 192.168.1.100
HTTP_PORT        = 3000
ROOT_URL         = https://192.168.1.100/
PROTOCOL         = http

3. 重启Gogs服务

复制代码
sudo systemctl restart gogs

六、防火墙配置

复制代码
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

七、访问测试

  1. 在浏览器访问 https://192.168.1.100

  2. 首次访问需要接受安全警告(因为是自签名证书)

八、解决IP地址SSL证书警告

方案1:在客户端临时禁用 SSL 验证(仅测试环境)
复制代码
git -c http.sslVerify=false clone https://192.168.28.130/root/project1.git
方案2:在客户端永久禁用 SSL 验证(不推荐生产环境)
复制代码
git config --global http.sslVerify false
方案3:将证书添加到 Git 信任列表(推荐)
  1. 导出证书(在服务器上执行)

sudo cp /etc/apache2/ssl/gogs/gogs.crt /tmp/

sudo chmod 644 /tmp/gogs.crt

  1. 将证书文件gogs.crt分发到客户端机器

  2. 在各客户端操作系统中导入证书为受信任的根证书

九、注意事项

  1. 如果服务器IP变更,需要重新生成证书并更新所有配置

  2. 建议在内网DNS服务器中添加解析记录,方便记忆

  3. 生产环境建议使用域名而非IP地址

十、验证配置

复制代码
# 检查Apache是否监听443端口
sudo netstat -tulnp | grep apache

# 检查Gogs是否运行
sudo systemctl status gogs

# 检查HTTPS访问日志
sudo tail -f /var/log/apache2/gogs-access.log

此配置方案专为内网IP访问设计,无需域名解析,适合纯内网环境使用。

相关推荐
小时前端12 小时前
HTTPS 页面加载 HTTP 脚本被拦?同源代理来救场
前端·https
欧云服务器5 天前
怎么让脚本命令可以同时在centos、debian、ubuntu执行?
ubuntu·centos·debian
智渊AI5 天前
Ubuntu 20.04/22.04 下通过 NVM 安装 Node.js 22(LTS 稳定版)
ubuntu·node.js·vim
The️5 天前
Linux驱动开发之Read_Write函数
linux·运维·服务器·驱动开发·ubuntu·交互
james的分享5 天前
大数据领域核心 SQL 优化框架Apache Calcite介绍
大数据·sql·apache·calcite
莫寒清5 天前
Apache Tika
java·人工智能·spring·apache·知识图谱
再战300年5 天前
Samba在ubuntu上安装部署
linux·运维·ubuntu
qwfys2005 天前
How to install golang 1.26.0 to Ubuntu 24.04
ubuntu·golang·install
赤月奇5 天前
https改为http
数据挖掘·https·ssl
归叶再无青6 天前
web服务安装部署、性能升级等(Apache、Nginx)
运维·前端·nginx·云原生·apache·bash