Forum: NGINX try_files / if / access_log question

2974d09ac2541e892966b762aad84943?d=identicon&s=25 sfrazer (Guest)
on 2014-05-23 18:22
(Received via mailing list)
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

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

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

curl -s -D- -A test-agent http://site.ordprofile01.example.net/uri/ |
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

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.6.0</center>
</body>
</html>

What am I doing wrong here?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,250325,250325#msg-250325
2974d09ac2541e892966b762aad84943?d=identicon&s=25 sfrazer (Guest)
on 2014-05-23 18:27
(Received via mailing list)
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:
http://forum.nginx.org/read.php?2,250325,250326#msg-250326
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2014-05-23 20:53
(Received via mailing list)
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 Daly        francis@daoine.org
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.