ngx_http_core_module
是 Nginx 中的一个核心模块,它提供了许多基本的配置指令,用于控制 Nginx 的基本行为。这些指令涵盖了服务器监听的端口、虚拟主机的配置、错误页面的设置等方面。下面是 ngx_http_core_module
中一些常用配置指令的详细解释。
常用配置指令
1. server
定义一个虚拟主机(也称为服务器块)。每个 server
块可以包含多个指令,用于配置该虚拟主机的行为。
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html index.htm;
}
2. listen
指定 Nginx 监听的端口和地址。
- 语法:
listen [address:]port [default_server] [ssl] [http2] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [so_keepalive=time[:time[:time]]] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=time[:time[:time]]];
- 示例:
listen 80; # 监听 80 端口
listen 443 ssl; # 监听 443 端口,并启用 SSL
listen 192.168.1.1:80; # 监听特定 IP 地址的 80 端口
listen [::]:80 ipv6only=on; # 监听 IPv6 地址的 80 端口
3. server_name
指定虚拟主机的域名或 IP 地址。
- 语法:
server_name name ...;
- 示例:
server_name example.com www.example.com;
4. root
指定网站根目录的位置。
- 语法:
root path;
- 示例:
root /var/www/html;
5. index
指定默认的索引文件。
- 语法:
index file ...;
- 示例:
index index.html index.htm;
6. location
定义处理特定 URL 的规则。
- 语法:
location [ = | ~ | ~* | ^~ ] uri { ... }
- 示例:
location / {
root /var/www/html;
}
location /images/ {
alias /data/images/;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
7. error_page
指定处理特定 HTTP 状态码的错误页面。
- 语法:
error_page code ... [ = answer-code ] [=[response]] uri;
- 示例:
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
8. access_log
指定访问日志的路径和格式。
- 语法:
access_log path [format [buffer=size] [flush=time] [gzip[=level]] [if=condition]];
- 示例:
access_log /var/log/nginx/access.log main;
9. error_log
指定错误日志的路径和级别。
- 语法:
error_log path [debug | info | notice | warn | error | crit | alert | emerg];
- 示例:
error_log /var/log/nginx/error.log error;
10. sendfile
启用或禁用 sendfile 功能,用于高效地传输文件。
- 语法:
sendfile on | off;
- 示例:
sendfile on;
11. tcp_nopush
启用或禁用 TCP_NOPUSH 选项,用于优化 TCP 数据包的发送。
- 语法:
tcp_nopush on | off;
- 示例:
tcp_nopush on;
12. tcp_nodelay
启用或禁用 TCP_NODELAY 选项,用于禁用 Nagle 算法,减少延迟。
- 语法:
tcp_nodelay on | off;
- 示例:
tcp_nodelay on;
13. keepalive_timeout
设置客户端连接的超时时间。
- 语法:
keepalive_timeout time [header_time];
- 示例:
keepalive_timeout 65;
14. client_max_body_size
设置客户端请求的最大 body 大小。
- 语法:
client_max_body_size size;
- 示例:
client_max_body_size 10m;
示例配置
以下是一个完整的 Nginx 配置示例,展示了如何使用 ngx_http_core_module
中的一些常用指令:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_max_body_size 10m;
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
}
}