Having intermittently struggled with
configuration files for the majority of my adult life, I find
an absolute joy to set up. I’m completely sincere about that. But, for those who are just getting into Web development,
is just about as much of a struggle as Apache used to be—in fact, probably more so, because there’s less abundant learning material out there on the Internet.
So, here’s an attempt to make that situation just the slightest bit better.
If you don’t already have
installed, I encourage you to follow these directions
for building OpenResty
, an enhanced version of
that enables building entire Web apps within the
process using the beautiful programming language Lua
But, from here on, I’m going to assume that you already have a stock version of
installed. Verify that if you run
$ nginx -v
you get some kind of reasonable response, like
nginx version: nginx/1.2.3
Now, make a file called
I’ve chosen the number 4945 so as to hopefully not conflict with any services that may already be running on your machine for one reason or another. Now, let’s launch
using this configuration file and test it:
$ nginx -p `pwd`/ -c hi.conf nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied) $ telnet localhost 4945 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.0 HTTP/1.1 200 OK Server: nginx/1.2.3 Date: Mon, 07 Apr 2014 01:50:28 GMT Content-Type: text/plain Content-Length: 0 Connection: close Connection closed by foreign host. $ kill -QUIT `cat nginx.pid`
You’ll have to actually enter the line
GET / HTTP/1.0
. HTTP is a protocol intended for humans to be able to read and write, and you may as well take advantage of it! Of course, you could also navigate to
in a browser, but then all you see is a blank page, which is not quite as satisfying (to me, at least) as a
on the terminal.
What’s that? You want to actually serve data, and not just a blank page?
Then just drop an
into the same folder as
$ nginx -p `pwd`/ -c hi2.conf
Now you should be able to load
and see what you wrote in