| m u s h i n |mu-shin.ca

 
June 5, 2013

nGinx: How to test your configuration in “console mode”

Category: Networking — Tags: — Marc Trudel-BĂ©lisle @ 7:38 AM

Technically, nGinx doesn’t have a “console mode”, where you can just call the executable and get the output on screen, suspend or kill the process, etc.

But recently, I was looking for a way to test my nGinx config on a live server without affecting the actual production service. The customer had a specific build version, and I didn’t want to waste time in trying to replicate their setup. But of course, I had to do it risk-free;

Here is how I did it

mt@kodachi:~ $ mkdir -p nginx/sites
mt@kodachi:~ $ cd nginx/
mt@kodachi:~/nginx $ vim nginx.conf

Then, in nginx.conf:

pid nginx.pid;

worker_processes  1;

error_log  /dev/stderr;

events {
    worker_connections  1024;
}

daemon off;

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log  /dev/stdout;

    sendfile        on;
    keepalive_timeout  65;

    include sites/*.conf;
}

From there, I just started to import the server configurations that I had to alter, put them in sites, comment out access/error_log definition and change the listen port.

Now, every time I was ready to test my code, I could simply do this (and curl from another console):

Here is how I did it

mt@kodachi:~/nginx $ nginx -c $(pwd)/nginx.conf 
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2013/06/05 20:33:49 [error] 12042#0: *1 access forbidden by rule, client: 127.0.0.1, server: superduper, request: "GET /api/admin/test HTTP/1.1", host: "localhost:8000"
127.0.0.1 - - [05/Jun/2013:20:33:49 +0900] "GET /api/admin/test HTTP/1.1" 403 168 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
2013/06/05 20:33:49 [error] 12042#0: *2 access forbidden by rule, client: 127.0.0.1, server: superduper, request: "GET /api/admin/test HTTP/1.1", host: "localhost:8000"
127.0.0.1 - - [05/Jun/2013:20:33:49 +0900] "GET /api/admin/test HTTP/1.1" 403 168 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
2013/06/05 20:33:49 [error] 12042#0: *3 access forbidden by rule, client: 127.0.0.1, server: superduper.jp, request: "GET /api/admin/test HTTP/1.1", host: "localhost:8000"
127.0.0.1 - - [05/Jun/2013:20:33:49 +0900] "GET /api/admin/test HTTP/1.1" 403 168 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
^C
mt@kodachi:~/nginx $

I found this way of testing a configuration to be really convenient. It is quick, could be leveraged to be used against a freshly compiled nGinx version, will never affect the machine you are working on, etc.

1 Comment »

  1. Thanks Mushin. Checking error logs again and again during development was driving me crazy.

    Comment by Akshat Jiwan Sharma — May 28, 2014 @ 4:30 PM

RSS feed for comments on this post. TrackBack URL

Leave your comment

Powered by WordPress