31

您好,我已经使用此
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md#installation安装了 Gitlab

现在我想使用 nginx 来提供除 gitlab 应用程序以外的其他内容我该怎么做

  • 我需要修改的配置文件在哪里
  • 如何指向 /var/www 之类的目录,以便 nginx 知道这是另一个应用程序的根目录。

更新(忘了提我在 Red Hat 6.5 下运行,欢迎使用 Debian/Ubuntu 解决方案)

4

6 回答 6

7

我在这里使用

- gitlab.example.com to serve gitlab.example.com over https.
- example.com over http to serve another content other than gitlab application.

从 deb 包安装的 Gitlab 正在使用 chef 来配置 ngnix,因此您必须修改 chef recipies 并将新的 vhost 模板添加到 chef cookbooks 目录

你可以在这里找到所有的厨师食谱:/opt/gitlab/embedded/cookbooks/gitlab/

打开/opt/gitlab/embedded/cookbooks/gitlab/recipes/nginx.rb

改变:

nginx_vars = node['gitlab']['nginx'].to_hash.merge({
  :gitlab_http_config => File.join(nginx_etc_dir, "gitlab-http.conf"),
})

至:

nginx_vars = node['gitlab']['nginx'].to_hash.merge({
  :gitlab_http_config => File.join(nginx_etc_dir, "gitlab-http.conf"),
  :examplecom_http_config => File.join(nginx_etc_dir, "examplecom-http.conf"),
})

将此添加到同一文件中:

template nginx_vars[:examplecom_http_config] do
  source "nginx-examplecom-http.conf.erb"
  owner "root"
  group "root"
  mode "0644"
  variables(nginx_vars.merge(
    {
      :fqdn => "example.com",
      :port => 80,
    }
  ))
  notifies :restart, 'service[nginx]' if OmnibusHelper.should_notify?("nginx")
end

然后在模板目录(/opt/gitlab/embedded/cookbooks/gitlab/templates/default)中,创建nginx vhost模板文件(nginx-examplecom-http.conf.erb)并在其中添加:

server {
  listen <%= @listen_address %>:<%= @port %>;
  server_name <%= @fqdn %>;
  root /var/www/example.com;

  access_log  <%= @log_directory %>/examplecom_access.log;
  error_log   <%= @log_directory %>/examplecom_error.log;

  location /var/www/example.com {
    # serve static files from defined root folder;.
    # @gitlab is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html;
  }

  error_page 502 /502.html;
}

你必须在(/etc/gitlab/gitlab.rb)中设置nginx['redirect_http_to_https'] = false :

external_url "https://gitlab.example.com"
gitlab_rails['gitlab_email_from'] = "info@example.com"
gitlab_rails['gitlab_support_email'] = "support@example.com"



nginx['redirect_http_to_https'] = false
nginx['ssl_certificate'] = "/etc/gitlab/ssl/ssl-unified.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/ssl.key"


gitlab_rails['gitlab_default_projects_limit'] = 10

添加包括 <%= @examplecom_http_config %>; 进入 /opt/gitlab/embedded/cookbooks/gitlab/templates/default/nginx.conf.erb :

http {
  sendfile <%= @sendfile %>;
  tcp_nopush <%= @tcp_nopush %>;
  tcp_nodelay <%= @tcp_nodelay %>;

  keepalive_timeout <%= @keepalive_timeout %>;

  gzip <%= @gzip %>;
  gzip_http_version <%= @gzip_http_version %>;
  gzip_comp_level <%= @gzip_comp_level %>;
  gzip_proxied <%= @gzip_proxied %>;
  gzip_types <%= @gzip_types.join(' ') %>;

  include /opt/gitlab/embedded/conf/mime.types;

  include <%= @gitlab_http_config %>;
  include <%= @examplecom_http_config %>;
}

在所有这些更改运行之后:

gitlab-ctl reconfigure
gitlab-ctl restart
于 2014-07-31T15:08:30.630 回答
7

vndr 的上述解决方案可以工作,但在https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/nginx.md上,它说:

将自定义设置插入 NGINX 配置

如果您需要将自定义设置添加到 NGINX 配置中,例如包含现有的服务器块,您可以使用以下设置。

示例:包含一个目录以扫描其他配置文件 nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"

所以让我们检查你的 /opt/gitlab/embedded/cookbooks/gitlab/templates/default/nginx.conf.erb 看它是否包含:<%= @custom_nginx_config %> (它看起来像当前的 gitlab-7.5.3_omnibus.5.2 .1.ci-1.el6.x86_64.rpm 不包括它)

如果没有,那么只需将它添加到行上方include <%= @gitlab_http_config %>; 喜欢:

<%= @custom_nginx_config %>
include <%= @gitlab_http_config %>;

然后打开/etc/gitlab/gitlab.rb添加:nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"

我们可以简单地添加:include /etc/nginx/conf.d/*.conf; 而是<%= @custom_nginx_config %>

然后在 /etc/nginx/conf.d/ 和 gitlab-ctl reconfigure 中创建普通的 nginx .conf 文件

于 2014-12-11T18:43:11.970 回答
5

由于我不想更改 gitlab Nginx 服务器的配置,也不想安装/配置另一个 Nginx 并确保 gitlab 能够在重大更新中幸存下来,所以我来到了Gitlab Omnibus 包的以下解决方案。

也按照

Gitlab:Ningx =>将自定义设置插入 NGINX 配置

编辑你的 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

并将内容添加到您的新配置中:/etc/nginx/conf.d/new_app.conf

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;

  root /var/www/html/new_app/;
  index index.html index.htm;

 }

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

gitlab-ctl reconfigure

在更改配置或在 /etc/nginx/conf.d 中添加更多配置后重新启动 nginx:

gitlab-ctl restart nginx

检查 nginx 错误日志:

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

并查看https://stackoverflow.com/a/39695791/6821811以重定向到另一个应用程序服务器。

于 2016-09-26T07:47:52.807 回答
3

即使你真的可以做到,更好的做法是使用上层单独的 nginx 服务器来同时为 gitlab 的 nginx 和你的其他自定义内容提供服务。Gitlab 的 nginx 可能会随时更改其配置,并且可能会破坏您的自定义内容。此外,单独的 nginx 完全由您来配置。

只需将这两个实例安装到不同的端口并用上一个代理 gitlab 的 nginx。当然,这将是一个开销,但完全微不足道。

于 2016-08-16T10:50:59.167 回答
3

我已经尝试了这两种方法,对我有用的一种方法是将一个干净的 NGINX 放在 gitlab 内置的一个之上。从长远来看,它更容易/方便。

根据您的需要,这里有一些必须首先到位 的关键事项:

  • 您的网络/路由器/等的 DNS 设置。(否则这将不起作用,因为这里的配置是基于服务器名称的,)
  • 我的设置是简单的一台服务器,多个站点托管在同一服务器 IP 中,我通过 NGINX 名称过滤器命名应用程序进行过滤。

以下是要遵循的主要步骤,请记住,根据您的需要,这可能意味着更多的调整,这也是Ubuntu Server 14.04

  1. 首先停用主 Nginx(与omnibus 捆绑的那个)编辑/etc/gitlab/gitlab.rb

    nginx['enable'] = false ci_nginx['enable'] = false

  2. 现在您可以免费安装一个干净的NGINX实例。
  3. 关于上一步:有时安装程序不会创建 sites-enabled/sites-available/文件夹,创建它们,并确保将它们包含在/etc/nginx/nginx.conf文件中

    include /etc/nginx/sites-enabled/*.conf;

  4. 在一般的 nginx 工作流程中,您将站点配置包含在sites-available/下,然后当您准备好/高兴时,您链接到sites-enabled/文件夹并重新启动 nginx,以便更改生效
  5. 将您的 Gitlab 配置添加到 Nginx站点可用/文件夹

这是我的conf:

`upstream gitlab-workhorse {
  server unix:/var/opt/gitlab/gitlab-workhorse/socket;
}

## Normal HTTP host
server {
  ## Either remove "default_server" from the listen line below,
  ## or delete the /etc/nginx/sites-enabled/default file. This will cause gitlab
  ## to be served if you visit any address that your server responds to, eg.
  ## the ip address of the server (http://x.x.x.x/)n 0.0.0.0:80 default_server;
  #listen 0.0.0.0:80 default_server;
  listen 0.0.0.0:80 ;
#  listen [::]:80 default_server;
  server_name gitlab.mycompany.com; ## Replace this with something like gitlab.example.com
  server_tokens off; ## Don't show the nginx version number, a security best practice
  root /opt/gitlab/embedded/service/gitlab-rails/public;

  ## See app/controllers/application_controller.rb for headers set

  ## Individual nginx logs for this GitLab vhost
  access_log  /var/log/nginx/gitlab.access.log;
  error_log   /var/log/nginx/gitlab.error.log;

  location / { 
    client_max_body_size 0;
    gzip off;

    ## https://github.com/gitlabhq/gitlabhq/issues/694
    ## Some requests take more than 30 seconds.
    proxy_read_timeout      300;
    proxy_connect_timeout   300;
    proxy_redirect          off;

    proxy_http_version 1.1;

    proxy_set_header    Host                $http_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;

        proxy_pass http://gitlab-workhorse;
      }
    } 

您可以在此处的更多选项中找到配置的更多详细信息

  1. 重启/重新加载 nginx

    sudo service nginx restart

  2. 重新启动 gitlab 综合并检查您的 Gitlab 配置

    sudo gitlab-ctl reconfigure

    sudo gitlab-ctl tail (只是为了检查您的 gitlab 配置中是否有问题)

  3. 在/etc/nginx/sites-available/中添加您需要的额外(任意数量)服务器配置,并最终在高兴/准备好时将链接添加到/etc/nginx/sites-enabled/
    这是另一个应用程序的另一个示例

    upstream app_server {
        server 127.0.0.1:9080 fail_timeout=0;
    }
    server {
        listen 80; 
        server_name jenkins.mycompany.com;    
        access_log            /var/log/nginx/jenkins.access.log;
        location / { 
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            if (!-f $request_filename) {
                proxy_pass http://app_server;
                break;
            }
    
        }   
    }
    
  4. 请记住始终重新启动/重新加载 nginx,以便您看到更改。

    sudo service nginx restart

  5. 检查日志以防出现问题/var/log/nginx/anysites*.log

  6. 请注意,这里我们使用具有不同端口的上游,并且名称(它们存在/真实/已在您公司的域中注册)都指向相同的 IP 地址,这意味着 NIGNX 会找到相同的 IP 地址,但它不会因为上游端口的不同而中断,这非常重要

这就是我的配置现在的工作方式,我对 Gitlab 或任何其他应用程序没有任何问题。
所以希望这会帮助那里的任何人。

于 2016-08-22T21:59:39.743 回答
2

那些“其他内容”在 NGiNX 中用“服务器块”声明。

GitLab 位于/etc/nginx/sites-available/gitlab(根据文档,并在 symlined 中[/etc/nginx/sites-enabled][3])。
您可以在其中添加其他服务器块,类似于这个(您可能必须选择不同的端口号),如此过程所示(针对 Ubuntu 14.04 在此处更新)

server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/example.com/public_html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name example.com;
}

root指令应引用 webapp 的根文件夹(/var/www或更可能是 的子文件夹/var/www)。

该服务器块与任何 GitLab 配置完全不同。

于 2014-06-07T05:14:16.700 回答