Originally published on 03 January 2019
Last updated on 09 October 2019
Nginx is a lightweight, open-source web server that features fast performance suitable for serving static pages such as those on this site. Let's get started on installing it on FreeBSD based on this tutorial from Digital Ocean.
The first thing we will do is to install both nginx and rsync
which will be used to sync the static html files from our local client over to our hosted server:
$ sudo pkg install nginx rsync
Enable the nginx service by adding the following line to /etc/rc.conf
:
nginx_enable="YES"
Start the service:
$ sudo service nginx start
Make a backup of the current nginx.conf
file:
$ cd /usr/local/etc/nginx $ sudo cp nginx.conf nginx.conf_YYYYMMDD.bak
Create a new directory under /usr/local/www
where your site's files will be served from:
$ sudo mkdir /usr/local/www/example
Configure nginx to serve files from that directory. Within the /usr/local/etc/nginx/nginx.conf
file, update the line with the root directive such that it looks like the following:
root /usr/local/www/example;
Note: Do not forget the semicolon (;) at the end of the line.
If you have a domain name which is pointing back to your server, you need to update the server_name
in the server
block:
/usr/local/etc/nginx/nginx.conf: http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { #server_name localhost; server_name example.com; #charset koi8-r; #access_log logs/host.access.log main; location / { #root /usr/local/www/nginx; root /usr/local/www/example; index index.html index.htm; } #error_page 404 /404.html;
Restart nginx for the changes to take effect:
$ sudo service nginx restart
Finally, change the owner of the directory to your own user to that you can write to it:
$ sudo chown -R username:username /usr/local/www
You can now upload your site's files to /usr/local/www/example
which you can then view at http://example.com. Be sure to include an index.html
file which will serve as the site's home page.
If you want to redirect requests from www.example.com
to example.com
, you will need to make further configuration changes to your /usr/local/etc/nginx/nginx.conf
file. Right below the comment line that says #gzip on;
, add the following server block to redirect www
requests to the main domain:
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
Please see this section of the nginx wiki for more details on the configuration of this server block. Be sure to restart the nginx service in order for the changes to take effect.
If you want to serve your blog posts from a separate subdomain (e.g. blog.example.com
), you will have to make further modifications to the /usr/local/etc/nginx.conf
file. Right below the section you added to redirect www
requests, but before the main server block, add another server block which will handle requests for the blog site (e.g. blog.example.com
):
server {
listen 80;
server_name blog.example.com;
root /usr/local/www/example/blog;
index index.html;
}
Restart the nginx service for the changes to take effect.
Make sure you create the appropriate directory from where the blog pages will be served:
$ sudo mkdir /usr/local/www/example/blog
In the main server block itself, be sure to update the server_name
and root
lines:
server_name example.com;
root /usr/local/www/example;
If you want to use a custom 404 "Page Not Found" page for your site, you can follow the directions over at this Digital Ocean article. I created a custom file (404-page-not-found.html
) that I then dropped into /usr/local/www/example. You can then add the following lines under both the blog.example.com
and example.com
server blocks:
error_page 404 /404-page-not-found.html;
location = /404-page-not-found.html {
root /usr/local/www/example;
internal;
}
Finally, be sure to restart the nginx service for your changes to take effect:
$ sudo service nginx restart