How to Respond to All Nginx Requests with a Static HTML Page

By Lowell Heddings on May 26th, 2017

After we switched HTG over to HTTPS, we enabled the HSTS headers that force browsers to always talk to *.howtogeek.com over HTTPS — but we forgot one thing. We were still using Feedburner to serve up feeds.howtogeek.com.

So once we got into the HSTS preload list, nobody could to go http://feeds.howtogeek.com anymore, and Feedburner’s custom DNS option didn’t support HTTPS. So it was time to switch to our own thing.

The quick and easily solution was to serve up all requests to a static copy of our RSS feed that we had stored in the index.html file. To force nginx to serve that file whether somebody went to /howtogeek/ or /howtogeek/1 or even /howtogeek1 was as simple as using the try_files directive.

You’ll just want to edit your server or location block with try_files like this:

try_files $uri /index.html;

Replace the /index.html part with whatever your static HTML page is. Once you’re done you’ll have a block kinda like this, which will send every request to the file index.html which should be found in the root directory, in this case /data/webroots/feeds/.

server {
	server_name feeds.howtogeek.com;
	access_log off; log_not_found off;

	root /data/webroots/feeds/;

	try_files $uri /index.html;
	index index.html;
}

Restart nginx with a

service nginx reload

command, and you’re all set.