Install Pygments and configure nginx
After setting up the blog, I ran into several problems, two of them were that I didn’t have syntax highlighting and nginx redirected requests to paths without /
at the end to wrong URL.
Install Pygments⌗
Pygments is a Python library used by Hugo to generate syntax highlighting for static HTML pages. Installing it locally is simple, just run
pip install Pygments
However, I am using Travis to automate the build process. Therefore, I need to tell Travis to install Pygments before running the build. Fortunately, Travis supports multiple languages for setting up the container. All I have to do is specify the python version I want to install in .travis.yml
file.
language: go
go:
- 1.7.x
python:
- "2.7"
And then in install
phase, I need to tell Travis to install Pygments. To simplify the build script, I just run pip
as sudo
so that I don’t have to go through all the virtual environment setup.
install:
- sudo pip install Pygments
However, in order for that to work, I also need to tell Travis that I need access to sudo
in order to set up my container. Just add sudo: required
to .travis.yml
will do the job.
Nginx configuration for Dokku⌗
Another problem was that Nginx redirected requests to paths without /
to wrong URL. For example
GET http://tannguyen.org/tags/hugo - 302 (redirected to http://tannguyen.org:5000/tags/hugo/)
GET http://tannguyen.org/tags/hugo/ - 200
The redirected path is correct but the port is wrong. 5000
is the port of the Docker container used to run my blog. So, either I need to make sure that nginx does not use internal port for redirecting requests or make sure all my blog URLs have /
at the end. I decided to go with the later because it’s easier to do and also make my blog URLs consistent.
In my setup there are 2 nginx servers running, one for handling public requests to the main domain and the other one is used internally to serve HTML pages generated by Hugo. Dokku allows me to overwrite its nginx configuration by simply having a nginx.conf.sigil
file in the folder that I am going to deploy. Now it’s only the matter of configuring nginx to force trailing slash for all URLs, just add the following rewrite rule to the location
block
rewrite ^([^.]*[^/])$ $1/ permanent;
In my set up, I make it so that the auto generated public
folder is used for tar deployment to Dokku server. Therefore, I have to put nginx.conf.sigil
outside that folder because it’s in .gitignore
. For my own convention, I put all the files that need to be copied to public
for deploying to .dokku
folder and then add this to after_success
step
after_success:
- cp -fv .dokku/* public
If you are curious about the final setup, you can take a look at the source code