try_files / if / access_log question

sfrazer nginx-forum at
Fri May 23 16:22:08 UTC 2014

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/;

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] "$request"
$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 them:

curl -s -D- -A test-agent | 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

<!DOCTYPE html><!--[if lt IE 7]>...

If I uncomment the "if" statement I see this instead:

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

HTTP/1.1 404 Not Found
Server: nginx/1.6.0
Date: Fri, 23 May 2014 16:11:24 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>

What am I doing wrong here?

Posted at Nginx Forum:,250325,250325#msg-250325

More information about the nginx mailing list