0

Can I show the port # in the access/error logs with NGINX ? I can run nginx-debug if that helps.

Debian 11, free NGINX 1.22 downloaded release (not built from source)

gwhiz
  • 37
  • 5

1 Answers1

1

nginx variables documentation on variables shows that $server_port variable contains the server side port for the request.

nginx log module documentation documents log_format and access_log directives.

The default log format is:

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

One can add the port to the end for example for another log format:

log_format combinedwithport '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"' $server_port;

To actually use this format for logs, one needs to define:

access_log /path/to/log/file combinedwithport;

It is recommended to add the port to the end of log line so that possible combined log format parsers can still process the log.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • Thank you Tero, very complete and easy to follow. I wound up using both $server_port and $remote_port in the log. – gwhiz Sep 13 '22 at 22:27