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

如何将code-server的Nginx配置转换为Apache2可用配置

code-server Apache2部署配置方案

第一步:启用必要的Apache模块

code-server需要代理、WebSocket代理、SSL、重定向等功能支持,先执行以下命令启用对应模块:

a2enmod proxy proxy_http proxy_wstunnel rewrite ssl
systemctl restart apache2

第二步:编写虚拟主机配置文件

在Apache站点配置目录/etc/apache2/sites-available/下新建配置文件,例如code-server.conf,写入以下内容:

# HTTP 80端口配置:强制跳转到HTTPS
<VirtualHost *:80>
    ServerName 你的实际域名
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [L,R=301]
</VirtualHost>

# HTTPS 443端口配置:代理code-server服务
<VirtualHost *:443>
    ServerName 你的实际域名

    # SSL证书配置,替换为你自己的证书路径
    SSLEngine on
    SSLCertificateFile /path/to/your/cert.pem
    SSLCertificateKeyFile /path/to/your/private.key

    # 基础代理配置
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/

    # WebSocket连接适配(对应原Nginx的Upgrade配置)
    RewriteEngine on
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*) ws://127.0.0.1:8080/$1 [P,L]

    # gzip压缩配置
    SetOutputFilter DEFLATE
    AddOutputFilterByType DEFLATE text/* application/javascript application/json
</VirtualHost>

注意:请将配置中的你的实际域名、证书路径替换为你自己的实际信息。

第三步:生效配置并验证

  • 启用站点配置:a2ensite code-server.conf
  • 检查配置语法是否正确:apache2ctl configtest
  • 语法无报错则重载Apache服务:systemctl reload apache2
  • 确认code-server服务处于运行状态:systemctl start code-server

配置完成后直接访问你的域名即可进入code-server登录界面。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 04:06:04