Nginx学习笔记:配置
本文更新于2026-04-12,使用nginx 1.16。
- 模块
- 语法
- 配置项
- error_log
- events
- events.worker_connections
- http
- http.access_log
- http.client_max_body_size
- http.default_type
- http.gzip
- http.gzip_comp_level
- http.gzip_min_length
- http.include
- http.limit_req_zone
- http.log_format
- http.keepalive_requests
- http.keepalive_timeout
- http.proxy_http_version
- http.proxy_set_header
- http.sendfile
- http.server
- http.server.client_max_body_size
- http.server.error_page
- http.server.include
- http.server.listen
- http.server.location
- http.server.location.alias
- http.server.location.allow
- http.server.location.client_max_body_size
- http.server.location.default_type
- http.server.location.deny
- http.server.location.index
- http.server.location.limit_req
- http.server.location.limit_req_status
- http.server.location.proxy_pass
- http.server.location.proxy_read_timeout
- http.server.location.proxy_set_header
- http.server.location.rewrite
- http.server.location.root
- http.server.rewrite
- http.server.root
- http.server.server_name
- http.server.ssl_certificate
- http.server.ssl_certificate_key
- http.server.ssl_ciphers
- http.server.ssl_prefer_server_ciphers
- http.server.ssl_session_cache
- http.server.ssl_session_timeout
- http.server.valid_referers
- http.server_names_hash_bucket_size
- http.tcp_nodelay
- http.tcp_nopush
- http.types_hash_max_xize
- include
- pid
- user
- upstream
- upstream.keepalive
- worker_processes
- 示例
官方文档:http://nginx.org/en/docs/。
指令列表:https://nginx.org/en/docs/dirindex.html
配置文件为安装目录下的conf/nginx.conf。
模块
Nginx包含以下模块。各模块都有若干配置项。
- ngx_http_access_module:HTTP访问控制模块。
- ngx_http_core_module:HTTP核心模块。
- ngx_http_geo_module:HTTP地理位置模块。
- ngx_http_gzip_module:HTTP使用Gzip压缩。
- ngx_http_gzip_static_module:HTTP静态文件Gzip压缩模块。
- ngx_http_index_module:主页面模块。
- ngx_http_limit_req_module:HTTP请求限流模块。
- ngx_http_log_module:HTTP日志模块。
- ngx_http_proxy_module:HTTP代理模块。
- ngx_http_ssl_module:HTTPS模块。
- ngx_http_upstream_module:HTTP上游服务器模块。
- ngx_mail_ssl_module
语法
注释
使用#单行注释。
变量
配置中可使用以下变量(使用了变量可能会导致不能直接解析localhost):
- $N:正则表达式匹配时与第N个分组(以
()引起)匹配的内容,从0开始。 - $http_upgrade:Upgrade首部的值。
- $request_method:请求方法。
- $request_uri:从路径开始的请求URI。
- $invalid_referer:是否为无效的Referer头。
- $uri:已别解码的请求路径。
PCRE运算符
- =:等于。
- ^~:优先使用前缀匹配。
- ~:匹配正则表达式,区分大小写。
- ~*:匹配正则表达式,不区分大小写。
- !~:不匹配正则表达式,区分大小写。
- !~*:不匹配正则表达式,不区分大小写。
配置项
[]引起表示可选,大写字母需使用实际的配置值(值可使用''引起)。
error_log
错误日志文件路径。
error_log FILEPATH;
events
events.worker_connections
单个工作进程的最大连接数。
events {
worker_connections N;
}
http
HTTP配置。
http {
}
http.access_log
HTTP访问日志。
http {
access_log FILEPATH [buffer=SIZE] [flush=DURATION]
}
http.client_max_body_size
最大的请求实体大小。可使用k、m、g等表示。
http {
client_max_body_size SIZE;
}
http.default_type
HTTP响应的默认MIME类型。
http {
default_type MIME;
}
http.gzip
HTTP是否启用Gzip压缩。
http {
gzip on|off;
}
http.gzip_comp_level
HTTP的Gzip压缩等级。
http {
gzip_comp_level N;
}
http.gzip_min_length
HTTP使用Gzip压缩的最小长度,原始数据长度小于此则不使用压缩。可使用k、m、g等表示。
http {
gzip_min_length LENGTH;
}
http.include
HTTP模块包含的文件。可使用*进行模糊匹配,配置文件必需以.conf结尾。
http {
include FILEPATH;
}
http.limit_req_zone
HTTP请求限流域。
http {
limit_req_zone $uri zone=ZONE:SIZE rate=Nr/m;
}
http.log_format
HTTP日志格式。
http.keepalive_requests
HTTP客户端持久化连接可发送的请求数。
http {
keepalive_requests: N;
}
http.keepalive_timeout
HTTP客户端空闲的持久化连接的保活超时。默认为65秒。
http {
keepalive_timeout N;
}
http.proxy_http_version
HTTP代理客户端使用的版本号。默认为1.0。
http {
proxy_http_version 1.0|1.1|2;
}
http.proxy_set_header
HTTP代理客户端设置的请求头。
http {
proxy_set_header KEY VALUE;
}
http.sendfile
HTTP是否允许以零复制(操作系统内核从一个文件描述符像另一个文件描述符复制)方式传输文件。
http {
sendfile on|off;
}
http.server
HTTP服务。可指定多个。
http {
server {
}
}
http.server.client_max_body_size
HTTP服务最大的实体大小。可使用k、m、g等表示大小。
http {
server {
client_max_body_size SIZE;
}
}
http.server.error_page
HTTP错误状态码的页面。使用URLPATH重新匹配规则,返回对应的内容。
http {
server {
error_page STATUSCODE[ ...] URLPATH;
}
}
http.server.include
HTTP服务包含的文件。可使用*进行模糊匹配,配置文件必需以.conf结尾。
http {
server {
include FILEPATH;
}
}
http.server.listen
HTTP服务监听的端口。多个http.server的listen端口可以相同,但需使用不同的http.server.server_name进行区分。
http {
server {
listen [IP:]PORT [ssl] [http2];
}
}
http.server.location
URL路径前缀匹配规则。可指定多个。
http {
server {
location URLPATH_REGEXP {
}
}
}
URLPATH_REGEXP前面可使用PCRE运算符(以空格隔开)。
http.server.location.alias
静态文件别名。
http {
server {
location URLPATH_REGEXP {
alias FILEPATH;
}
}
}
当FILEPATH为文件时,假设原始请求的路径为/URLPATH,若URLPATH_REGEXP匹配/PATH,则使用FILEPATH文件内容作为响应。
当FILEPATH为目录时,假设原始请求的路径为/URLPATH/SUB_PATH,URLPATH_REGEXP匹配/URLPATH或/URLPATH/,则使用FILEPATH/SUB_PATH文件内容作为响应。
如FILEPATH不以/开头,则为安装目录下的相对路径。
http.server.location.allow
HTTP白名单。多个黑白名单规则自上而下读取。
http {
server {
location URLPATH_REGEXP {
allow all|IP;
}
}
}
http.server.location.client_max_body_size
匹配此URL路径的最大的请求实体大小。可使用k、m、g等表示大小。
http {
server {
location URLPATH_REGEXP {
client_max_body_size SIZE;
}
}
}
http.server.location.default_type
HTTP默认的响应Content-Type的MIME值。
http {
server {
location URLPATH_REGEXP {
default_type MIME;
}
}
}
http.server.location.deny
HTTP黑名单。多个黑白名单规则自上而下读取。
http {
server {
location URLPATH_REGEXP {
deny all|IP;
}
}
}
http.server.location.index
HTTP主页面。使用URLPATH重新匹配规则,返回对应的内容。
http {
server {
location URLPATH_REGEXP {
index URLPATH[ ...];
}
}
}
http.server.location.limit_req
HTTP请求限流。
http {
server {
location URLPATH_REGEXP {
limit_req zone=ZONE;
}
}
}
ZONE为http.limit_req_zone中定义的限流域。
http.server.location.limit_req_status
HTTP请求限流状态码。
http {
server {
location URLPATH_REGEXP {
limit_req_status STATUSCODE;
}
}
}
http.server.location.proxy_pass
反向代理请求的后端地址。
http {
server {
location URLPATH_REGEXP {
proxy_pass PROXY_URL;
}
}
}
PROXY_URL中可使用$request_uri。
假设原始请求的路径为/URLPATH/SUB_PATH,URLPATH_REGEXP匹配/URLPATH或/URLPATH/。若PROXY_URL以/结尾,则代理请求的路径为PROXY_URL/SUB_PATH;若PROXY_URL不以/结尾,则代理请求的路径为PROXY_URL/URLPATH/SUB_PATH。
http.server.location.proxy_read_timeout
反向代理从后端的读超时。可使用h、m、s等表示时长。
http {
server {
location URLPATH_REGEXP {
proxy_read_timeout TIMEOUT;
}
}
}
http.server.location.proxy_set_header
反向代理请求后端时的首部设置。可指定多个。
http {
server {
location URLPATH_REGEXP {
proxy_set_header HEADER VALUE;
}
}
}
VALUE可使用$http_upgrade。
http.server.location.rewrite
重定向的地址。
http {
server {
location URLPATH_REGEXP {
rewrite REQUEST_PATH_REGEXP REDIRECT_URL [last|break|redirect|permanent];
}
}
}
REDIRECT_URL可使用以下变量:
- $N
- $request_uri
http.server.location.root
HTTP静态文件根目录。
http {
server {
location URLPATH_REGEXP {
root DIR;
}
}
}
假设原始请求的路径为/URLPATH,则使用DIR/URLPATH文件内容作为响应。
如DIR不以/开头,则为安装目录下的相对路径。
http.server.rewrite
HTTP重定向。
http {
server {
rewrite REQUEST_PATH_REGEXP REDIRECT_URL [last|break|redirect|permanent];
}
}
REDIRECT_URL可使用以下变量:
- $N
- $request_uri
http.server.root
HTTP静态文件根目录。
http {
server {
root DIR;
}
}
假设原始请求的路径为/URLPATH,则使用DIR/URLPATH文件内容作为响应。
如DIR不以/开头,则为安装目录下的相对路径。
http.server.server_name
HTTP服务的虚拟主机名。请求的Host首部匹配该值。_等表示无效域名。
http {
server {
server_name HOST;
}
}
http.server.ssl_certificate
HTTPS证书PEM文件路径。
http {
server {
ssl_certificate CERT.PEM;
}
}
如CERT.PEM不以/开头,则为安装目录下conf目录的相对路径。
http.server.ssl_certificate_key
HTTPS证书KEY文件路径。
http {
server {
ssl_certificate_key CERT.KEY;
}
}
如CERT.KEY不以/开头,则为安装目录下conf目录的相对路径。
http.server.ssl_ciphers
HTTPS返回客户端支持的加密套件。
http {
server {
ssl_ciphers [!]CIPHER[:...];
}
}
以“:”分隔多个套件,“!”表示排除此套件。
http.server.ssl_prefer_server_ciphers
HTTPS是否优先使用服务器的加密套件。
http {
server {
ssl_prefer_server_ciphers on;
}
}
http.server.ssl_session_cache
HTTPS证书会话缓存的类型和大小。shared为所有工作进程共享缓存。
http {
server {
ssl_session_cache shared:SSL:1m;
}
}
http.server.ssl_session_timeout
HTTPS客户端重用会话的时间。
http {
server {
ssl_session_timeout 5m;
}
}
http.server.valid_referers
HTTP有效的Referer头。
http {
server {
valid_referers none|blocked|server_names|URL[ ...]
}
}
none为无Referer头;blocked为Referer头存在,但不以http://或https://开头(通常是因为被代理服务器修改过);server_names为与当前虚拟主机域名匹配;URL可以使用*模糊匹配。
http.server_names_hash_bucket_size
出现类似“nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32”的错误时,加大该值。
http {
server_names_hash_bucket_size N;
}
http.tcp_nodelay
HTTP对TCP协议是否使用Nagle算法,把小包组装成大包提高带宽利用率。
http {
tcp_nodelay on|off;
}
http.tcp_nopush
HTTP是否启用此功能防止网络阻塞。
http {
tcp_nopush on|off;
}
http.types_hash_max_xize
HTTP哈希表类型的最大大小。
http {
types_hash_max_size N;
}
include
包含的配置文件。可使用*进行模糊匹配,配置文件必需以.conf结尾。
include FILEPATH;
pid
进程ID文件路径。
pid FILEPATH;
user
运行工作进程的操作系统用户名。
user USER;
upstream
上游服务器。
upstream.keepalive
每个工作进程连接到上游服务器的最大空闲持久化连接数。
upstream {
keepalive N;
}
worker_processes
工作进程数。默认为1。
worker_processes N|auto;
示例
HTTP反向代理
http {
# Other configurations...
server {
listen 80;
server_name www.myweb.com;
location / {
proxy_pass http://localhost:81;
}
}
}
HTTPS反向代理
http {
# Other configurations...
server {
listen 443 ssl;
server_name www.myweb.com;
ssl_certificate /certificate/dir/cert.pem;
ssl_certificate_key /certificate/dir/key.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:81;
}
}
}
WebSocket反向代理
http {
# Other configurations...
server {
listen 80;
server_name www.myweb.com;
location /websocket {
proxy_pass http://localhost:81/websocket;
proxy_read_timeout 1h;
proxy_set_header Connection Upgrade;
proxy_set_header Upgrade $http_upgrade;
}
}
}
静态文件服务器
http {
# Other configurations...
server {
listen 80;
server_name www.myweb.com;
location / {
root html/static/dir/;
}
location /a.txt {
alias html/a.txt;
}
}
}
重定向
http {
server {
listen 80;
server_name www.myweb.com;
rewrite .* https://www.herweb.com$request_uri;
}
}
防盗链
http {
server {
location ~* \.(jpg|gif|png)$ {
valid_referers none blocked http://www.myweb.com/*;
if ($invalid_referer) {
return 404;
}
}
}
}
限流
http {
limit_req_zone $uri zone=api:10m rate=100r/m;
server {
listen 80;
server_name www.myweb.com;
location / {
proxy_pass http://localhost:81;
limit_req zone=api;
limit_req_status 503;
}
}
}

浙公网安备 33010602011771号