A
A
Alexander Chernykh2022-02-01 18:18:13
Nginx
Alexander Chernykh, 2022-02-01 18:18:13

How to tell nginx to write logs for one domain?

I'm trying here with the help of map to force nginx to write information to the log only on the news.tld domain, but it writes on two news.tld and blank.tld domains. Something is wrong in the config. How right?

http {
  map $http_referer $news_access_log {
  default 0;
  .news.tld 1;
  }
  log_format main
                '$remote_addr - $remote_user [$time_local] '
                '$http_host $host "$request" $status $bytes_sent '
                '"$http_referer" "$http_user_agent" '
                '"$gzip_ratio"';
...
}
server {
    listen 80;
    listen 443 ssl http2;
    server_name  news.tld blank.tld;
    access_log /var/log/nginx/news.access.log combined if=$news_access_log;
...
}


piece of log

xxxx - - [01/Feb/2022:17:02:05 +0200] "GET /themes/news/manifest.json HTTP/2.0" 200 527 " https://news.tld/taxation/pdv/73948 " " Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G955F) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/16.0 Chrome/92.0.4515.166 Mobile Safari/537.36"

yyyy - - [01/Feb/2022:17:02: 05 +0200] "GET /modules/clients/js/clients/clients.js HTTP/2.0" 200 14861 " https://blank.tld/catalogues/indexes/13 " "Mozilla/5.0 (iPhone; CPU iPhone OS 15_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Mobile/15E148 Safari/604.1"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
ky0, 2022-02-01
@ky0

You are trying to use map as an if, which is architecturally rather inelegant. Is it problematic for you to simply separate domain processing into two server blocks for some reason?

A
Alexander Karabanov, 2022-02-01
@karabanov

share server uncomme il faut

Just the opposite. The DRY principle does not apply well to Nginx - this is the official recommendation from the developer.
Make two server blocks each with its own domain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question