Using HTTPS with Github Pages and a Custom Domain

Tutorials

Github pages is a very convenient way to host both personal websites, and project websites. Websites are linked to a git branch or repo, and update when pushed too. Github pages supports custom domains as well! Whilst Github Pages supports HTTPS for github.io websites, it does not support HTTPS when using a custom domain.

However, we can use a proxy server to allow us to use a custom domain with HTTPS! Both the connection between the client and the proxy server, and the proxy server and Github will be secure.

Whilst you lose the advantage of Github’s CDN, you still have the following advantages:

Add CNAME file

This is recommended to make sure that example.github.io is redirected to example.com.

Create an NGINX proxy

Point your DNS to the proxy server instead of github.io.

Add the following virtual server to your sites file:

server {
	listen 443 ssl http2;
	server_name example.com;

	# Insert SSL specific settings here

	location / {
		proxy_pass https://example.github.io;
		proxy_intercept_errors on;

		# Makes us request example.github.io, instead of example.com
		proxy_set_header Host example.com;
		proxy_set_header X_FORWARDED_PROTO https;

		# Give GitHub control over caching headers
		expires off;
	}
}

# Redirect HTTP to HTTPS
server {
	listen 80;
	listen [::]:80;
	server_name example.com;
	return 301 https://$server_name$request_uri;
}

Setting up NGINX and SSL is out of scope for this tutorial, I’m assuming you know how to configure both. If not, have a look at installing NGINX and securing NGINX with Let’s Encrypt.

Make sure you add the domain to your certificate first.

Be warned that if you have HSTS enabled, you won’t be able to go back to HTTP.