diff options
| author | Void Agent <void@jayrup.hermes> | 2026-10-22 23:11:10 +0100 |
|---|---|---|
| committer | Void Agent <void@jayrup.hermes> | 2026-10-22 23:11:10 +0100 |
| commit | 70dffce202a8cc1d15473bbc2750bdf29a54b0fb (patch) | |
| tree | 8383299dd8a49f7805f64d57415975cf9fd9410b /nginx.conf | |
| parent | b81fa0b2040e5b1b97806ffbf242948e74c4ef88 (diff) | |
nginx: serve public_key to curl cleanly, restore lost security headers
`curl https://jayrup.me/public_key` is the documented way to get this key
onto a VPS, so both the file and the config serving it need to be exact.
Key file: drop the trailing space, one trailing newline. A key with no
trailing newline makes `curl ... >> ~/.ssh/authorized_keys` glue itself onto
the previous line and silently break every later key in the file.
nginx:
- `/assets/public_key` ignored $final_suffix, so curl got 404 while browsers
got 200. Route both through the suffix-aware try_files.
- add_header does not merge across levels: the asset, PDF and .txt locations
each declared one, so they inherited none of the security headers. The
asset location now repeats them; the PDF and .txt locations no longer
declare any (default_type / mime.types + charset do the job), so they
inherit again.
- `add_header Content-Type application/pdf` appended a second Content-Type
instead of setting it. Use default_type.
- `expires 30d` plus an explicit Cache-Control emitted two Cache-Control
headers. Use one explicit header.
- add `charset utf-8` so text/plain (what curl gets) is not served
charset-less while the content contains non-ASCII.
deploy: ship nginx.conf alongside public/. It is bind-mounted into the
container, so it never travelled with the rsync'd tree — the live config was
from May and predated the hardening committed in 0ee667a. Test with
`nginx -t` before reloading and restore the previous config on failure.
Diffstat (limited to 'nginx.conf')
| -rw-r--r-- | nginx.conf | 43 |
1 files changed, 31 insertions, 12 deletions
@@ -17,6 +17,12 @@ server { # Don't leak nginx version on error pages / server header server_tokens off; + # The whole site is UTF-8. Without this, text/plain responses (i.e. what + # curl gets) carry no charset at all, so em dashes and other non-ASCII + # bytes are left ambiguous. + charset utf-8; + charset_types text/css text/plain text/xml application/javascript application/json application/rss+xml; + # Gzip compression gzip on; gzip_vary on; @@ -25,28 +31,42 @@ server { gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss image/svg+xml; # Security headers + # + # add_header does NOT merge across levels: a location that declares any + # add_header inherits none from here. Every location below that adds a + # header therefore repeats these three verbatim. add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; - # Static asset caching + # Static asset caching. + # Cache-Control is set explicitly rather than via `expires`, because + # `expires` emits its own Cache-Control and the pair would duplicate. + # No `always`: a cached 404 for a missing asset is worse than no cache. location ~* \.(ico|css|js|gif|jpe?g|png|svg|woff2?)$ { - expires 30d; - add_header Cache-Control "public, no-transform"; + add_header X-Content-Type-Options "nosniff" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Cache-Control "public, max-age=2592000, no-transform"; + try_files $uri =404; } - # Compatibility for assets prefix + # Compatibility for the assets prefix. These must honour $final_suffix: + # the deploy script ships the key as both /public_key and /public_key.txt, + # and only the .txt twin exists for curl/wget. location = /assets/public_key { - try_files /public_key /assets/public_key =404; + try_files /public_key$final_suffix /public_key /assets/public_key =404; } location = /assets/dissertation.pdf { try_files /dissertation.pdf /assets/dissertation.pdf =404; } - # Serve PDFs directly + # Serve PDFs inline. `default_type` sets Content-Type; using add_header + # here would emit a second Content-Type next to the one from mime.types. + # Content-Disposition is omitted deliberately — nginx already serves + # unknown-to-it types inline, and browsers render PDFs inline by default. location ~* \.pdf$ { - add_header Content-Type application/pdf; - add_header Content-Disposition inline; + default_type application/pdf; try_files $uri =404; } @@ -78,8 +98,7 @@ server { =404; } - location ~* \.txt$ { - add_header Content-Type text/plain; - } + # No explicit .txt location: mime.types already maps .txt to text/plain and + # `charset utf-8` above appends the charset. Declaring one only to set + # Content-Type would drop the inherited security headers. } - |
