Skip to content

Commit 1e1c276

Browse files
authored
assets: nginx bypass puma when accessing assets. (#5)
* assets: nginx bypass puma when accessing assets. * testdrive: make asset check dynamic by finding actual asset files. * fix: Add xargs to properly handle asset file basename extraction. * change GITLAB_HOME mode for access, instead of using user group.
1 parent 2878a14 commit 1e1c276

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ FROM sameersbn/gitlab:18.1.1
22

33
# Override files
44
COPY assets/runtime/config/gitlabhq/gitlab.yml ${GITLAB_RUNTIME_DIR}/config/gitlabhq/gitlab.yml
5+
COPY assets/runtime/config/nginx/gitlab ${GITLAB_RUNTIME_DIR}/config/nginx/gitlab
56
COPY assets/runtime/functions ${GITLAB_RUNTIME_DIR}/functions

assets/runtime/config/nginx/gitlab

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
## GitLab
2+
##
3+
## Lines starting with two hashes (##) are comments with information.
4+
## Lines starting with one hash (#) are configuration parameters that can be uncommented.
5+
##
6+
##################################
7+
## CONTRIBUTING ##
8+
##################################
9+
##
10+
## If you change this file in a Merge Request, please also create
11+
## a Merge Request on https://gitlab.com/gitlab-org/omnibus-gitlab/merge_requests
12+
##
13+
###################################
14+
## configuration ##
15+
###################################
16+
##
17+
## See installation.md#using-https for additional HTTPS configuration details.
18+
19+
upstream gitlab-workhorse {
20+
server localhost:8181 fail_timeout=0;
21+
}
22+
23+
map $http_upgrade $connection_upgrade_gitlab {
24+
default upgrade;
25+
'' close;
26+
}
27+
28+
## Obfuscate access_token and private_token in access log
29+
map $request_uri $obfuscated_request_uri {
30+
~(.+\?)(.*&)?(private_token=|access_token=)[^&]*(&.*|$) $1$2$3****$4;
31+
default $request_uri;
32+
}
33+
log_format gitlab_access '$remote_addr - $remote_user [$time_local] '
34+
'"$request_method $obfuscated_request_uri $server_protocol" $status $body_bytes_sent '
35+
'"$http_referer" "$http_user_agent"';
36+
37+
## Normal HTTP host
38+
server {
39+
## Either remove "default_server" from the listen line below,
40+
## or delete the /etc/nginx/sites-enabled/default file. This will cause gitlab
41+
## to be served if you visit any address that your server responds to, eg.
42+
## the ip address of the server (http://x.x.x.x/)n 0.0.0.0:80 default_server;
43+
listen 0.0.0.0:80 default_server;
44+
listen [::]:80 default_server;
45+
server_name {{GITLAB_HOST}}; ## Replace this with something like gitlab.example.com
46+
server_tokens off; ## Don't show the nginx version number, a security best practice
47+
48+
## See app/controllers/application_controller.rb for headers set
49+
50+
## Real IP Module Config
51+
## http://nginx.org/en/docs/http/ngx_http_realip_module.html
52+
real_ip_header X-Real-IP; ## X-Real-IP or X-Forwarded-For or proxy_protocol
53+
real_ip_recursive {{NGINX_REAL_IP_RECURSIVE}}; ## If you enable 'on'
54+
## If you have a trusted IP address, uncomment it and set it
55+
set_real_ip_from {{NGINX_REAL_IP_TRUSTED_ADDRESSES}}; ## Replace this with something like 192.168.1.0/24
56+
57+
add_header X-Accel-Buffering {{NGINX_ACCEL_BUFFERING}};
58+
add_header Strict-Transport-Security "max-age={{NGINX_HSTS_MAXAGE}};";
59+
60+
## Individual nginx logs for this GitLab vhost
61+
access_log {{GITLAB_LOG_DIR}}/nginx/gitlab_access.log gitlab_access;
62+
error_log {{GITLAB_LOG_DIR}}/nginx/gitlab_error.log;
63+
64+
location / {
65+
client_max_body_size 0;
66+
gzip off;
67+
68+
## https://github.com/gitlabhq/gitlabhq/issues/694
69+
## Some requests take more than 30 seconds.
70+
proxy_read_timeout 300;
71+
proxy_connect_timeout 300;
72+
proxy_redirect off;
73+
proxy_buffering {{NGINX_PROXY_BUFFERING}};
74+
75+
proxy_http_version 1.1;
76+
77+
proxy_set_header Host $http_host;
78+
proxy_set_header X-Real-IP $remote_addr;
79+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
80+
proxy_set_header X-Forwarded-Proto {{NGINX_X_FORWARDED_PROTO}};
81+
proxy_set_header Upgrade $http_upgrade;
82+
proxy_set_header Connection $connection_upgrade_gitlab;
83+
84+
proxy_pass http://gitlab-workhorse;
85+
}
86+
87+
error_page 404 /404.html;
88+
error_page 422 /422.html;
89+
error_page 500 /500.html;
90+
error_page 502 /502.html;
91+
error_page 503 /503.html;
92+
location /assets/ {
93+
alias {{GITLAB_INSTALL_DIR}}/public/assets/;
94+
expires max;
95+
add_header Cache-Control public;
96+
}
97+
location ~ ^/(404|422|500|502|503)\.html$ {
98+
root {{GITLAB_INSTALL_DIR}}/public;
99+
internal;
100+
}
101+
102+
{{NGINX_CUSTOM_GITLAB_SERVER_CONFIG}}
103+
}

assets/runtime/functions

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,11 @@ install_configuration_templates() {
19581958
install_template ${GITLAB_USER}: gitaly/config.toml ${GITLAB_GITALY_CONFIG}
19591959
}
19601960

1961+
gitlab_configure_assets_access() {
1962+
# https://github.com/ustclug/docker-gitlab/issues/4
1963+
chmod 755 ${GITLAB_HOME}
1964+
}
1965+
19611966
configure_gitlab() {
19621967
echo "Configuring gitlab..."
19631968
update_template ${GITLAB_CONFIG} \
@@ -2018,6 +2023,7 @@ configure_gitlab() {
20182023
gitlab_configure_sentry
20192024
generate_healthcheck_script
20202025
gitlab_configure_content_security_policy
2026+
gitlab_configure_assets_access
20212027

20222028
# remove stale gitlab.socket
20232029
rm -rf ${GITLAB_INSTALL_DIR}/tmp/sockets/gitlab.socket

testdrive.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ check() {
5151
echo "Error: Failed to find 'example oauth' in gitlab.yml"
5252
return 1
5353
fi
54+
first_asset=$(docker exec "gitlab-${SUFFIX}" bash -c 'ls /home/git/gitlab/public/assets/*.js 2>/dev/null | head -n 1 | xargs -n 1 basename')
55+
assets_location="/assets/$first_asset"
56+
assets_code=$(curl --write-out '%{http_code}' --silent --output /dev/null "$url$assets_location")
57+
if [[ $assets_code -lt 200 || $assets_code -gt 399 ]]; then
58+
echo "Error: Failed to access $url$assets_location (status code: $assets_code)"
59+
return 1
60+
fi
5461
return 0
5562
}
5663

5764
RETRIES="48"
5865
RETRIED=0
5966
WAIT_TIME="5s"
6067

61-
until check || { [[ "$((RETRIED++))" == "${RETRIES}" ]] && exit 1; } ; do
68+
until check || { [[ "$((RETRIED++))" == "${RETRIES}" ]] && exit 1; }; do
6269
sleep "${WAIT_TIME}"
6370
done

0 commit comments

Comments
 (0)