Files
romm/docker/nginx/templates/default.conf.template
Michael Manganiello 8c52b7828b feat: Add COEP and COOP headers for EmulatorJS within Nginx config
The COEP (Cross-Origin Embedder Policy) and COOP (Cross-Origin Opener
Policy) headers are needed by EmulatorJS to use the `SharedArrayBuffer`
feature, and enable multi-threaded cores.

These headers are only being set by Nginx for responses to requests
made to the EmulatorJS player path. This is because cross-origin
isolation breaks other features in the application (such as YouTube
embeds), so we only want to enable it for the EmulatorJS player.

It's important to mention that this change does not work when `DEV_MODE`
is set, as we would need Vite server to also set these headers in that
case. This could be implemented separately, if needed.

These changes are not enough to enable multi-threaded cores in the
frontend. Because our application is a SPA, we also need to ensure
that navigation to the EmulatorJS player path results in a full page
reload, so that the cross-origin isolation can be applied. This will be
handled in a separate PR.
2025-08-31 21:43:32 -03:00

104 lines
3.1 KiB
Plaintext

# This template is used to generate the default.conf file for the nginx server,
# by using `envsubst` to replace the environment variables in the template with
# their actual values.
# Helper to get scheme regardless if we are behind a proxy or not
map $http_x_forwarded_proto $forwardscheme {
default $scheme;
https https;
}
# COEP and COOP headers for cross-origin isolation, which are set only for the
# EmulatorJS player path, to enable SharedArrayBuffer support, which is needed
# for multi-threaded cores.
map $request_uri $coep_header {
default "";
~^/rom/.*/ejs$ "require-corp";
}
map $request_uri $coop_header {
default "";
~^/rom/.*/ejs$ "same-origin";
}
server {
root /var/www/html;
listen ${ROMM_PORT};
listen [::]:${ROMM_PORT};
server_name localhost;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $forwardscheme;
location / {
try_files $uri $uri/ /index.html;
proxy_redirect off;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers *;
add_header Cross-Origin-Embedder-Policy $coep_header;
add_header Cross-Origin-Opener-Policy $coop_header;
}
# Static files
location /assets {
try_files $uri $uri/ =404;
}
# OpenAPI for swagger and redoc
location /openapi.json {
proxy_pass http://wsgi_server;
}
# Backend api calls
location /api {
proxy_pass http://wsgi_server;
proxy_request_buffering off;
proxy_buffering off;
}
location /ws {
proxy_pass http://wsgi_server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# Internally redirect download requests
location /library/ {
internal;
alias "${ROMM_BASE_PATH}/library/";
}
# This location, and the related server at port 8081, are used to serve files when
# using the `mod_zip` module. This is because the `mod_zip` module does not support
# calculating CRC-32 values when using subrequests pointing directly to internal
# locations that access the filesystem.
# TODO: If that gets fixed, this workaround can be removed, and the `/library` location
# can be used directly (also removing the server at port 8081).
# Related issue: https://github.com/evanmiller/mod_zip/issues/90
location /library-zip {
internal;
rewrite ^/library-zip/(.*)$ /library/$1 break;
proxy_pass http://localhost:8081;
# Proxy buffering must be disabled, for the module to correctly calculate CRC-32 values.
proxy_buffering off;
}
# Internal decoding endpoint, used to decode base64 encoded data
location /decode {
internal;
js_content decode.decodeBase64;
}
}
server {
listen 8081;
listen [::]:8081;
server_name localhost;
location /library/ {
alias "${ROMM_BASE_PATH}/library/";
}
}