配置文件结构
整体结构概览
Nginx 配置文件采用层级结构,由指令(Directive)和块(Block)组成:
# 全局块 - 影响 Nginx 整体运行
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# events 块 - 影响 Nginx 与用户的网络连接
events {
worker_connections 1024;
use epoll;
}
# http 块 - HTTP 服务器相关配置
http {
# http 全局块
include /etc/nginx/mime.types;
default_type application/octet-stream;
# server 块 - 虚拟主机配置
server {
# server 全局块
listen 80;
server_name example.com;
# location 块 - URL 匹配配置
location / {
root /var/www/html;
index index.html;
}
location /api {
proxy_pass http://backend;
}
}
# 可以有多个 server 块
server {
listen 80;
server_name another.com;
# ...
}
}
# stream 块 - TCP/UDP 代理配置(可选)
stream {
server {
listen 3306;
proxy_pass mysql_cluster;
}
}
2026/3/20大约 12 分钟