Try_files / if / access_log question

I’m trying to create a config that doesn’t log the requests from
specific
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
files
before proxying to the back-end.

The try_files config is the new part, everything was working fine before
I
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/nginx.pid;

events {
worker_connections 1024;
}

http {
upstream backend {
server 127.0.0.1:8004;
}

include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr - $remote_user [$time_local]
“$request”
$status $body_bytes_sent “$http_referer” “$http_user_agent”
“$http_x_forwarded_for”’;
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
them:

curl -s -D- -A test-agent http://site.ordprofile01.example.net/uri/ |
head
-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”
is
not found (2: No such file or directory), client: 127.0.0.1, server: ,
request: “GET /uri/ HTTP/1.1”, host: “site.ordprofile01.example.net

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.

http://nginx.org/r/access_log

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

f

Francis D. [email protected]