Nginx配置文件详解(5大必备配置文件)

Nginx是大型架构核心,下面我详解Nginx配置文件@mikechen

一、Nginx配置文件

Nginx 的主配置文件通常为 nginx.conf,其核心结构由若干个 指令(directive) 和 上下文(context) 构成。

配置文件整体呈层级化组织,常见包括:

Nginx配置文件详解(5大必备配置文件)-mikechen

main:全局配置,作用于整个 Nginx 进程;

events:用于配置网络连接相关参数;

http:用于 HTTP 服务配置,是最常用的部分;

server:表示一个虚拟主机;

location:用于匹配请求路径并定义具体处理规则。

这种分层结构使得 Nginx 的配置清晰而灵活,适合构建复杂的服务体系。

 

二、主流配置项详解

1. 全局配置

全局配置通常位于 nginx.conf 顶部,主要用于设置 Nginx 运行用户、进程数、日志路径等。

# ==================== Main 全局配置 ====================
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log warn;

例如:

user:指定运行用户;

worker_processes:设置工作进程数;

error_log:错误日志路径;

pid:进程文件路径。

其中,worker_processes auto; 是较为常见的写法,可让 Nginx 根据 CPU 核心数自动调整进程数量。

Nginx配置文件详解(5大必备配置文件)-mikechen

2. events 配置

events 模块主要控制连接层面的性能参数,例如:

# ==================== Events ====================
events {
    use epoll;
    worker_connections 10240;
    multi_accept on;
}

worker_connections:单个 worker 进程允许的最大连接数;

use:指定事件模型,如 epoll

multi_accept:是否尽可能一次接受多个连接。

对于高并发场景,合理设置 worker_connections 和事件模型尤为重要。

3. http 配置

http 是 Nginx 中最核心的模块之一,负责处理 HTTP 请求。常见配置包括:

# ==================== HTTP ====================
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # 性能优化
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # 日志
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" '
                    '$request_time $upstream_response_time';
    access_log /var/log/nginx/access.log main buffer=16k flush=5s;

include mime.types:引入 MIME 类型定义;

default_type:默认文件类型;

sendfile on;:启用高效文件传输;

keepalive_timeout:长连接超时时间;

gzip on;:开启压缩传输,减少带宽消耗。

此外,http 模块中还可定义多个 server,从而实现多个站点的部署。

4. server 配置

server 块代表一个虚拟主机,常用来指定监听端口、域名和站点根目录。例如:

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate     /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
        expires 30d;
        add_header Cache-Control "public";
    }

    # 安全头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
}

listen 80;:监听 80 端口;

server_name:绑定域名;

root:站点根目录;

index:默认首页文件。

通过多个 server 块,Nginx 可以在同一台服务器上托管多个网站。

5. location 配置

location 用于匹配 URI 路径,是 Nginx 配置中最灵活的部分之一。常见匹配方式包括:

location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }

精确匹配:location = /test

前缀匹配:location /images/

正则匹配:location ~ \.php$

在实际应用中,location 常用于静态资源访问、接口转发、缓存控制以及访问权限限制。

评论交流
    说说你的看法