Skip to main content

Compiling Nginx 1.29 on Linux with HTTP3/QUIC Support

Navigation bar

Generally applicable to Linux systems. Quickly set up your own nginx static website.


0. Prepare Resources Before Compilation​

## If you have a pre-compiled nginx, installing libpcre3 might be sufficient
apt update
apt install libpcre3 libpcre3-dev gcc make perl cmake

cd /usr/local/src/

## Download resources
wget https://nginx.org/download/nginx-1.29.4.tar.gz
wget https://zlib.net/zlib-1.3.1.tar.gz
# Note: The link for zlib-1.3.1 returns a 404. Please check the latest version on zlib.net
# wget https://github.com/openssl/openssl/releases/download/openssl-3.6.0/openssl-3.6.0.tar.gz
wget https://github.com/openssl/openssl/releases/download/openssl-3.0.15/openssl-3.0.15.tar.gz # Using 3.0.15 as a stable alternative
git clone --recursive https://github.com/google/ngx_brotli.git

## Extract
tar -zxvf nginx-1.29.4.tar.gz
tar -zxvf zlib-1.3.1.tar.gz # Replace with the actual downloaded version if different
tar -zxvf openssl-3.0.15.tar.gz # Replace with the actual downloaded version

Optionally Compile Brotli​

ngx_brotli is a Brotli compression module for Nginx. It allows the Nginx server to compress transmitted content using the Brotli algorithm, improving transfer efficiency and reducing bandwidth usage.

## Simple compilation of brotli
cd /usr/local/src/ngx_brotli/deps/brotli
rm -rf out && mkdir out && cd out
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
make -j$(nproc)
make install

1. Compile Nginx​

cd /usr/local/src/nginx-1.29.4
make clean
./configure \
--with-debug \
--with-pcre-jit \
--with-pcre \
--with-stream \
--with-mail \
--with-stream_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-threads \
--with-openssl-opt=enable-tls1_3 \
--prefix=/usr/local/nginx \
--with-zlib=../zlib-1.3.1 \ # Ensure this path points to your extracted zlib directory
--add-module=../ngx_brotli \
--with-openssl=../openssl-3.0.15 # Ensure this path points to your extracted openssl directory

make -j$(nproc)
make install

2. Configure systemctl​

vim /etc/systemd/system/nginx.service

[Unit]
Description=Nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/usr/local/nginx/sbin/nginx -s reload

PrivateTmp=true
Restart=on-failure
RestartSec=5s
PIDFile=/usr/local/nginx/nginx.pid

[Install]
WantedBy=multi-user.target

3. Optional: Modify Server Name Before Compilation​

  1. Modify the internal Nginx name: vim /usr/local/src/nginx-1.29.4/src/core/nginx.h
    ##define nginx_version      260000
##define NGINX_VERSION "26.2 (25C56)"
##define NGINX_VER "macOS/" NGINX_VERSION
  1. Modify the HTTP Response Header: vim /usr/local/src/nginx-1.29.4/src/http/ngx_http_header_filter_module.c
    static u_char ngx_http_server_string[] = "Server: macOS/ 26.2 (25C56)" CRLF;
  1. Modify the footer on error pages: vim /usr/local/src/nginx-1.29.4/src/http/ngx_http_special_response.c
    static u_char ngx_http_error_tail[] =
"<hr><center>macOS/26.2 (25C56)</center>" CRLF;

4. Configuration​

A configuration example I use personally.


Note the Changes:​

Changed ItemDefault ValueChanged ValueNotes
proxy_cache_pathnone/www/nginx_tempWeb cache, can be removed if not needed
include servernone/etc/nginx/*.confServer configuration files
include streamnone/etc/nginx/*.streamStream configuration files

If using this configuration, you need to create the corresponding directories and files:

nginx include basic configuration directory

Includes will be loaded from here.

mkdir -p /etc/nginx

conf configuration

It is recommended to use one file per server { ... } block. Adding a number prefix can help with priority.

touch /etc/nginx/01.default.conf

stream configuration (optional)

Add based on requirements. It is recommended to use one file per stream { ... } block.

## stream files placed here. Adding a number prefix can help with priority.
touch /etc/nginx/02.default.stream

proxy_cache_path cache directory

The proxy_cache_path directive specifies a directory. Its absence will cause an error. Remove the directive if caching is not needed.

## The proxy_cache_path directory will be created at: /www/nginx_temp
mkdir /www

Complete Configuration:​

vim /usr/local/nginx/conf/nginx.conf

user                                        root;
worker_processes auto;
worker_rlimit_nofile 65536; # {{ worker_connections * worker_processes + 1024 }};
pid /usr/local/nginx/logs/nginx.pid;

events{
use epoll;
multi_accept on; # Suitable for high concurrency
accept_mutex off; # Newer Linux kernels have resolved the thundering herd problem
worker_connections 8192; # As long as the CPU can handle it!
}

http{
include mime.types;
default_type application/octet-stream;
# default_type text/plain;
charset_types text/plain text/markdown; # Add types that need charset
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m; # Limit client upload file size
client_body_buffer_size 10m; # In-memory buffer size for a single request
sendfile on;
tcp_nopush on;
tcp_nodelay on;
index index.html;
charset utf-8;
server_tokens on; # Hides default version info (setting 'off' is common)

proxy_connect_timeout 3s;
proxy_read_timeout 10s;
proxy_send_timeout 30s;

keepalive_timeout 60;
keepalive_requests 1000;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 64 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

gzip on;
gzip_comp_level 6;
gzip_min_length 1k;
gzip_types text/plain text/css text/xml text/javascript text/x-component application/json application/javascript application/x-javascript application/xml application/xhtml+xml application/rss+xml application/atom+xml application/x-font-ttf application/vnd.ms-fontobject image/svg+xml image/x-icon font/opentype;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_vary on;
gzip_proxied any;

log_format log_json
'{"timestamp":"$time_iso8601",\n'
'"response_time": "$upstream_response_time",\n'
'"client_load_time": "$request_time",\n' # New field
'"remote_addr":"$remote_addr",\n'
'"http_x_forwarded_for":"$http_x_forwarded_for",\n'
'"request_status":"$request_method-$status-$http3-$upstream_cache_status",\n'
'"request_url":"$scheme://$host:$server_port$request_uri",\n'
'"referer":"$http_referer",\n'
'"user_agent":"$http_user_agent",\n'
'"http_expires":"$http_expires",\n'
'"http_Dnt":"$http_Dnt",\n'
'"http_Pragma":"$http_Pragma",\n'
'"http_Cache_Control":"$http_Cache_Control",\n'
'"http_Priority":"$http_Priority",\n'
'"http_authorization":"$http_authorization",\n'
# '"http_cookie":"$http_cookie",\n'
'"upstream_addr":"$upstream_addr",\n'
'"upstream_status":"$upstream_status"\n'
'},';
access_log /usr/local/nginx/logs/access.json log_json;

### Define cache path and settings
proxy_cache_key "$host$request_uri";
proxy_cache_path /www/nginx_temp
levels=2 keys_zone=nginx_temp:50m
max_size=80g inactive=365d
use_temp_path=off;

### Include configurations
include /etc/nginx/*.conf;
}

stream {
log_format proxy_json
'{"timestamp":"$time_iso8601",\n'
'"remote_addr":"$remote_addr",\n'
'"upstream_addr":"$upstream_addr",\n'
'"protocol_status":"$protocol$status",\n'
'"bytes_sent":"$bytes_sent",\n'
'"bytes_received":"$bytes_received",\n'
'"session_time":"$session_time",\n'
'"upstream_bytes_sent":"$upstream_bytes_sent",\n'
'"upstream_bytes_received":"$upstream_bytes_received",\n'
'"upstream_connect_time":"$upstream_connect_time"\n'
'},';

include /etc/nginx/*.stream;
# access_log /usr/local/nginx/logs/proxy.json proxy_json;
# open_log_file_cache off;
}

Configure Your First Website​

Create a website in the /www/static directory for testing:

mkdir -p /www/static
echo "My first website" > /www/static/index.html
#
vim /etc/nginx/01.default.conf
######## Static Server ########
server {
server_name myWebServer; ## No domain name, this is the default
listen 80;
listen [::]:80;

# Dynamic directory listing
location / {
root /www/static;
charset utf-8;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}

Access it directly via the server's IP address.

Be Aware of autoindex Directory Listing Security!​

Be Aware of autoindex Directory Listing Security!​

Be Aware of autoindex Directory Listing Security!​


Image


AI Translation Notice

This page was translated into English by AI and is provided for reference only. The original document is written in Chinese. If there is any discrepancy, the Chinese version prevails.

本页英文内容由 AI 自动翻译生成,仅供参考;原文为中文,如有差异请以中文原文为准。

查看中文原文 / View the Chinese original →