Nginx location and rewrite

gyre asked:

I’m dealing with the following issue and I can’t figure out to resolve it.
I have HAproxy in front of app server farm which are running nginx.
HAproxy has some httpchecks enabled for backend services – the checks are hitting /health_check URI:

option httpchk HEAD /health_check HTTP/1.1rnHost: staging.example.com

What I can’t figure out is how to avoid logging for Virtualhosts which have permanent rewrites enabled for all URIs.
Here’s the example

server {
  listen      8000;
  server_name "";

  location = /health_check {
    access_log off;
    return 200;
  }

  rewrite     ^(.*)   https://staging.example.com$1 permanent;
}

I thought the above location directive would disable the logs for given URI but I was wrong – it’s being completely ignored.

is there any way how to acomplish something like this ??

My answer:


Put the rewrite in a location / block.

location / {
    rewrite     ^(.*)   https://staging.example.com$1 permanent;
}

This way, it will match all URLs except for /health_check which has a more specific location.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.