-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add frontend nginx container for logging and plausible proxying (#3108)
* Add nginx proxy for frontend * Forward plausible requests with Nginx, removing this responsibility from Nuxt * Build and publish frontend_nginx from CI * Fix analytics playwright tracking * Fix iteration * Fix docs build * Update frontend/nginx.conf.template
- Loading branch information
1 parent
b299c85
commit f78f2e2
Showing
11 changed files
with
189 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ playwright_tests | |
storybook_tests | ||
miscellaneous | ||
healthcheck | ||
nginx | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Frontend reverse proxy | ||
|
||
The frontend service relies on an Nginx reverse proxy in live environments to | ||
facilitate per-request logging in a format that matches other services we run | ||
(namely, Django). It also acts as a [proxy for Plausible][plausible_proxy], | ||
removing the need for Nuxt to handle these requests, freeing it up to handle | ||
rendering SSR requests. | ||
|
||
[plausible_proxy]: https://plausible.io/docs/proxy/guides/nginx | ||
|
||
Additionally, the reverse proxy may be used in the future for the following: | ||
|
||
- Serving static Nuxt content, freeing Nuxt from serving these requests. This is | ||
considered relatively low value due to Cloudflare caching already handling 99% | ||
of the benefit this would bring for the vast majority of cases. | ||
|
||
## Testing | ||
|
||
To test the frontend reverse proxy locally, run `just frontend/up`. To test the | ||
integration with your local Plausible container, follow the existing | ||
instructions in [the frontend analytics documentation][analytics_docs]. | ||
Everything should "just work". | ||
|
||
[analytics_docs]: /frontend/guides/analytics.md#plausible-set-up-and-first-run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Keeping this as a separate Dockerfile prevents the need to rely on additional | ||
# contexts required for the frontend build (namely, `repo_root`). These are | ||
# possible with compose as of https://github.com/docker/compose/pull/10369 | ||
# and do appear to work, but it increases the complexity and these features | ||
# are not yet all available in every containerd implementation or distribution | ||
# (Debian, for example, still distributes an older version of compose without | ||
# this new feature). For the sake of keeping the local setup simpler and not | ||
# requiring contributors to have bleeding-edge Docker and compose plugin versions, | ||
# we can simply use a separate file for the time being. Once/if we start to use | ||
# nginx to serve static files for the frontend, we will need to integrate this | ||
# target into the shared Dockerfile. | ||
######### | ||
# NGINX # | ||
######### | ||
|
||
FROM docker.io/nginx:1.25.2-alpine as nginx | ||
|
||
LABEL org.opencontainers.image.source = "https://github.com/WordPress/openverse" | ||
|
||
WORKDIR /app | ||
|
||
COPY nginx.conf.template /etc/nginx/templates/openverse-frontend.conf.template | ||
|
||
# Only environment variables with this prefix will be available in the template | ||
ENV NGINX_ENVSUBST_FILTER="OPENVERSE_NGINX_" | ||
ENV OPENVERSE_NGINX_ENVIRONMENT="local" | ||
# Add the release version to the docker container | ||
ARG SEMANTIC_VERSION | ||
ENV OPENVERSE_NGINX_GIT_REVISION=$SEMANTIC_VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# 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 | ||
# - OPENVERSE_NGINX_UPSTREAM_URL | ||
# - OPENVERSE_NGINX_GIT_REVISION | ||
# - OPENVERSE_NGINX_ENVIRONMENT | ||
# - OPENVERSE_NGINX_PLAUSIBLE_EVENT_URL | ||
|
||
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"' | ||
'}'; | ||
|
||
access_log /var/log/nginx/access.log json_combined; | ||
|
||
tcp_nopush on; | ||
tcp_nodelay on; | ||
types_hash_max_size 2048; | ||
|
||
# Compress large responses to save bandwidth and improve latency | ||
gzip on; | ||
gzip_min_length 860; | ||
gzip_vary on; | ||
gzip_proxied expired private auth; | ||
gzip_types application/json text/plain application/javascript; | ||
gzip_disable "MSIE [1-6]\."; | ||
|
||
upstream ov_service { | ||
server $OPENVERSE_NGINX_UPSTREAM_URL; | ||
} | ||
|
||
server { | ||
access_log /var/log/nginx/access.log json_combined; | ||
|
||
listen 8080; | ||
server_name _; | ||
charset utf-8; | ||
client_max_body_size 75M; | ||
error_page 500 /500.json; | ||
absolute_redirect off; | ||
|
||
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_redirect off; | ||
proxy_pass http://ov_service; | ||
error_page 500 /500.json; | ||
} | ||
|
||
location /version { | ||
default_type "application/json"; | ||
return 200 '{"release": "$OPENVERSE_NGINX_GIT_REVISION", "environment": "$OPENVERSE_NGINX_ENVIRONMENT"}'; | ||
} | ||
|
||
# This is Docker's internal DNS resolver | ||
# In localhost it will resolve the local Plausible and forward anything else to the host | ||
# In production, AWS replaces | ||
resolver 127.0.0.11; | ||
set $plausible_event_url $OPENVERSE_NGINX_PLAUSIBLE_EVENT_URL; | ||
location = /api/event { | ||
proxy_pass $plausible_event_url; | ||
proxy_set_header Host plausible.io; | ||
proxy_buffering on; | ||
proxy_http_version 1.1; | ||
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto $scheme; | ||
proxy_set_header X-Forwarded-Host $host; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters