Skip to main content

Configuring HTTP Block

1. Setting MIME Types

Extension MIME Type Notes
.html text/html Standard HTML document
.css text/css Cascading Style Sheet
.js application/javascript JavaScript file
.json application/json JSON data
.xml application/xml XML document
.png image/png Portable Network Graphics image
.jpg image/jpeg JPEG image
.gif image/gif Graphics Interchange Format image
.svg image/svg+xml Scalable Vector Graphics
.mp4 video/mp4 MP4 video file
.mp3 audio/mpeg MPEG audio file
.pdf application/pdf Portable Document Format
.txt text/plain Plain text file
.ico image/x-icon Icon file format
.woff font/woff Web Open Font Format
.woff2 font/woff2 Web Open Font Format 2
.eot application/vnd.ms-fontobject Embedded OpenType font
.ttf font/truetype TrueType font
.otf font/opentype OpenType font
.map application/json Source map file

Example: Custom MIME Type

types {
    text/html html;
    image/jpeg jpg;
    application/json json;
    font/woff woff;
    font/woff2 woff2;
}

# Adding a custom MIME type
types {
    text/html html;
    image/jpeg jpg;
    application/json json;
    font/woff woff;
    font/woff2 woff2;
    application/vnd.ms-fontobject eot;
    font/truetype ttf;
    font/opentype otf;
    application/x-font-opentype otf;
}

2. Configuring Access Logging

Directive Values Description
access_log path [ format ] Defines the path to the access log file and optional format. Default is /var/log/nginx/access.log.
access_log off Disables access logging for the current context.

Example: Basic Access Log Configuration

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

Example: Custom Log Format

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

access_log /var/log/nginx/access.log main;
Note: The log_format directive must be defined before it is used in access_log.

Example: Using JSON Format for Logs

log_format json '{"time": "$time_iso8601", "remote_addr": "$remote_addr", "method": "$request_method", "uri": "$request_uri", "status": $status}';

access_log /var/log/nginx/access.json json;
Warning: Writing logs to disk can impact performance. Consider using a dedicated logging service or rotating logs.

3. Configuring Error Logging

Directive Values Description
error_log path [ level ] Specifies the file to which error logs are written and the logging level.
error_log stderr [ level ] Logs errors to the standard error stream.
error_log off Disables error logging.

Example: Basic Error Logging Configuration

error_log /var/log/nginx/error.log warn;

Example: Logging to Standard Error

error_log stderr notice;

Example: Disabling Error Logging

error_log off;
Log Level Description
debug Logs all messages, including debug information.
info Logs informational messages.
notice Logs normal but significant messages.
warn Logs warning messages.
error Logs error messages.
crit Logs critical error messages.
alert Logs alert messages (requires system log).
emerg Logs emergency messages (requires system log).
Note: The error_log directive can be specified at multiple levels (main, server, location), and each level overrides the previous one.

4. Setting Charset

Directive Values Description
charset utf-8 Sets the default character set for responses
charset_map utf-8, iso-8859-1 Maps character sets to MIME types
default_charset utf-8 Defines the default charset for content negotiation

Example: Setting Default Charset

server {
    listen 80;
    server_name example.com;

    location / {
        charset utf-8;
    }
}
Note: The charset directive affects the Content-Type header and can be overridden by individual location blocks.

5. Configuring Keepalive

Directive Values Description
keepalive_timeout 65s Timeout for keepalive connections. Default is 75s.
keepalive_requests 100 Maximum number of requests per keepalive connection. Default is 100.
keepalive_disable none Disables keepalive for specific user agents. Values can include msie6, safari, etc.

Example: Basic Keepalive Configuration

server {
    listen 80;
    server_name example.com;

    keepalive_timeout 65s;
    keepalive_requests 100;
}
Note: Setting keepalive_timeout to 0 disables keepalive connections entirely.

6. Setting Client Body Size

Directive Values Description
client_max_body_size 10m, 1024k, 1g Maximum allowed size of the client request body. Default is 1m.
client_body_buffer_size 128k, 256k Size of the buffer used to read client request body. Default is 128k.

Example: Setting client_max_body_size

http {
    client_max_body_size 50m;
}

Example: Setting client_body_buffer_size

server {
    client_body_buffer_size 256k;
}

7. Configuring Buffer Sizes

Directive Values Description
client_body_buffer_size 128k Size of the buffer for client request body
client_max_body_size 1m Maximum allowed size of the client request body
client_body_temp_file_write_size 1m Size of data written to a temporary file in one write operation
client_body_timeout 60s Timeout for reading client request body
client_header_buffer_size 1k Size of the buffer for reading client request headers
large_client_header_buffers 4 8k Number and size of buffers for large client request headers
lingering_time 5s Time to keep connection open after sending response
lingering_timeout 5s Timeout for lingering close of connections

Example: Setting buffer sizes for large uploads

http {
    client_max_body_size 100m;
    client_body_buffer_size 256k;
    client_header_buffer_size 2k;
    large_client_header_buffers 4 4k;
}
Note: Adjusting buffer sizes can impact performance and memory usage. Larger buffers may improve throughput for large requests but increase memory consumption.

8. Setting Timeouts

Directive Values Description
proxy_connect_timeout 60s Timeout for connecting to upstream server
proxy_send_timeout 60s Timeout for sending request to upstream server
proxy_read_timeout 60s Timeout for reading response from upstream server
proxy_busy_buffers_size 256k Size of buffer to use for busy upstream connections
client_body_timeout 60s Timeout for reading client request body
client_header_timeout 60s Timeout for reading client request header
keepalive_timeout 7s Timeout for keep-alive connections
lingering_timeout 5s Timeout for lingering connections

Example: Timeout Configuration

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
        proxy_connect_timeout 30s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;
        client_body_timeout 30s;
        client_header_timeout 30s;
    }
}

9. Configuring TCP Options

Directive Description Default Notes
tcp_nodelay Enables or disables the TCP_NODELAY option off When enabled, disables Nagle's algorithm to reduce latency
tcp_nopush Enables or disables the TCP_NOPUSH option off When enabled, delays sending packets to avoid small packets

Example: Enabling TCP optimizations

server {
    listen 80;
    tcp_nodelay on;
    tcp_nopush on;

    location / {
        proxy_pass http://backend;
    }
}

10. Setting Server Tokens

Directive Values Description
server_tokens on, off, master_only Controls whether the server version is included in error pages and responses.

Example: Disabling Server Tokens

server {
    listen 80;
    server_name example.com;

    # Hide server version in error pages and headers
    server_tokens off;
}
Note: Setting server_tokens off hides the Nginx version from error pages and HTTP headers, enhancing security by not exposing internal server details.

11. Configuring Default Server

Directive Description Example
listen Specifies the IP address and port to listen on. The default server is selected by the default_server parameter. listen 80 default_server;
server_name Defines the server name(s) for this default server. If no server_name matches the request, this server block is used. server_name example.com;

Example: Default Server Configuration

server {
    listen 80 default_server;
    server_name example.com;

    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

12. Setting Real IP

Directive Values Description
set_real_ip_from 192.168.1.0/24 Defines trusted proxy servers that can send real IP addresses
set_real_ip_from 10.0.0.0/8 Another subnet of trusted proxies
real_ip_header X-Forwarded-For Header field used to get the real IP address
real_ip_recursive on Enables recursive processing of headers for multiple proxies

Example: Basic real IP configuration

server {
    listen 80;
    server_name example.com;

    set_real_ip_from 192.168.1.0/24;
    set_real_ip_from 10.0.0.0/8;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;

    location / {
        proxy_pass http://backend;
    }
}

Was this article helpful?