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; } server { listen 80 default; listen [::]:80; 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"; } } 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; } }