Try_files / if / access_log question

I’m trying to create a config that doesn’t log the requests from
user agents. The site has a gunicorn backend that we proxy to, and I’m
trying to set up try_files to test for the existence of static local
before proxying to the back-end.

The try_files config is the new part, everything was working fine before
added that.

Here’s the nginx.conf I’m using for testing:

user nginx;
worker_processes 8;

error_log /var/log/nginx/error.log warn;
pid /var/run/;

events {
worker_connections 1024;

http {
upstream backend {

include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr - $remote_user [$time_local]
$status $body_bytes_sent “$http_referer” “$http_user_agent”
access_log /var/log/nginx/access.log main;

map $http_user_agent $ignore_ua {
default 0;
“test-agent” 1;

server {
listen 80;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header Accept-Encoding “”;

location @gunicorn {
  proxy_pass http://backend$uri$args;
  add_header X-Cached $upstream_cache_status;

location / {

if ($ignore_ua) { access_log off; }

  try_files $uri/index.cchtml @gunicorn;


With the “if” statement commented out, the requests work as I expect

curl -s -D- -A test-agent |
-n 20

HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Fri, 23 May 2014 16:11:38 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Cookie

Additional note: the error.log shows this on the 404:

2014/05/23 11:11:24 [error] 25677#0: *5 “/etc/nginx/html/uri/index.html”
not found (2: No such file or directory), client:, server: ,
request: “GET /uri/ HTTP/1.1”, host: “

Posted at Nginx Forum:

On Fri, May 23, 2014 at 12:22:08PM -0400, sfrazer wrote:

Hi there,

The try_files config is the new part, everything was working fine before I
added that.

location / {

if ($ignore_ua) { access_log off; }

  try_files $uri/index.cchtml @gunicorn;

What am I doing wrong here?

You are using “if” inside “location” without understanding the
subtleties. The safest option is not to do that.

I suspect that the simplest option will be to use a newer nginx which
can do conditional logging without the “if” directive.

Failing that, then – untested! – you might have luck if you duplicate
the try_files line inside the “if” block.


Francis D. [email protected]