nginx 配置简介
一、nginx 配置目录
-
nginx.conf:这是 Nginx 的主配置文件,包含了服务器全局设置以及如何运行的基本指令。这里定义了 worker 进程的数量、事件模型、错误日志的位置等关键参数。同时,它也会引入其他配置文件或包含特定上下文(如 http, server, location)的配置。
-
conf.d/:该目录用于存放额外的配置文件。这些文件可以被 nginx.conf 中的 include 指令加载。这种结构允许将不同的配置项分离到多个文件中,便于管理和维护。例如,你可以为每个虚拟主机创建一个单独的配置文件放在这个目录里,如果不存在这个目录可以自行创建。
-
mime.types:
定义了 MIME 类型映射,即文件扩展名与内容类型的对应关系。这对于正确地发送 HTTP 响应头至关重要,确保浏览器能够正确解析并显示资源。 -
fastcgi_params, uwsgi_params, scgi_params:
这些文件包含了 FastCGI、uWSGI 和 SCGI 协议所需的参数设置。当 Nginx 作为反向代理与后端应用程序服务器(如 PHP-FPM, uWSGI 应用程序)通信时会使用到它们。 -
win-utf 和 koi-utf(如果适用):
这些文件提供了字符编码转换表,主要用于处理非 UTF-8 编码的内容。不过,在现代 Web 开发中,UTF-8 已经成为标准,因此这些文件的使用频率较低。
二、nginx 命令行参数
Nginx 支持以下命令行参数,用于控制其行为和配置
2.1 命令行参数
-
信息与帮助
-? | -h:打印命令行参数的帮助信息。
-
配置文件相关
-
-c file:使用指定的替代配置文件,而不是默认的配置文件。 -
-e file:使用指定的替代错误日志文件存储日志,而不是默认的日志文件(从版本 1.19.5 开始)。特殊值stderr选择标准错误输出。 -
-g directives:设置全局配置指令。例如:nginx -g "pid /var/run/nginx.pid; worker_processes `sysctl -n hw.ncpu`;"
-
-
路径设置
-p prefix:设置 Nginx 的路径前缀,即存放服务器文件的目录(默认值为/usr/local/nginx)。
-
日志与测试
-
-q:在测试配置时抑制非错误消息。 -
-t:测试配置文件:检查配置文件的语法正确性,并尝试打开配置中引用的文件。 -
-T:与-t相同,但额外将配置文件内容输出到标准输出(从版本 1.9.2 开始)。
-
-
版本信息
-
-v:打印 Nginx 版本。 -
-V:打印 Nginx 版本、编译器版本以及配置参数。
-
-
发送信号
-s signal:向主进程发送信号。信号可以是以下之一:stop:快速关闭(不等待工作进程完成当前请求)。quit:优雅地关闭(等待所有工作进程处理完当前请求后再退出)。reload:重新加载配置文件,启动带有新配置的新工作进程,并优雅地关闭旧的工作进程。reopen:重新打开日志文件(通常用于日志轮转后重新指向新的日志文件)。
2.2 示例用法
-
启动 Nginx 并指定配置文件:
nginx -c /etc/nginx/custom_nginx.conf -
测试配置文件并抑制非错误消息:
nginx -t -q -
重新加载配置并重启工作进程:
nginx -s reload -
查看 Nginx 和编译器版本信息:
nginx -V
通过这些命令行参数,你可以灵活地管理 Nginx 的启动、配置测试、日志管理和进程控制等操作。这有助于确保 Nginx 按照预期的方式运行,并简化日常维护任务。
三、nginx 配置文件单位
nginx支持多种测量单位,用于在配置文件中指定大小、偏移和时间间隔。
3.1 大小和偏移量
在 Nginx 配置文件中,可以使用以下后缀来指定大小(以字节为单位)和偏移量:
- k 或 K:千字节 (KB),例如
8k表示 8192 字节。 - m 或 M:兆字节 (MB),例如
1m表示 1048576 字节。 - g 或 G:吉字节 (GB),例如
2g表示 2147483648 字节。
示例:
client_max_body_size 8m; # 最大上传文件大小为 8 MB
limit_rate 512k; # 下载速度限制为 512 KB/s
3.2 时间间隔
时间间隔可以使用以下后缀来指定:
- ms:毫秒,例如
500ms表示 0.5 秒。 - s:秒,默认单位,例如
60s表示 60 秒。 - m:分钟,例如
10m表示 10 分钟。 - h:小时,例如
2h表示 2 小时。 - d:天,例如
7d表示 7 天。 - w:周,例如
2w表示 2 周。 - M:月,表示 30 天,例如
1M表示 30 天。 - y:年,表示 365 天,例如
1y表示 365 天。
示例:
keepalive_timeout 75s; # 保持连接超时时间为 75 秒
proxy_read_timeout 1m; # 反向代理读取超时时间为 1 分钟
3.3 组合单位
多个单位可以在一个值中组合使用,按从最大到最小的顺序排列,并可选地用空格分隔。例如:
1h 30m表示 1 小时 30 分钟,等同于90m或5400s。2d 12h表示 2 天 12 小时,等同于60h或216000s。
示例:
fastcgi_read_timeout 1h 30m; # FastCGI 读取超时时间为 1 小时 30 分钟
注意事项
- 默认单位:如果没有指定后缀,默认单位是秒 (
s)。 - 推荐做法:建议总是指定后缀,以提高配置的清晰度和可维护性。
- 秒级分辨率:某些时间间隔只能以秒为单位指定。
{
user www-data;
worker_processes auto;
worker_cpu_affinity 0001 0010 0100 1000;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
worker_rlimit_nofile 10240;
load_module modules/ngx_http_perl_module.so;
env TZ;
daemon on;
master_process on;
timer_resolution 100ms;
}
四、nginx.conf 模块
4.1 nginx.conf 默认配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
4.1.1 main 全局块
包含影响整个 Nginx 服务的基本指令。这些指令适用于所有上下文,并且在所有其他上下文之前被解析
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
参数说明:
- user:用户设置,默认使用启动 Nginx 的用户。
- worker_processes:设置了工作进程数为 1。通常建议根据 CPU 核心数设置,或使用 auto 自动检测。
- error_log:默认情况下,Nginx 使用默认路径的日志文件。你可以通过取消注释并修改来指定不同的日志级别(如 info, notice, warn, error 等)
- pid: Nginx 主进程 PID 文件的位置
4.1.1 events 块
用于配置与连接处理相关的参数,特别是针对高并发场景下的性能优化。
events {
worker_connections 1024;
}
参数说明:
- worker_connections:每个工作进程的最大连接数设置为 1024,这决定了 Nginx 可以同时处理的最大客户端连接数。
4.1.2 http 块
包含所有 HTTP 服务的相关配置。这是配置文件的核心部分,可以包含多个 server 块以及其他上下文,例如 upstream、mail 等。
MIME 类型和支持的默认类型
http {
include mime.types;
default_type application/octet-stream;
参数说明:
- include:包含 MIME 类型定义文件,确保 Nginx 能正确识别和处理不同类型的文件
- default_type:设置默认的 MIME 类型为 application/octet-stream,即二进制流,如果找不到匹配的 MIME 类型,则返回此类型。
日志格式和访问日志
#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 logs/access.log main;
参数说明:
- log_format:定义了名为 main 的日志格式,记录客户端 IP 地址、请求时间、HTTP 请求行、状态码、发送的字节数、来源页面、用户代理和 X-Forwarded-For 头信息。
access_log:指定访问日志的位置和使用的日志格式(main)
性能优化
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
参数说明:
- sendfile:启用 sendfile 系统调用以提高文件传输效率,直接从磁盘读取数据到网络接口,而不经过用户空间缓冲区。
- tcp_nopush:TCP_NOPUSH 该选项可以减少网络延迟,确保所有数据包完整发送。
- keepalive_timeout:设置长连接的超时时间为 65 秒,允许浏览器保持与服务器的连接,减少每次请求的握手开销。
- gzip: 启用后可以压缩响应内容,减少传输的数据量,但会增加服务器 CPU 负载
4.1.3 server 块
每个 server 块代表一个虚拟主机或站点配置,可以有多个。它定义了监听的端口、服务器名称、根目录等信息
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
参数说明:
- listen:监听 80 端口,用于 HTTP 请求
- server_name:指定服务器名称为 localhost,可以是域名或 IP 地址
- location /:处理根路径请求,设置静态文件目录为 html,并指定索引文件为 index.html 和 index.htm。
- error_page:定义了 500、502、503 和 504 错误页面,并指向 /50x.html。
- location = /50x.html:精确匹配 /50x.html 请求,设置其根目录为 html,用于显示自定义的错误页面。
五、常用配置
5.1 常用配置模块目录
- ngx_http_core_module:包含核心的 HTTP 的参数配置,对应Nginx 的配置为 HTTP 的区块部分。
- ngx_http_access_module:访问控制模块,用来控制网站用户对 Nginx 的访问。
- ngx_http_auth_basic_module:Web 认证模块,设置 Web 用户通过账号密码访问 Nginx。
- ngx_http_gzip_module:压缩模块,对Nginx 返回的数据压缩,属于性能优化模块。
- ngx_http_fastcgi_module:FastCGI 模块,和动态应用相关的模块,如PHP。
- ngx_http_proxy_module:proxy 代理模块。
- ngx_http_upstream_module:负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查。
- ngx_http_rewrite_module:URL 地址重写模块。
- ngx_http_limit_conn_module:限制用户并发连接数及请求数模块。
- ngx_http_limit_req_module:根据定义的key限制 Nginx 请求过程的速率。
- ngx_http_log_module:访问日志模块,以指定的格式记录 Nginx 客户访问日志等消息。
- ngx_http_ssl_module:ssl 模块,用于加密的 HTTP 的连接,如 https。
- ngx_http_stub_status_module:记录 Nginx 基本访问状态信息等模块。
5666

被折叠的 条评论
为什么被折叠?



