Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Allow `/version` to be defined by environment variables (#1015)

Madison Swain-Bowdenc865f8032622
diff --git a/api/Dockerfile b/api/Dockerfile
index 6aeb15128..eff96ba8d 100644
--- a/api/Dockerfile
+++ b/api/Dockerfile
@@ -48,7 +48,9 @@ WORKDIR /app
COPY nginx.conf.template /etc/nginx/templates/openverse-api.conf.template
COPY /static /app/static
-ENV NGINX_ENVSUBST_FILTER="DJANGO_UPSTREAM_URL"
+# Only environment variables with this prefix will be available in the template
+ENV NGINX_ENVSUBST_FILTER="DJANGO_NGINX_"
+ENV DJANGO_NGINX_ENVIRONMENT="local"
#######
# API #
diff --git a/api/nginx.conf.template b/api/nginx.conf.template
index 78b556dfe..bd70cb56d 100644
--- a/api/nginx.conf.template
+++ b/api/nginx.conf.template
@@ -1,19 +1,26 @@
+# The following variables are substituted into the environment using envsubst.
+# This occurs automatically as part of the nginx image startup.
+# See: https://github.com/nginxinc/docker-nginx/blob/456bf337ceb922a207651aa7c6077a316c3e368c/entrypoint/20-envsubst-on-templates.sh#L17
+# - DJANGO_NGINX_UPSTREAM_URL
+# - DJANGO_NGINX_GIT_REVISION
+# - DJANGO_NGINX_ENVIRONMENT
+
error_log /var/log/nginx/error.log;
log_format json_combined escape=json
'{'
- '"time_local":"\$time_local",'
- '"remote_addr":"\$remote_addr",'
- '"remote_user":"\$remote_user",'
- '"request":"\$request",'
- '"status": "\$status",'
- '"host_header": "\$host",'
- '"body_bytes_sent":\$body_bytes_sent,'
- '"request_time":"\$request_time",'
- '"http_referrer":"\$http_referer",'
- '"http_user_agent":"\$http_user_agent",'
- '"upstream_response_time":\$upstream_response_time,'
- '"http_x_forwarded_for":"\$http_x_forwarded_for"'
+ '"time_local":"$time_local",'
+ '"remote_addr":"$remote_addr",'
+ '"remote_user":"$remote_user",'
+ '"request":"$request",'
+ '"status": "$status",'
+ '"host_header": "$host",'
+ '"body_bytes_sent":$body_bytes_sent,'
+ '"request_time":"$request_time",'
+ '"http_referrer":"$http_referer",'
+ '"http_user_agent":"$http_user_agent",'
+ '"upstream_response_time":$upstream_response_time,'
+ '"http_x_forwarded_for":"$http_x_forwarded_for"'
'}';
access_log /var/log/nginx/access.log json_combined;
@@ -31,8 +38,7 @@ gzip_types application/json text/plain application/javascript;
gzip_disable "MSIE [1-6]\.";
upstream django {
- # DJANGO_UPSTREAM_URL must be substituted using a tool like envsubst
- server $DJANGO_UPSTREAM_URL;
+ server $DJANGO_NGINX_UPSTREAM_URL;
}
server {
@@ -51,18 +57,18 @@ server {
}
location / {
- proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto \$scheme;
- proxy_set_header Host \$http_host;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://django;
error_page 500 /500.json;
}
location ~ ^/(v1|admin)/ {
- proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto \$scheme;
- proxy_set_header Host \$http_host;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://django;
error_page 500 /500.json;
@@ -80,6 +86,6 @@ server {
location /version {
default_type "application/json";
- alias /var/api/version.json;
+ return 200 '{"release": "$DJANGO_NGINX_GIT_REVISION", "environment": "$DJANGO_NGINX_ENVIRONMENT"}';
}
}
diff --git a/justfile b/justfile
index f1049e0ce..da5f17644 100644
--- a/justfile
+++ b/justfile
@@ -231,12 +231,23 @@ ipython:
just dj shell
# Run `collectstatic` to prepare for building the `nginx` Dockerfile target.
-@collectstatic: _api-up
+@collectstatic:
# The `STATIC_ROOT` setting is relative to the directory in which the Django
# container runs (i.e., the `api` directory at the root of the repository).
# The resulting output will be at `api/static` and is git ignored for convenience.
@STATIC_ROOT="./static" just dj collectstatic --noinput
+# Run the nginx image locally
+nginx upstream_url='api.openverse.engineering': collectstatic
+ # upstream_url can also be set to 172.17.0.1:50280 for local testing
+ cd api && docker build --target nginx . -t openverse-api-nginx:latest
+ @echo "--> NGINX server will be run at http://localhost:9090, upstream at {{ upstream_url }}"
+ @echo "--> Try a static URL like http://localhost:9090/static/admin/css/base.css to test"
+ docker run --rm -p 9090:8080 -it \
+ -e DJANGO_NGINX_UPSTREAM_URL="{{ upstream_url }}" \
+ -e DJANGO_NGINX_GIT_REVISION="$(git rev-parse HEAD)" \
+ openverse-api-nginx:latest
+
##########
# Sphinx #