Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx: Add $host to log

Tags:

nginx

How can I (or why can't I) add the $host variable to my nginx configuration file to see what domain (or subdomain) a client is requesting?

Current Format:

log_format   main '$remote_addr - $remote_user [$time_local]  $status '
                  '"$host" "$request" $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

Current Output:

10.7.2.104 - - [17/Feb/2017:14:31:15 -0600] "GET /crazystairs HTTP/1.1" 404 2327 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"

Desired Ouput:

    10.7.2.104 - - [17/Feb/2017:14:31:15 -0600] "GET example.com/crazystairs HTTP/1.1" 404 2327 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"

Notice the host in front of the request

like image 677
Neve12ende12 Avatar asked Oct 11 '25 09:10

Neve12ende12


1 Answers

I found the problem. You must declare main after setting the directory where the log will be saved for the main format to be used.

access_log  logs/crazystairs/portal/access.log main;
like image 184
Neve12ende12 Avatar answered Oct 16 '25 09:10

Neve12ende12



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!