如何在 Gitlab Omnibus 服务器旁边为其他虚拟主机提供服务? [完整的逐步解决方案]

Posted

技术标签:

【中文标题】如何在 Gitlab Omnibus 服务器旁边为其他虚拟主机提供服务? [完整的逐步解决方案]【英文标题】:How to serve other vhosts next to Gitlab Omnibus server? [Full step-by-step solution] 【发布时间】:2015-10-24 02:48:17 【问题描述】:

我在带有 Omnibus 包的专用 Ubuntu 14.04 服务器版本上安装了 Gitlab CE

现在我想在 gitlab 旁边安装另外三个 虚拟主机

两个是由运行在两个不同ports > 1024 上的non-root user 启动的node.js Web 应用程序,第三个是需要从Web 服务器启动的php Web 应用程序。

有:

8081 (node.js) 上运行的私有凉亭注册表 在8082 (node.js) 上运行的私有 npm 注册表 私人作曲家注册表 (PHP)

但是 Omnibus listen 80 并且似乎既没有使用 Apache2 也没有使用 nginx因此我不能使用它们来服务我的 PHP 应用程序和反向代理我的其他两个节点应用程序

Gitlab Omnibus 对listen 80 使用了哪些服务机制? 我应该如何创建其他三个虚拟主机才能提供以下虚拟主机?

gitlab.mycompany.com (:80) -- 已在使用中 bower.mycompany.com (:80) npm.mycompany.com (:80) packagist.mycompany.com (:80)

【问题讨论】:

难道omnibus没有使用nginx作为web服务器??? 我猜不是因为系统没有安装nginx包... 哦,确实是这样!我现在明白了。查看解决方案的答案。 【参考方案1】:

关于这些

但是 Omnibus 听 80 并且似乎既没有使用 Apache2 也没有使用 Nginx [,因此 ...]

和@stdob 评论:

omnibus 没有使用 nginx 作为 web 服务器吗??? -

我回复了

我猜不是因为系统没有安装nginx包...

事实

来自 Gitlab 官方文档:

默认情况下,omnibus-gitlab 安装 GitLab 和捆绑的 Nginx。

所以是的!

Omnibus 包其实是用 Nginx 的!

但它是捆绑在一起的,解释了为什么它不需要作为主机操作系统的依赖项安装。

所以是的! Nginx 可以而且应该用于为我的 PHP 应用程序提供服务并反向代理我的其他两个节点应用程序。

那么现在

Omnibus-gitlab 允许通过驻留的用户gitlab-www 访问网络服务器 在同名组中。允许外部网络服务器访问 GitLab,外部网络服务器用户需要添加gitlab-www组。

要使用另一个 Web 服务器,如 Apache 或现有的 Nginx 安装,您必须这样做 以下步骤:

通过在/etc/gitlab/gitlab.rb中指定禁用捆绑的Nginx

nginx['enable'] = false
# For GitLab CI, use the following:
ci_nginx['enable'] = false

检查非捆绑网络服务器用户的用户名。默认情况下,omnibus-gitlab 没有外部网络服务器用户的默认设置。 您必须在配置中指定外部网络服务器用户用户名! 例如,假设网络服务器用户是www-data。 在/etc/gitlab/gitlab.rb设置

web_server['external_users'] = ['www-data']

此设置是一个数组,因此您可以指定多个用户添加到 gitlab-www 组。

运行 sudo gitlab-ctl reconfigure 以使更改生效。

设置 NGINX 监听地址或地址

默认情况下,NGINX 将接受所有本地 IPv4 地址上的传入连接。 您可以更改/etc/gitlab/gitlab.rb中的地址列表。

nginx['listen_addresses'] = ["0.0.0.0", "[::]"] # listen on all IPv4 and IPv6 addresses

对于 GitLab CI,使用 ci_nginx['listen_addresses'] 设置。

设置 NGINX 监听端口

默认情况下,NGINX 将侦听external_url 中指定的端口或 隐式使用正确的端口(HTTP 为 80,HTTPS 为 443)。如果你正在跑步 GitLab 在反向代理后面,您可能希望覆盖监听端口 别的东西。例如,使用 8080 端口:

nginx['listen_port'] = 8080

同样,对于 GitLab CI:

ci_nginx['listen_port'] = 8081

支持代理 SSL

默认情况下,如果external_url,NGINX 会自动检测是否使用 SSL 包含https://。如果你在反向代理后面运行 GitLab,你 可能希望将external_url 保留为 HTTPS 地址但与 GitLab NGINX 在内部通过 HTTP。为此,您可以使用禁用 HTTPS listen_https 选项:

nginx['listen_https'] = false

同样,对于 GitLab CI:

ci_nginx['listen_https'] = false

请注意,您可能需要将反向代理配置为转发某些 标头(例如HostX-Forwarded-SslX-Forwarded-ForX-Forwarded-Port)到 GitLab。

您可能会看到不正确的重定向或错误(例如“422 Unprocessable Entity”、 “无法验证 CSRF 令牌真实性”)如果您忘记了这一步。更多 信息,请参阅:

What's the de facto standard for a Reverse Proxy to tell the backend SSL is used? https://wiki.apache.org/couchdb/Nginx_As_a_Reverse_Proxy

要更进一步,您可以关注https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/nginx.md#using-a-non-bundled-web-server的官方文档

配置我们的 gitlab 虚拟主机

安装 Phusion 乘客

我们需要在 OS 中全局安装 ruby​​(gitlab 在omnibus 中运行,捆绑了 ruby​​)

$ sudo apt-get update 
$ sudo apt-get install ruby
$ sudo gem install passenger

用passenger模块重新编译nginx

例如,代替Apache2,nginx 不能即时插入二进制模块。必须为您要添加的每个新插件重新编译它。

Phusion 乘客开发团队努力提供“a bundled nginx version of passenger”:使用乘客插件编译的 nginx 箱。

所以,让我们使用它:

要求:我们需要打开我们的TCP端口11371APT key端口)。

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
$ sudo apt-get install apt-transport-https ca-certificates
创建passenger.list
$ sudo nano /etc/apt/sources.list.d/passenger.list

用这些线

# Ubuntu 14.04
deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main

为您的 ubuntu 版本使用正确的存储库。以 Ubuntu 15.04 为例: deb https://oss-binaries.phusionpassenger.com/apt/passengervivid main

编辑权限:

$ sudo chown root: /etc/apt/sources.list.d/passenger.list
$ sudo chmod 600 /etc/apt/sources.list.d/passenger.list

更新包列表:

$ sudo apt-get update

允许它为unattended-upgrades

$ sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

在文件顶部查找或创建此配置块:

// Automatically upgrade packages from these (origin:archive) pairs
Unattended-Upgrade::Allowed-Origins 

  // you may have some instructions here

;

添加以下内容:

// Automatically upgrade packages from these (origin:archive) pairs
Unattended-Upgrade::Allowed-Origins 

  // you may have some instructions here

  // To check "Origin:" and "Suite:", you could use e.g.:
  // grep "Origin\|Suite" /var/lib/apt/lists/oss-binaries.phusionpassenger.com*
    "Phusion:stable";

;

现在(重新)安装nginx-extrapassenger

$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak_"$(date +%Y-%m-%d_%H:%M)"
$ sudo apt-get install nginx-extras passenger

配置它

取消注释/etc/nginx/nginx.conf 文件中的passenger_rootpassenger_ruby 指令:

$ sudo nano /etc/nginx/nginx.conf

...获得类似的东西:

##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/passenger_free_ruby;

创建 nginx 站点配置(虚拟主机配置)

$ nano /etc/nginx/sites-available/gitlab.conf

server 
  listen *:80;
  server_name gitlab.mycompany.com;
  server_tokens off;
  root /opt/gitlab/embedded/service/gitlab-rails/public;

  client_max_body_size 250m;
  access_log  /var/log/gitlab/nginx/gitlab_access.log;
  error_log   /var/log/gitlab/nginx/gitlab_error.log;

  # Ensure Passenger uses the bundled Ruby version
  passenger_ruby /opt/gitlab/embedded/bin/ruby;

  # Correct the $PATH variable to included packaged executables
  passenger_env_var PATH "/opt/gitlab/bin:/opt/gitlab/embedded/bin:/usr/local/bin:/usr/bin:/bin";

  # Make sure Passenger runs as the correct user and group to
  # prevent permission issues
  passenger_user git;
  passenger_group git;

  # Enable Passenger & keep at least one instance running at all times
  passenger_enabled on;
  passenger_min_instances 1;

  error_page 502 /502.html;

现在我们可以启用它了:

$ sudo ln -s /etc/nginx/sites-available/gitlab.cong /etc/nginx/sites-enabled/

nginx 没有原生的a2ensite 等价物,所以我们使用ln,但如果你愿意,github 上有一个项目: nginx_ensite: nginx_ensite 和 nginx_dissite 用于快速启用和禁用虚拟主机

这是一个 shell (Bash) 脚本,它为 nginx 复制 Debian a2ensite 和 a2dissite,用于在 Apache 2.2/2.4 中启用和禁用站点作为虚拟主机。

完成了:-)。最后重启nginx

$ sudo service nginx restart

有了这个新配置,你可以在 gitlab 旁边运行其他虚拟主机来提供你想要的服务

只需在/etc/nginx/sites-available 中创建新配置。

就我而言,我在同一台主机上以这种方式运行和服务:

gitlab.mycompany.com - 用 ruby​​ 编写的awesome git platform ci.mycompany.com - 用 ruby​​ 编写的gitlab continuous integration server npm.mycompany.com - 一个私有的npm 注册表,用node.js 编写 bower.mycompany.com - 一个私人的bower 注册表,写在node.js packagist.mycompany.com - 一个私有的packagist 用于composer 用php 编写的注册表

例如,为npm.mycompany.com 服务:

为日志创建目录:

$ sudo mkdir -p /var/log/private-npm/nginx/

并填写一个新的虚拟主机配置文件:

$ sudo nano /etc/nginx/sites-available/npm.conf

有了这个配置

server 
  listen *:80;
  server_name npm.mycompany.com

  client_max_body_size 5m;
  access_log  /var/log/private-npm/nginx/npm_access.log;
  error_log   /var/log/private-npm/nginx/npm_error.log;

  location / 
    proxy_pass http://localhost:8082;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  

然后启用它并重新启动它:

$ sudo ln -s /etc/nginx/sites-available/npm.conf /etc/nginx/sites-enabled/
$ sudo service nginx restart

【讨论】:

我无法使用这个commangsudo service nginx restart重新启动gitlab bundle nginx。 您应该在 *** 上创建一个新问题帖子并详细解释您的问题。你有错误信息吗?等等......然后在这里分享永久链接,我会很高兴地帮助你。 我创建了一个问题here。 也谢谢你,@TheF!所以......你也可以投票赞成这个问题,这将有助于获得更多的知名度;-) 查看 dgoo2308 的答案以获得更好的答案。这个解决方案用很多不必要的语言解释了如何禁用捆绑的 nginx 并安装你自己的 - 然后你必须自己管理、更新和配置。【参考方案2】:

由于我不想为 gitlab 更改 nginx 服务器(与其他一些集成),所以最安全的方法是下面的解决方案。

也按照

Gitlab:Ningx =>Inserting custom settings into the NGINX config

编辑你的 gitlab 的 /etc/gitlab/gitlab.rb:

nano /etc/gitlab/gitlab.rb

然后滚动到 nginx['custom_nginx_config'] 并进行如下修改,确保取消注释

# Example: include a directory to scan for additional config files
nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"

创建新的配置目录:

mkdir -p /etc/nginx/conf.d/
nano /etc/nginx/conf.d/new_app.conf

并将内容添加到您的新配置中

# my new app config : /etc/nginx/conf.d/new_app.conf
# set location of new app 
upstream new_app 
  server localhost:1234; # wherever it might be

# set the new app server
server 
  listen *:80;
  server_name new_app.mycompany.com;
  server_tokens off;
  access_log  /var/log/new_app_access.log;
  error_log   /var/log/new_app_error.log;
  proxy_set_header Host      $host;
  proxy_set_header X-Real-IP $remote_addr;
  location /  proxy_pass  http://new_app; 

并重新配置 gitlab 以插入新设置

gitlab-ctl reconfigure

重启nginx

gitlab-ctl restart nginx

查看 nginx 错误日志:

tail -f /var/log/gitlab/nginx/error.log

【讨论】:

很好的说明。但是,我会添加两件事需要修复。 server 和 proxy_pass 行需要一个尾随 ';'并且 proxy_pass 行必须在位置上下文中。喜欢:上游应用程序服务器本地主机:8080; 服务器 听 *:80; server_name app.domain.com; server_tokens 关闭; access_log /var/log/app_access.log;错误日志/var/log/app_error.log; proxy_set_header 主机 $host; proxy_set_header X-Real-IP $remote_addr;位置 / proxy_pass app; 请注意 - proxy_pass 指令属于位置指令 你最好使用最新的master分支:gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/… @Danny 很好的答案。有没有办法在 Gitlab Nginx 上为此类域添加 Let's Encrypt?在此示例中,对于“new_app.mycompany.com”? @Slavik 运行 sudo certbot --nginx 并这样做。也感谢@Danny,因为这让我很头疼

以上是关于如何在 Gitlab Omnibus 服务器旁边为其他虚拟主机提供服务? [完整的逐步解决方案]的主要内容,如果未能解决你的问题,请参考以下文章

使用HTTPS配置gitlab omnibus,无需在AWS上使用nginx证书

GitLab Omnibus 在“等”中缺少“nginx”文件夹

centos6使用Omnibus方法安装gitlab

centos6使用Omnibus方法安装gitlab

GITLAB_OMNIBUS_CONFIG 环境变量不起作用?

gitlab升级gitlab Omnibus CE安装遇到的一些问题