1

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)

2
  • Please define "request port number". Commented Sep 13, 2022 at 19:07
  • When a request comes to the machine, it comes on a protocol and port number. e.g. a GET request comes on port 80 or 443. Commented Sep 13, 2022 at 19:50

1 Answer 1

3

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.

1
  • Thank you Tero, very complete and easy to follow. I wound up using both $server_port and $remote_port in the log. Commented Sep 13, 2022 at 22:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.