0
0
Nginxdevops~5 mins

Performance bottleneck identification in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When a website or app feels slow, it often means something is blocking fast responses. Nginx can help find these slow spots by showing detailed logs and stats. This helps fix the problem and make the site faster.
When your website loads slowly and you want to find what part is causing delays
When you want to check if Nginx is handling requests efficiently or if it is overloaded
When you want to see detailed timing of requests to find slow backend services
When you want to monitor how many requests are active and how long they take
When you want to log errors or slow requests to improve your server setup
Config File - nginx.conf
nginx.conf
worker_processes auto;
events {
    worker_connections 1024;
}
http {
    log_format timed_combined '$remote_addr - $remote_user [$time_local] "$request" '
                              '$status $body_bytes_sent "$http_referer" '
                              '"$http_user_agent" $request_time $upstream_response_time';

    access_log /var/log/nginx/access.log timed_combined;

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://localhost:8080;
            proxy_connect_timeout 5s;
            proxy_read_timeout 10s;
        }
    }
}

worker_processes: Sets number of worker processes for handling requests.

log_format timed_combined: Custom log format that adds request_time and upstream_response_time to measure how long requests take.

access_log: Enables logging with the custom format to track request timings.

server block: Defines a simple proxy setup to forward requests to a backend server, with timeouts to avoid hanging.

Commands
Check if the Nginx configuration file is valid before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
View the last 10 lines of the access log to see recent request timings and status codes.
Terminal
tail -n 10 /var/log/nginx/access.log
Expected OutputExpected
192.168.1.10 - - [27/Apr/2024:14:22:10 +0000] "GET /index.html HTTP/1.1" 200 1024 "-" "Mozilla/5.0" 0.123 0.120 192.168.1.11 - - [27/Apr/2024:14:22:11 +0000] "GET /api/data HTTP/1.1" 504 0 "-" "curl/7.68.0" 10.001 - 192.168.1.12 - - [27/Apr/2024:14:22:12 +0000] "POST /submit HTTP/1.1" 200 512 "-" "Mozilla/5.0" 0.456 0.450
-n 10 - Show last 10 lines of the log
Check Nginx version and compile options to ensure modules for performance monitoring are enabled.
Terminal
sudo nginx -V
Expected OutputExpected
nginx version: nginx/1.24.0 built by gcc 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04) configure arguments: --with-http_ssl_module --with-http_v2_module --with-stream --with-http_stub_status_module
Key Concept

If you remember nothing else from this pattern, remember: detailed request timing logs help pinpoint exactly where delays happen in Nginx.

Common Mistakes
Not enabling detailed timing in the log format
Without request_time and upstream_response_time, you cannot see how long requests or backend calls take, missing the bottleneck.
Add $request_time and $upstream_response_time variables to the log_format to capture timing.
Reloading Nginx without testing configuration first
If the config has errors, Nginx will fail to reload and cause downtime.
Always run 'nginx -t' to test config before reloading.
Ignoring proxy timeouts in server config
Without timeouts, slow backend responses can hang requests and block Nginx workers.
Set proxy_connect_timeout and proxy_read_timeout to reasonable values.
Summary
Configure Nginx to log detailed request and upstream response times.
Test and reload Nginx configuration safely to apply changes.
Use access logs to identify slow requests and backend delays.
Check Nginx version and modules to ensure monitoring features are available.