nginx proxy config not forwarding requests to back

2019-09-06 14:09发布

问题:

Below is the relevant section of my nginx.conf file. I only see the js|css... requests forward to my backend server when i remove the initial location block in the conf file. What im trying to accomplish is to turn off nginx access logging for files of those extensions.

Anybody know a working nginx config technique to allow me to turn off the access logs yet still forward these requests to the proxy location?

...   
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            access_log   off;
}

location / {

if ($ignore_ua) {
    access_log off;
    return 200;
}
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:7777/;
}

回答1:

nginx chooses a location block to process a request. In the case of .js files, your location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ block is used. None of the directives within the location / block are involved. See this document for details.

If you need conditional logging, you could use the if= parameter to the access_log directive instead of a separate location block. See this document for an example.

In your case, it might look like this:

map $request_uri $loggable {
    default 1;
    \.(js|css|png|jpg|jpeg|gif|ico)(\?|$) 0;
}
access_log /path/to/access.log combined if=$loggable;

Note that the map directive goes in the http block.



标签: nginx