Skip to content

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

3.8 KB
Raw Blame
# 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
# - OPENVERSE_NGINX_DNS_RESOLVER

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",'
    '"upstream_cache_status":"$upstream_cache_status",'
    '"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;
}

proxy_cache_path  /data/nginx/cache levels=1:2 keys_zone=nuxt-cache:25m max_size=1g inactive=7d use_temp_path=off;

server {
    access_log  /var/log/nginx/access.log  json_combined;

    listen          $OPENVERSE_NGINX_LISTEN_PORT;
    server_name     _;
    charset        utf-8;
    client_max_body_size 75M;
    error_page 500 /500.json;
    absolute_redirect off;

    location / {
        include snippets/nuxt_location.conf;
        error_page 500 /500.json;
    }

    location /_nuxt {
        include snippets/cached_location.conf;
    }

    location /homepage_images {
        include snippets/cached_location.conf;
    }

    location /error_images {
        include snippets/cached_location.conf;
    }

    # Refers to the nested src/static/static directory
    # See README in that directory for details
    location /static {
        include snippets/cached_location.conf;
    }

    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 $OPENVERSE_NGINX_DNS_RESOLVER;
    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;

        proxy_intercept_errors on;
        error_page 502 503 504 = @custom_424;  # Report Plausible errors as Failed dependency
    }

    # Replace Plausible server errors (when the service is down) with a 424 error.
    location @custom_424 {
        internal;
        add_header Content-Type "application/json";

        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;

        return 424 '{"error": "Failed Dependency", "message": "Plausible service unavailable.", "plausible_status": $upstream_status}';
    }
}