aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/nuldoc/nginx.conf
blob: 29ff8fc168785ebe81d3ba43f4ef29952fcd21b0 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
map $http_x_forwarded_host $docroot_path {
    hostnames;
    default  /public/default;
    about.*  /public/about;
    blog.*   /public/blog;
    slides.* /public/slides;
}

map $http_x_forwarded_host $about_domain {
    hostnames;
    default          about.nsfisis.dev;
    *.localhost:8000 about.localhost:8000;
}

map $http_x_forwarded_host $blog_domain {
    hostnames;
    default          blog.nsfisis.dev;
    *.localhost:8000 blog.localhost:8000;
}

map $http_x_forwarded_host $slides_domain {
    hostnames;
    default          slides.nsfisis.dev;
    *.localhost:8000 slides.localhost:8000;
}

map $http_x_forwarded_host $is_about_domain {
    hostnames;
    default 0;
    about.* 1;
}

map $http_x_forwarded_host $is_blog_domain {
    hostnames;
    default 0;
    blog.*  1;
}

map $http_x_forwarded_host $is_slides_domain {
    hostnames;
    default  0;
    slides.* 1;
}

map $http_accept $prefer_markdown {
    default "";
    "~text/markdown" "yes";
}

server {
    listen 80 default;
    listen [::]:80;

    gzip on;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_min_length 1024;
    gzip_proxied any;
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/xml
        image/svg+xml
        text/css
        text/markdown
        text/plain
        text/xml;

    location / {
        root $docroot_path;

        location ~* ^.*/atom\.xml$ {
            types { }
            default_type "application/atom+xml; charset=utf-8";
        }

        location ~* \.mjs$ {
            types { }
            default_type "application/javascript; charset=utf-8";
        }

        location ~* \.md$ {
            types { }
            default_type "text/markdown; charset=utf-8";
        }

        location ~ "^(/posts/\d{4}-\d{2}-\d{2}/[^/]+)/$" {
            if ($prefer_markdown) {
                rewrite "^(/posts/\d{4}-\d{2}-\d{2}/[^/]+)/$" $1.md last;
            }
        }
    }

    error_page 404 /404.html;

    # Redirect to canonical path.
    rewrite ^/posts/1/?$  /posts/  permanent;
    rewrite ^/slides/1/?$ /slides/ permanent;

    # Redirect to posts/slides pages.
    if ($is_blog_domain) {
        rewrite ^/?$ $http_x_forwarded_proto://$blog_domain/posts/ permanent;
    }
    if ($is_slides_domain) {
        rewrite ^/?$ $http_x_forwarded_proto://$slides_domain/slides/ permanent;
    }

    if ($is_blog_domain) {
        # Old URL patterns.
        rewrite ^/posts/(my-first-post)/?$                                           $http_x_forwarded_proto://$blog_domain/posts/2021-03-05/$1/ permanent;
        rewrite ^/posts/(phperkaigi-2021)/?$                                         $http_x_forwarded_proto://$blog_domain/posts/2021-03-30/$1/ permanent;
        rewrite ^/posts/(cpp-you-can-use-keywords-in-attributes)/?$                  $http_x_forwarded_proto://$blog_domain/posts/2021-10-02/$1/ permanent;
        rewrite ^/posts/(python-unbound-local-error)/?$                              $http_x_forwarded_proto://$blog_domain/posts/2021-10-02/$1/ permanent;
        rewrite ^/posts/(ruby-detect-running-implementation)/?$                      $http_x_forwarded_proto://$blog_domain/posts/2021-10-02/$1/ permanent;
        rewrite ^/posts/(ruby-then-keyword-and-case-in)/?$                           $http_x_forwarded_proto://$blog_domain/posts/2021-10-02/$1/ permanent;
        rewrite ^/posts/(rust-where-are-primitive-types-from)/?$                     $http_x_forwarded_proto://$blog_domain/posts/2021-10-02/$1/ permanent;
        rewrite ^/posts/(vim-difference-between-autocmd-bufwrite-and-bufwritepre)/?$ $http_x_forwarded_proto://$blog_domain/posts/2021-10-02/$1/ permanent;
        rewrite ^/posts/(vim-swap-order-of-selected-lines)/?$                        $http_x_forwarded_proto://$blog_domain/posts/2021-10-02/$1/ permanent;
        rewrite ^/posts/(phperkaigi-2022-tokens)/?$                                  $http_x_forwarded_proto://$blog_domain/posts/2022-04-09/$1/ permanent;

        # I mistakenly wrote 2023 in the URL instead of 2024.
        rewrite ^/posts/2023-01-10/(neovim-insert-namespace-declaration-to-empty-php-file)/?$ $http_x_forwarded_proto://$blog_domain/posts/2024-01-10/$1/ permanent;

        # Renamed posts.
        rewrite ^/posts/2024-03-20/todos-in-my-life/?$ $http_x_forwarded_proto://$blog_domain/posts/2024-03-20/my-bucket-list/ permanent;
    }

    # Redirect to new domains.
    if ($is_about_domain = 0) {
        rewrite ^/about/?$ $http_x_forwarded_proto://$about_domain/ permanent;
    }
    if ($is_blog_domain = 0) {
        rewrite ^/posts(/.*)/?$ $http_x_forwarded_proto://$blog_domain/posts$1/ permanent;
    }
    if ($is_slides_domain = 0) {
        rewrite ^/slides(/.*)/?$ $http_x_forwarded_proto://$slides_domain/slides$1/ permanent;
    }
}