最全面生产环境 HTTPS的 Nginx配置、详细参数解释
nginx配置
详解
1.events
user nginx;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
- user nginx;: 指定 Nginx 进程运行的用户。这里设置为 nginx 用户。
- worker_processes auto;: 指定 Nginx 使用的 worker 进程数。auto 表示根据 CPU 核心数自动设置。
- pid /run/nginx.pid;: 指定 Nginx 进程 ID 的存储路径。
- events { … }: 事件模型的配置块,用于配置 Nginx 处理连接的方式。
- worker_connections 1024;: 指定每个 worker 进程的最大并发连接数。
- multi_accept on;: 启用多个连接同时接受模式。
- use epoll;: 指定使用 epoll 事件驱动模型。
2.http
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;
error_log /var/log/nginx/error.log error;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# 配置文件引入
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
-
include /etc/nginx/mime.types;: 引入 MIME 类型配置文件,该文件包含了文件扩展名和对应的 MIME 类型。
-
default_type application/octet-stream;: 指定默认的 MIME 类型,如果无法从文件扩展名中确定类型,则使用 application/octet-stream。
-
log_format main ‘…’;: 定义日志格式,指定了日志记录的内容和格式。
-
access_log /var/log/nginx/access.log main;: 指定访问日志的路径和使用的日志格式。
-
error_log /var/log/nginx/error.log error;: 指定错误日志的路径和使用的日志级别。
-
sendfile on;: 启用零拷贝技术,提高文件传输性能。
-
tcp_nopush on;: 启用 TCP_NOPUSH 选项,减少网络传输的延迟。
-
tcp_nodelay on;: 启用 TCP_NODELAY 选项,减少网络传输的延迟。
-
keepalive_timeout 65;: 指定 keep-alive 连接的超时时间。
-
types_hash_max_size 2048;: 指定 MIME 类型哈希表的最大大小。
-
最后两行是引入其他配置文件的指令:
- include /etc/nginx/conf.d/*.conf;: 引入 /etc/nginx/conf.d/ 目录下的所有以 .conf 结尾的配置文件。
- include /etc/nginx/sites-enabled/*;: 引入 /etc/nginx/sites-enabled/ 目录下的所有配置文件。
3.server
http {
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
# SSL证书和私钥