OpenBSD httpd Tips and Tricks - There are a number of neat things you can do with httpd. The first one that I will mention is the ability to setup password protected directories on your server. Below you will find what to add to your server configuration in your /etc/httpd.conf file. Add lines 2-5 below to your server block to create a password protected directory.

Password protected directories

server "default" {
location "/protected/*" {
directory index "index.html"
authenticate with "/.htpasswd"
   }
}

To set a user and password for the server, you are going to open up a terminal and type:

# htpasswd /var/www/.htpasswd username

Now, you need to chown on .htpasswd and set permissions 440

# chown www:www /var/www/.htpasswd
# chmod 440 /var/www/.htpasswd

So now, once you have these things in place, you can open up your browser and point it towards your newly created password protected directory, for example if your internal IP address of your webserver is 10.0.0.157, then go to https://10.0.0.157/protected/ and you should get a password prompt asking you to enter your username and password.

Custom error pages

Another cool thing you can do with httpd is designing your own custom error pages. You can setup your own custom error pages, by placing them in /var/www/htdocs/errordocs and naming them for example, 404.html. Simply add the following to your server block in your /etc/httpd.conf file

errdocs "/htdocs/errordocs/"

Theres a list of common error codes here

Enable compression on httpd

Another really cool thing you can do with httpd is enable compression in order to compresss the data in your html/css files in order to create faster loading pages using less bandwidth. It's actually quite simple to do, just add to your server block in your /etc/httpd.conf file.

gzip-static

Once you have that in your configuration file, you simply run the following command on your html and css files.

# gzip -k *.html *.css

This gzips and compresses your files while leaving the original files in place, which is necessary. You can test your server to make sure that it is serving the gzipped compressed files.

Get compression info from httpd

Alternatively, to simply determine if a page is being served compressed or not, you can perform a quick test yourself of any page, you can use the following command

# curl -sIH 'Accept-Encoding: gzip,deflate' https://openbsd.mywire.org | grep gzip
Content-Encoding: gzip
If theres any output then your page is being served compressed. No output means it is not served compressed. Another thing you can do with curl and wc is measure the size of those files in bytes before and after gzip

# curl -s https://openbsd.mywire.org | wc -c
7800
# curl -s https://openbsd.mywire.org | gzip | wc -c
2954
If you make any changes to your html or css files, you must run the gzip command again because the gzip files must have a later date/time than the original html/css files.


Powered by OpenBSD httpd on a Raspberry Pi | This website is IPv6 enabled