You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: labs/lab5/readme.md
-123
Original file line number
Diff line number
Diff line change
@@ -607,129 +607,6 @@ Now that you have a server, location, and upstream block defined, you can add th
607
607
608
608
<br/>
609
609
610
-
### NGINX Extended Access Logging
611
-
612
-
<br/>
613
-
614
-
Now that you have a working NGINX Proxy, and several backends, you will be adding and using additional NGINX Directives, Variables, and testing them out. In order to better see the results of these new Directives on your proxied traffic, you need better and more information in your Access logs. The default NGINX `main` access log_format only contains a fraction of the information you need, so you will `extend` it to includemore information, especially about the Upstream backend servers.
615
-
616
-
1. In this next exercise, you will use a new `log_format` which has additional $variables added the access.log, so you can see this metadata. You will use the Best Practice of defining the log format ONCE, but potentially use it in many Server blocks.
617
-
618
-
1. Inspect the `main` access log_format that is the default when you install NGINX. You will find it in the `/etc/nginx/nginx.conf` file. As you can see, there is `nothing`in this log format about the Upstreams, Headers, or other details you need.
619
-
620
-
```nginx
621
-
...snip from /etc/nginx/nginx.conf
622
-
623
-
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
624
-
'$status $body_bytes_sent "$http_referer" '
625
-
'"$http_user_agent" "$http_x_forwarded_for"';
626
-
627
-
# nothing above on Upstreams, how do you even know which server handled the request ???
628
-
629
-
...
630
-
631
-
```
632
-
633
-
1. Inspect the `main_ext.conf` configuration file, located in the `/etc/nginx/includes/log_formats` folder. You will see there are many added $variables, some are for the request, some for proxy functions, some for the response, and several are about the Upstream (so you know which backend was chosen and used for a request). NGINX has `hundreds` of $variables you can use, depending on what you need to see.
1. You will use the Extended log_format for the next few exercises. Update your `cafe.example.com.conf` file within your mounted folder (`labs/lab4/nginx-plus/etc/nginx/conf.d`) to use the `main_ext` log format:
660
-
661
-
```nginx
662
-
# cars.example.com HTTP
663
-
# NGINX Basics Workshop
664
-
# Nov 2024, Chris Akker, Shouvik Dutta, Adam Currier
665
-
#
666
-
server {
667
-
668
-
listen 80;# Listening on port 80 on all IP addresses on this machine
669
-
670
-
server_name cafe.example.com;# Set hostname to match in request
671
-
status_zone cafe-VirtualServer;
672
-
673
-
access_log /var/log/nginx/cafe.example.com.log main_ext;# Change this to "main_ext"
1. Once the content of the file has been updated and saved, Docker Exec into the nginx-plus container.
681
-
682
-
```bash
683
-
docker exec -it nginx-plus bin/bash
684
-
685
-
```
686
-
687
-
1. Test and reload your NGINX config by running `nginx -t` and `nginx -s reload` commands respectively from within the container.
688
-
689
-
1. Test your new log format. Docker Exec into your nginx-plus container. Tail the `/var/log/nginx/cafe.example.com.log` access log file, and you will see the new Extended Log Format.
690
-
691
-
```bash
692
-
docker exec -it nginx-plus bin/bash
693
-
tail -f /var/log/nginx/cafe.example.com.log
694
-
695
-
```
696
-
697
-
1. While watching your new log format, use curl or your browser, and hit the `cafe.example.com` website a few times.
698
-
699
-
It should look something like this (comments and line breaks added for clarity):
700
-
701
-
```bash
702
-
##Sample output##
703
-
704
-
# Raw Output
705
-
remote_addr="192.168.65.1", [time_local=19/Feb/2024:19:14:32 +0000], request="GET / HTTP/1.1", status="200", http_referer="-", body_bytes_sent="651", Host="cafe.example.com", sn="cafe.example.com", request_time=0.001, http_user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36", http_x_forwarded_for="-", request_length="471", upstream_address="172.18.0.3:80", upstream_status="200", upstream_connect_time="0.000", upstream_header_time="0.000", upstream_response_time="0.000", upstream_response_length="651",
706
-
707
-
# Formatted output with Line Breaks to make it more readable
708
-
remote_addr="192.168.65.1",
709
-
[time_local=19/Feb/2024:19:14:32 +0000],
710
-
request="GET / HTTP/1.1", status="200",
711
-
http_referer="-",
712
-
body_bytes_sent="651",
713
-
Host="cafe.example.com",
714
-
sn="cafe.example.com",
715
-
request_time=0.001,
716
-
http_user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
717
-
http_x_forwarded_for="-",
718
-
request_length="471",
719
-
upstream_address="172.18.0.3:80", # Nice, now you know what backend was selected
As you can see here, NGINX has `many $variables` that you can use, to customize the Access log_format to meet your needs. You will find a link to NGINX Access Logging, and ALL the NGINX Variables that are availabe in the [References](#references) section below.
728
-
729
-
It should also be pointed out, that you can use different log formats for different Hosts, Server Blocks, or even different Location Blocks, you are not limited to just one log_format.
0 commit comments