best way to debug nginx rewrite rules in config file?

Solution 1:

Enable rewrite_log:

rewrite_log on;

and set debug level in error_log directive:

error_log /var/log/nginx/localhost.error_log notice;

Solution 2:

Enable debugging support, then set debug level in error_log.

error_log   /var/log/nginx/error.log debug;

Now you can tail the log and send your requests through. There's probably more detail than you want, but that can sometimes be a lifesaver.

Oh, and you should be aware that if is evil, in a location context at least...


Solution 3:

Using logs and and built-in support for debugging is definitely the most reasonable way. If you are doing some quick routing debugging at early stages and want to interact through the browser/client only, using the return 4xx "text"; directive may also give you the answer you want with very little effort. For example,

http {
  server {
    listen 80;
    server_name mydomain.net;
    return 404 "mydomain 80 route";
  }

  server {
    listen 80 default_server;
    return 404 "default 80 route";
  }
}

The text in the returned webpage will tell you which server block your request triggered.