The tl;dr version of setting up vanity Go import urls using nginx only.
# /etc/nginx/sites.d/01-go.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name go.oneofone.dev;
include conf.d/ssl.conf; # config with ssl config
location ~* /([^/]+) { # match the first part of the path, we only need that
set $pkg "$1";
set $fullURI '$server_name$uri';
set $pkgURI '$server_name/$pkg';
set $gh 'https://github.com/OneOfOne/$pkg';
set $goimp '<meta name="go-import" content="$pkgURI git $gh">\n';
set $gosrc '<meta name="go-source" content="$pkgURI $gh $gh/tree/master{/dir} $gh/blob/master{/dir}/{file}#L{line}">\n';
set $redir '<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/$fullURI@master">\n';
default_type 'text/html';
return 200 '<html>\n<head>\n<title>$uri</title>\n$goimp$gosrc$redir</head>\n<body>\nthe pink stars are falling in <a href="$gh">lines</a>.\n</body>\n</html>\n';
}
location / {
return 301 "https://oneofone.dev";
}
}