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
Update(forgot to mention I'm running this under Red Hat 6.5, Debian/Ubuntu solution welcome)
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
vndr's above solution would work but on the https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/nginx.md, it said:
Inserting custom settings into the NGINX config
If you need to add custom settings into the NGINX config, for example to include existing server blocks, you can use the following setting.
Example: include a directory to scan for additional config files nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
So let's check your /opt/gitlab/embedded/cookbooks/gitlab/templates/default/nginx.conf.erb to see if it contains: <%= @custom_nginx_config %> (it looks like the current gitlab-7.5.3_omnibus.5.2.1.ci-1.el6.x86_64.rpm doesn't include it)
If not, then just add it above the line include <%= @gitlab_http_config %>; like:
<%= @custom_nginx_config %>
include <%= @gitlab_http_config %>;
Then open the /etc/gitlab/gitlab.rb to add: nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
We can make it simply by just add: include /etc/nginx/conf.d/*.conf; instead <%= @custom_nginx_config %>
Then create normal nginx .conf files in /etc/nginx/conf.d/ and gitlab-ctl reconfigure
As I did not wanted to change the config for gitlab Nginx server nor installing/configuring another Nginx and to make sure gitlab would survive an major update, I came to below solution for the Gitlab Omnibus package.
also as per
Gitlab:Ningx =>Inserting custom settings into the NGINX config
edit the /etc/gitlab/gitlab.rb of your gitlab:
nano /etc/gitlab/gitlab.rb
and sroll to nginx['custom_nginx_config'] and modify as below make sure to uncomment
# Example: include a directory to scan for additional config files
nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
create the new config dir:
mkdir -p /etc/nginx/conf.d/
nano /etc/nginx/conf.d/new_app.conf
and add content to your new config: /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;
}
and reconfigure gitlab to get the new settings inserted
gitlab-ctl reconfigure
to restart nginx after changing your config's or adding more config's in /etc/nginx/conf.d:
gitlab-ctl restart nginx
to check nginx error log:
tail -f /var/log/gitlab/nginx/error.log
and see https://stackoverflow.com/a/39695791/6821811 for redirecting to another application server.
Even through you really can do it, a better practise would be to use upper level separate nginx server to serve both gitlab's nginx and your other custom content. Gitlab's nginx may change it's configuration at any time and it can break your custom content. Also, separate nginx is completely yours for configuring.
Just install those two instances to different ports and proxy gitlab's nginx with upper one. Of cause, it will be an overhead, but completely insignificant one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With