Back to lessons

Hosting Operations

List Nginx Listen Directives

You need to see which Nginx server blocks claim ports before chasing DNS or app errors.

Command

grep -RInE '^[[:space:]]*listen[[:space:]]' fixtures/nginx/conf.d fixtures/nginx/sites-enabled

What changed

Nothing changes. The command searches Nginx config files for listen directives.

Danger

safe

When to use it

Use when a site answers on the wrong port, misses HTTPS, or has competing server blocks.

When not to use it

Do not assume a listen line proves the service is actually bound; check sockets separately when needed.

Undo or recovery

No undo needed because this command is read-only.

Expected output

Config filenames and line numbers containing listen directives.

demo script

Disposable terminal steps

  1. find fixtures/nginx -type f | sort
  2. grep -RInE '^[[:space:]]*listen[[:space:]]' fixtures/nginx/conf.d fixtures/nginx/sites-enabled
  3. grep -RInE 'default_server|ssl' fixtures/nginx/conf.d fixtures/nginx/sites-enabled

simulated output

What it looks like

disposable vessel
::fixture-ready::
$ find fixtures/nginx -type f | sort
fixtures/nginx/conf.d/admin.conf
fixtures/nginx/nginx.conf
fixtures/nginx/sites-enabled/example.conf
::exit-code::0
$ grep -RInE '^[[:space:]]*listen[[:space:]]' fixtures/nginx/conf.d fixtures/nginx/sites-enabled
fixtures/nginx/conf.d/admin.conf:2:    listen 127.0.0.1:8080;
fixtures/nginx/sites-enabled/example.conf:2:    listen 80 default_server;
fixtures/nginx/sites-enabled/example.conf:7:    listen 443 ssl;
::exit-code::0
$ grep -RInE 'default_server|ssl' fixtures/nginx/conf.d fixtures/nginx/sites-enabled
fixtures/nginx/sites-enabled/example.conf:2:    listen 80 default_server;
fixtures/nginx/sites-enabled/example.conf:7:    listen 443 ssl;
::exit-code::0

YouTube Short

Check what Nginx listens on.

Before blaming the app, list the listen directives. A missing 443 or duplicate default server can explain the outage.

LinkedIn hook

The site was configured, but the port was not.

Question: When Nginx serves the wrong site, do you check listen lines or server names first?

experiments

A/B tests to run

Metric: save_rate

A: The port was wrong.

B: List Nginx listen lines first.