skillhub/web/nginx.conf.template
千乘妍 (Xiaoyaner) 7fb74e81b2 feat(deploy): configure same-origin optional analytics
Safely transport bounded JSON using existing image tools, generate isolated exact proxy routes with verified HTTPS, and wire Compose and Helm with closed defaults.

Refs #989

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>
2026-10-29 23:16:31 +08:00

129 lines
5 KiB
Text

server_tokens off;
server {
listen 80;
server_name _;
resolver ${NGINX_LOCAL_RESOLVERS} valid=10s;
set $skillhub_api_upstream "${SKILLHUB_API_UPSTREAM}";
root /usr/share/nginx/html;
index index.html;
client_max_body_size 100M;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_min_length 1000;
# Ignore client-supplied forwarded proto by default. Operators may explicitly trust a
# sanitizing upstream proxy; only canonical http/https values are then accepted.
set $proxy_x_forwarded_proto $scheme;
set $forwarded_proto_source "${SKILLHUB_TRUST_FORWARDED_PROTO}:$http_x_forwarded_proto";
if ($forwarded_proto_source ~* "^true:https$") {
set $proxy_x_forwarded_proto https;
}
if ($forwarded_proto_source ~* "^true:http$") {
set $proxy_x_forwarded_proto http;
}
# Keep the request authority, including an explicit port or bracketed IPv6.
# $host drops the port; Spring also resets it when applying forwarded proto,
# so forward the complete authority in X-Forwarded-Host as well as Host.
# Reject ambiguous authority syntax, but allow internal underscore names.
# HTTP/1.0 requests without Host retain Nginx's existing $host fallback.
if ($http_host !~ "^(?:(?:[A-Za-z0-9._-]+|\\[[0-9A-Fa-f:.]+\\])(?::[0-9]{1,5})?)?$") {
return 400;
}
set $skillhub_forwarded_host $host;
if ($http_host != "") {
set $skillhub_forwarded_host $http_host;
}
# Sub-path routing is generated by docker-entrypoint.d/20-base-path.sh. The glob
# tolerates its absence (root deployment or config-only tests); the default below
# keeps $skillhub_forwarded_prefix defined even when the file is not present.
set $skillhub_forwarded_prefix "";
include /etc/nginx/skillhub-base-path*.conf;
include /etc/nginx/skillhub-analytics*.conf;
# Reserve this prefix even in config-only checks without an entrypoint.
location /_skillhub-analytics/ { return 404; }
location / {
add_header Cache-Control "no-cache, must-revalidate" always;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass $skillhub_api_upstream;
proxy_set_header Host $skillhub_forwarded_host;
proxy_set_header Forwarded "";
proxy_set_header X-Forwarded-Host $skillhub_forwarded_host;
proxy_set_header X-Forwarded-Port "";
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 $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Prefix $skillhub_forwarded_prefix;
}
location /oauth2/ {
proxy_pass $skillhub_api_upstream;
proxy_set_header Host $skillhub_forwarded_host;
proxy_set_header Forwarded "";
proxy_set_header X-Forwarded-Host $skillhub_forwarded_host;
proxy_set_header X-Forwarded-Port "";
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 $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Prefix $skillhub_forwarded_prefix;
}
location /login/oauth2/ {
proxy_pass $skillhub_api_upstream;
proxy_set_header Host $skillhub_forwarded_host;
proxy_set_header Forwarded "";
proxy_set_header X-Forwarded-Host $skillhub_forwarded_host;
proxy_set_header X-Forwarded-Port "";
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 $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Prefix $skillhub_forwarded_prefix;
}
location /.well-known/ {
proxy_pass $skillhub_api_upstream;
proxy_set_header Host $skillhub_forwarded_host;
proxy_set_header Forwarded "";
proxy_set_header X-Forwarded-Host $skillhub_forwarded_host;
proxy_set_header X-Forwarded-Port "";
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 $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Prefix $skillhub_forwarded_prefix;
}
location /assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
location = /registry/skill.md {
default_type text/plain;
include /etc/nginx/skillhub-guide-public-url*.conf;
add_header Cache-Control "no-cache";
add_header Content-Disposition "inline";
add_header X-Content-Type-Options "nosniff";
try_files $uri =404;
}
location = /registry/skill.md.template {
return 404;
}
location = /runtime-config.js {
add_header Cache-Control "no-store";
try_files $uri =404;
}
location /nginx-health {
return 200 'ok';
add_header Content-Type text/plain;
}
}