nginx on OS X (for pelican)

Installing nginx on OS X is fairly straight forward.

It involves using a pkg manager like homebrew. Additionally, if you want to use it with something like pelican, there are a couple tweaks I do to faciliate local development on my laptop.

nginx installation

Starting off, we install nginx with brew:

brew install nginx


nginx startup

As the last part of the install, we need to link the plist and then load the plist so launchd will keep nginx running. We can do that with:

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents


I make a tweak to the plist to keep nginx running (meaning that if I edit the config file and kill nginx, launchd will restart it for me). I put this bit right above the line <key>Label</key>.

<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>


launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist


nginx configuration

Now we can go edit the nginx config file (by defafult, brew puts this in /usr/local/etc/nginx).

Here is a snippet from the nginx config. I specify a root that is where I’m doing my pelican work, in the output directory. Of note, I enable autoindex for the ‘drafts’ directory because there is no index page. The idea being that for publication, you don’t want people rooting around in your drafts, and with out a direct link, it will (in essence) keep people out. For my deveopment machine, there is no harm in making it a little easier for me to look at drafts while I’m working on pages.

server {
listen       8000;
server_name  test.your_domain.com;
root /Users/louisk/path/to/pelican/your_domain.com/output;
index  index.html
index.htm;
location /drafts {
autoindex on;
allow 127.0.0.1;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}


/etc/hosts

If you’re running nginx on a desktop or laptop, changes are you aren’t planning on doing a lot of web hosting with it. Additionally, you probably don’t want to run a DNS server (even a light weight one) either. Fortunately, you don’t have. Some modifications to /etc/hosts and we should be up and running. The reason I used test as a subdomain, is that I didn’t want to collide with any of the actual domain running external to the laptop (in case I want to have a browser page open with both the test site and the regular production site at the same time).

127.0.0.1 test.your_domain.com


Now, you should be able to point your browser at ‘test.your_domain.com:8000’ and you should see the proper content that pelican has generated.