Vanity Go Import Paths with NGINX

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;
	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 '$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=$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 "";
Ahmed W. Mones
Ahmed W. Mones
VP Of Technology

I’m always interested in learning new things and new technologies.

comments powered by Disqus