Nginx的基本配置文件结构
2025-04-14 11:55:14
84
参考资料
Nginx的基本配置文件结构
Nginx基本配置文件结构示例:
主配置文件(nginx.conf)基本结构:
# 全局块
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# events块
events {
worker_connections 1024;
use epoll;
}
# http块
http {
# http全局配置
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 65;
# server块(虚拟主机配置)
server {
listen 80;
server_name example.com;
root /var/www/html;
location / {
index index.html;
}
location /api {
proxy_pass http://backend;
}
}
# 可包含其他配置文件
include /etc/nginx/conf.d/*.conf;
}关键配置部分说明:
全局块:配置影响nginx全局的指令
events块:配置影响nginx服务器与用户的网络连接
http块:可以嵌套多个server块,配置代理、缓存、日志等
server块:配置虚拟主机的相关参数
location块:配置请求的路由,处理特定URI
典型server配置示例:
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/nginx/ssl/example.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
location /static/ {
alias /var/www/static/;
expires 30d;
}
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8000;
}
} 
