Useless GET requests for favicon.ico and assets in my /static/
folder are littering my nginx access log. And it's a busy server - meaning I/O is being wasted too. Not to mention my fail2ban installation sometimes takes warped decisions!
How do I exclude these nuisance log lines? I'm on nginx 1.4.x
.
I found a way to do it in apache, but not in nginx.
Here's my nginx virtual host file:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=6m;
upstream my_server {
server unix:/home/myuser/myfolder/myapp/myapp.sock fail_timeout=0;
}
server {
server_name example.com www.example.com;
listen 80;
return 301 https://example.com$request_uri;
}
server {
server_name www.example.com;
listen 443 ssl;
listen [::]:443 ssl;
. . . SSL related stuff . . .
. . . adding security related headers . . .
return 301 https://example.com$request_uri;
}
server {
server_name example.com;
listen 443 ssl;
listen [::]:443 ssl;
. . . SSL related stuff . . .
. . . adding security related headers . . .
charset utf-8;
underscores_in_headers on;
limit_conn conn_limit_per_ip 10;
limit_req zone=req_limit_per_ip burst=20;
# write error log file for https errors:
error_log /var/log/nginx/https-error_log warn;
location ~* \.(?:ico|css|js|gif|jpg|jpeg|png|svg|woff|ttf|eot)$ {
root /home/myuser/myfolder/myapp;
access_log off;
expires 120d;
add_header Pragma public;
add_header Cache-Control public;
}
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
access_log off;
alias /home/myuser/myfolder/myapp;
}
location /status {
stub_status on;
allow 127.0.0.1;
allow 40.114.247.165;
deny all;
}
location / {
#proxy_pass_request_headers on;
proxy_buffering on;
proxy_buffers 24 4k;
proxy_buffer_size 2k;
proxy_busy_buffers_size 8k;
try_files $uri @https_proxy_to_app;
}
location @https_proxy_to_app {
proxy_set_header X-Forwarded-Proto https;
# additional proxy parameters
include proxy_params;
proxy_redirect off;
proxy_pass http://my_server;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/myuser/myfolder/myapp/templates/;
}
location = /backflip.png {
root /home/myuser/myfolder/myapp/static/img/;
}
}
In nginx.conf
, I have defined the following in the http
block:
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /var/log/nginx/access.log compression;
And here's an example of stuff I'm seeing in the access logs:
192.16.53.127 - - [24/Sep/2017:01:12:24 +0000] "GET /static/emoticons/puke1.png HTTP/1.1" 301 178 "https://example.com/page2/" "service-1460643764266048;Mozilla/5.0 (Linux; Android 6.0; QMobile X32 Power Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36" "-"
152.195.94.170 - - [24/Sep/2017:01:12:24 +0000] "GET /static/img/favicon-192.png HTTP/1.1" 301 178 "https://example.com/comment/3" "Opera/9.80 (Android; Opera /24.0.2254/69.162; U; en) Presto/2.12.423 Version/12.16" "-"