Understanding Nginx Default Configuration and Log Formatting
Default Nginx Configuration
To examine the default configuration files for Nginx, inspect /etc/nginx/nginx.conf. The end of this file typically includes directives that load all .conf file from /etc/nginx/conf.d/.
Check the contents of /etc/nginx/conf.d/:
ls /etc/nginx/conf.d/
By default, two configuration files exist: nginx.conf and default.conf.
The primary sections of the nginx.conf file are:
Section One:
user: Sets the system user under which Nginx operatesworker_processes: Defines the number of worker processeserror_log: Specifies the path to the error logpid: Indicates where the process ID file is stored
Section Two (Events):
worker_connections: Sets the maximum number of connections per workeruse: Determines the event model used (e.g., epoll, select)
Section Three:
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
Detailed File Analysis
Below is an annotated version of a typical nginx.conf configuration:
# User and group for running Nginx
user www www;
# Number of worker processes, ideally matching CPU cores
worker_processes 8;
# Global error logging level
error_log /usr/local/nginx/logs/error.log info;
# Process identifier file
pid /usr/local/nginx/logs/nginx.pid;
# Maximum number of open file descriptors per worker
worker_rlimit_nofile 65535;
events {
# Event handling model (epoll recommended for Linux)
use epoll;
# Maximum connections per worker
worker_connections 65535;
# Keep-alive timeout
keepalive_timeout 60;
# Buffer size for client headers
client_header_buffer_size 4k;
# Open file cache settings
open_file_cache max=65535 inactive=60s;
open_file_cache_valid 80s;
open_file_cache_min_uses 1;
open_file_cache_errors on;
}
http {
# MIME types mapping
include /etc/nginx/mime.types;
# Default content type
default_type application/octet-stream;
# Hash table size for server names
server_names_hash_bucket_size 128;
# Client header buffer size
client_header_buffer_size 32k;
# Large client header buffers
large_client_header_buffers 4 64k;
# Maximum body size
client_max_body_size 8m;
# Enable sendfile for efficient file transfer
sendfile on;
# TCP optimizations
tcp_nopush on;
tcp_nodelay on;
# Keep-alive timeout
keepalive_timeout 120;
# FastCGI parameters
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
# Gzip compression
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
# Load balancing setup
upstream piao.jd.com {
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
}
Virtual Host Configuration
The virtual host configuration is located at /etc/nginx/conf.d/default.conf:
server {
listen 80;
server_name www.jd.com jd.com;
index index.html index.htm index.php;
root /data/www/jd;
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 10d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /usr/local/nginx/logs/host.access.log main;
access_log /usr/local/nginx/logs/host.access.404.log log404;
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_intercept_errors on;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file confpasswd;
}
location ~ .\.(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|
pdf|xls|mp3|wma)$ {
expires 15d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
}
Log Format Definitions
HTTP Request Structure
Nginx handles HTTP requests built upon TCP. A complete HTTP request consists of a request and resposne.
Request components: Request line, headers, and body.
Response components: Status line, headers, and body.
Viewing a full HTTP request via command line:
curl -v https://coding.imooc.com > /dev/null
The output shows the request (> part) and response (< part).
Nginx Log Types
error.log: Records errors related to HTTP requests and Nginx service issuesaccess_log: Logs each HTTP request processed
Log formatting uses log_format to combine variables into readable logs.
Example configuration:
Syntax: log_format name [escape=default|json] string …;
Default: log_format combined "...";
Context: http
Available Variables in Log Formats
- HTTP request variables like
arg_PARAMETER,http_HEADER,send_http_HEADER - Built-in variables provided by Nginx
- Custom-defined variables
Sample custom log format:
log_format custom '$remote_addr [$time_local] $request $status';
After saving changes, validate the configuration:
nginx -t -c /etc/nginx/nginx.conf
Then reload the service:
nginx -s reload -c /etc/nginx/nginx.conf
Testing the configuration:
curl 127.0.0.1
curl 127.0.0.1
Default log format variables:
$remote_addr: Client IP address$remote_user: Authenticated username$time_local: Time of request$request: Request line$status: Response status code$body_bytes_sent: Size of response body sent$http_referer: Referring page$http_user_agent: Browser agent string$http_x_forwarded_for: Forwarded IP addresses
Built-in Variables
$args: Query string from URL$content_length: Content-Length header$content_type: Content-Type header$document_root: Root directory of currrent request$host: Host header or server name$http_user_agent: User-Agent header$http_cookie: Cookie header$limit_rate: Connection rate limit$request_method: HTTP method (GET, POST)$remote_addr: Client IP address$remote_port: Client port$remote_user: Authenticated user$request_filename: Full path to requested file$scheme: Protocol (http, https)$server_protocol: HTTP protocol version$server_addr: Server IP address$server_name: Server name$server_port: Port number$request_uri: Original URI including query string$uri: Current URI without query string$document_uri: Same as$uri
Example with URL http://localhost:88/test1/test2/test.php:
$host: localhost$server_port: 88$request_uri: http://localhost:88/test1/test2/test.php$document_uri: /test1/test2/test.php$document_root: /var/www/html$request_filename: /var/www/html/test1/test2/test.php