summaryrefslogtreecommitdiff
path: root/nginx.conf
blob: a7e345ae0a45678049a5eacce19fe7cb29f9ea35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
map $http_user_agent $format_suffix {
    "~*curl"  ".txt";
    "~*wget"  ".txt";
    default   "";
}

map $http_accept $final_suffix {
    "~*text/plain" ".txt";
    default        $format_suffix;
}

server {
    listen 80;
    server_name jayrup.me;
    root /usr/share/nginx/html;

    # 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;
    gzip_min_length 1024;
    gzip_proxied any;
    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.
    # 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?)$ {
        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 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$final_suffix /public_key /assets/public_key =404;
    }
    location = /assets/dissertation.pdf {
        try_files /dissertation.pdf /assets/dissertation.pdf =404;
    }

    # 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$ {
        default_type application/pdf;
        try_files $uri =404;
    }

    # /cv should point to the PDF or plaintext cv
    location = /cv {
        try_files
            /cv$final_suffix
            /cv/index$final_suffix
            /cv.pdf
            =404;
    }

    # /dissertation serves summary page or PDF fallback
    location = /dissertation {
        try_files
            /projects/dis/index$final_suffix
            /projects/dis/index.html
            /projects/dis$final_suffix
            /dissertation.pdf
            =404;
    }

    location / {
        try_files
            $uri$final_suffix
            $uri.html
            $uri/index$final_suffix
            $uri/index.html
            =404;
    }

    # 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.
}