Using gitlab's nginx to serve another app

yokodev picture yokodev · Jun 6, 2014 · Viewed 11.9k times · Source

Hello I have installed Gitlab using this
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md#installation

Now I want to use nginx to serve another content other than gitlab application how can I do this

  • Where are the config files that I need to modify
  • How can I point a directory like /var/www so that nginx knows that is the root for another app.

Update(forgot to mention I'm running this under Red Hat 6.5, Debian/Ubuntu solution welcome)

Answer

vndr picture vndr · Jul 31, 2014

Here I am using

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

Gitlab installed from deb package is using chef to provision ngnix, so you have to modify chef recipies and add new vhost template into chef cookbooks directory

You can find all chef cookbooks here: /opt/gitlab/embedded/cookbooks/gitlab/

open /opt/gitlab/embedded/cookbooks/gitlab/recipes/nginx.rb

change:

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

to:

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"),
})

add this to the same file:

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

then in template directory(/opt/gitlab/embedded/cookbooks/gitlab/templates/default), create nginx vhost template file( nginx-examplecom-http.conf.erb) and add this there:

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;
}

you have to set nginx['redirect_http_to_https'] = false in(/etc/gitlab/gitlab.rb):

external_url "https://gitlab.example.com"
gitlab_rails['gitlab_email_from'] = "[email protected]"
gitlab_rails['gitlab_support_email'] = "[email protected]"



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

add include <%= @examplecom_http_config %>; into /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 %>;
}

after all those changes run:

gitlab-ctl reconfigure
gitlab-ctl restart