|
user www-data; |
|
worker_processes auto; |
|
worker_cpu_affinity auto; |
|
# Change the default thread pool settings |
|
thread_pool default threads=16 max_queue=65536; |
|
# The max amount of open files (FDs) per worker process |
|
# (set at least twice the value of worker_connections) |
|
worker_rlimit_nofile 12000; |
|
pid /run/nginx.pid; |
|
include /etc/nginx/modules-enabled/*.conf; |
|
|
|
events { |
|
# Determines how much clients can be served per worker process |
|
# max clients = worker_connections * worker_processes |
|
worker_connections 4096; |
|
# Optimized to serve many clients with each thread, for linux only |
|
use epoll; |
|
# Accept as many connections as possible |
|
multi_accept on; |
|
} |
|
|
|
http { |
|
## |
|
# Basic Settings |
|
## |
|
|
|
# Copies data between one FD and other from within the kernel |
|
# faster than read() + write() |
|
sendfile on; |
|
# Use the default thread pool for asynchronous file I/O |
|
aio threads; |
|
# Only use AIO is used for when larger than or equal to this size |
|
directio 6m; |
|
# Send headers in one piece, it is better than sending them one by one |
|
tcp_nopush on; |
|
# Don't buffer data sent, good for small data bursts in real time |
|
tcp_nodelay on; |
|
# For security reasons don't send Nginx version in error messages or response headers |
|
server_tokens off; |
|
# Disable logging if a file can't be found |
|
log_not_found off; |
|
# Server will close connection after this time |
|
keepalive_timeout 65s; |
|
# Max. allowed requests on a single keepalive connection |
|
keepalive_requests 1000; |
|
# Max size of types hash tables (processing static sets of data. eg. server names, map directives or mime types) |
|
types_hash_max_size 2048; |
|
# Set bicket size of the hash tables |
|
types_hash_bucket_size 64; |
|
# Max allowed size of the client request body |
|
client_max_body_size 250M; |
|
# If the request body size is more than the buffer size, then the entire (or partial) |
|
# request body is written into a temporary file |
|
client_body_buffer_size 512k; |
|
# Request timed out |
|
client_body_timeout 300s; |
|
# Allow the server to close connection on non responding client, this will free up memory |
|
reset_timedout_connection on; |
|
# Increase max number of concurrenty HTTP/3 request streams in a connection |
|
http3_max_concurrent_streams 1024; |
|
# Increase the size of the buffer used for reading and writing of the QUIC streams |
|
http3_stream_buffer_size 1024k; |
|
# Enable sending in optimized batch mode using segmentation offloading |
|
quic_gso on; |
|
# Enable QUIC Address Validation feature |
|
quic_retry on; |
|
|
|
# Do not turn off request buffering (proxy_buffering), directly pass it to the server without caching |
|
# See also: https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#proxy_buffering-off |
|
# Buffer the response from the backend server, which contains the headers. |
|
proxy_buffer_size 32k; |
|
# Buffer size of the response to the client while the response is not yet fully read |
|
# proxy_buffer_size + 2x 4k buffers |
|
proxy_busy_buffers_size 32k; |
|
# xk = 128 times 16k buffering response (16k for the headers, 496k for the body response) |
|
proxy_buffers 128 16k; |
|
|
|
include /etc/nginx/mime.types; |
|
default_type application/octet-stream; |
|
|
|
# DNS |
|
resolver 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s ipv6=off; |
|
resolver_timeout 4s; |
|
|
|
## |
|
# Rate limiting |
|
## |
|
# Whitelist of rate limit (incl. my external IP address) |
|
geo $limit { |
|
default 1; |
|
77.61.56.117 0; |
|
127.0.0.1 0; |
|
192.168.1.0/24 0; |
|
} |
|
map $limit $limit_key { |
|
0 ""; |
|
1 $binary_remote_addr; |
|
} |
|
# Two stage rate limit (10 MB zone): 2 requests/sec limit (=second stage) |
|
limit_req_zone $limit_key zone=ip:10m rate=2r/s; |
|
# Dedicated limit for Synapse server |
|
limit_req_zone $limit_key zone=matrix_limit:10m rate=5r/s; |
|
# First stage (burst) can be found in the individual virtual server configs |
|
|
|
## |
|
# Logging Settings |
|
## |
|
# Discard 2xx or 3xx responses from logging |
|
# Fail2Ban rate limit filter still works, because it checks on Nginx error log |
|
map $status $loggable { |
|
~^[23] 0; |
|
default 1; |
|
} |
|
log_format timed_combined '$remote_addr - $remote_user [$time_local] ' |
|
'"$request" $status $body_bytes_sent ' |
|
'"$http_referer" "$http_user_agent" "$host" $request_time'; |
|
|
|
access_log /var/log/nginx/access.log timed_combined if=$loggable; |
|
# Show warn, error, crit, alert and emerg messages |
|
error_log /var/log/nginx/error.log warn; |
|
|
|
## |
|
# Gzip Settings |
|
# Reduce the data that needs to be sent over network |
|
## |
|
gzip on; |
|
gzip_disable msie6; |
|
|
|
gzip_vary on; |
|
gzip_comp_level 5; |
|
gzip_min_length 256; |
|
gzip_buffers 16 8k; |
|
gzip_proxied any; |
|
gzip_types |
|
text/css |
|
text/plain |
|
text/javascript |
|
text/cache-manifest |
|
text/vcard |
|
text/vnd.rim.location.xloc |
|
text/vtt |
|
text/x-component |
|
text/x-cross-domain-policy |
|
application/javascript |
|
application/json |
|
application/x-javascript |
|
application/ld+json |
|
application/xml |
|
application/xml+rss |
|
application/xhtml+xml |
|
application/x-font-ttf |
|
application/x-font-opentype |
|
application/vnd.ms-fontobject |
|
application/manifest+json |
|
application/rss+xml |
|
application/atom_xml |
|
application/vnd.geo+json |
|
application/x-web-app-manifest+json |
|
image/svg+xml |
|
image/x-icon |
|
image/bmp |
|
font/opentype; |
|
|
|
## |
|
# Use upstream block for PHP |
|
# |
|
upstream php_backend { |
|
server unix:/var/run/php/php-fpm.sock; |
|
keepalive 8; |
|
} |
|
|
|
## |
|
# Virtual Host Configs |
|
## |
|
|
|
include /etc/nginx/conf.d/*.conf; |
|
include /etc/nginx/sites-enabled/*; |
|
} |