Nginx 配置文件位置详解

Nginx 配置文件位置详解

在使用 Nginx 作为 Web 服务器时,了解配置文件的位置和结构非常重要。这篇文章将详细介绍 Nginx 配置文件的各个位置及其用途。

📁 配置文件位置总览

文件类型 路径 用途
主配置文件 /etc/nginx/nginx.conf Nginx 全局配置
站点配置(可用) /etc/nginx/sites-available/ 存放所有站点配置
站点配置(启用) /etc/nginx/sites-enabled/ 符号链接到启用的站点
SSL 证书配置 /etc/nginx/ssl/ SSL/TLS 证书文件
模块配置 /etc/nginx/modules-enabled/ 启用的模块
日志文件 /var/log/nginx/ 访问日志和错误日志

🔍 主配置文件:nginx.conf

这是 Nginx 的核心配置文件,位置:

1
/etc/nginx/nginx.conf

查看主配置

1
cat /etc/nginx/nginx.conf

主要配置项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
user www-data;              # 运行用户
worker_processes auto; # 工作进程数
pid /run/nginx.pid; # PID 文件位置

events {
worker_connections 768; # 最大连接数
}

http {
# 基本设置
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

# 日志格式
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

# Gzip 压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript;

# 包含其他配置文件
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

🌐 站点配置文件

sites-available 目录

存放所有可用的站点配置:

1
/etc/nginx/sites-available/

常见文件:

  • default - 默认站点配置
  • hexo-blog - 自定义站点配置(如你的博客)

sites-enabled 目录

存放已启用的站点配置(通常是符号链接):

1
/etc/nginx/sites-enabled/

查看站点配置

1
2
3
4
5
6
7
8
# 列出所有可用站点
ls -la /etc/nginx/sites-available/

# 列出已启用站点
ls -la /etc/nginx/sites-enabled/

# 查看具体配置
cat /etc/nginx/sites-available/hexo-blog

📄 我的博客配置示例

这是我的 Hexo 博客使用的 Nginx 配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 80;
listen [::]:80;

server_name _;

root /var/www/hexo-blog;
index index.html;

location / {
try_files $uri $uri/ =404;
}

# 缓存静态文件
location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}

# 禁止访问隐藏文件
location ~ /\. {
deny all;
}
}

配置说明

指令 说明
listen 80 监听 80 端口(HTTP)
server_name _ 匹配所有域名
root 网站根目录
index 默认首页文件
location / 根路径处理规则
try_files 尝试访问文件,不存在返回 404

🔧 常用管理命令

测试配置

1
sudo nginx -t

输出示例:

1
2
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

查看完整配置

1
sudo nginx -T

这会输出所有配置文件的完整内容。

重载配置

修改配置后,重载 Nginx:

1
sudo systemctl reload nginx

重启 Nginx

1
sudo systemctl restart nginx

检查状态

1
sudo systemctl status nginx

📝 添加新站点的步骤

1. 创建配置文件

1
sudo nano /etc/nginx/sites-available/my-site

2. 编写配置

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name example.com www.example.com;
root /var/www/my-site;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}

3. 启用站点

1
sudo ln -s /etc/nginx/sites-available/my-site /etc/nginx/sites-enabled/

4. 删除默认配置(可选)

1
sudo rm /etc/nginx/sites-enabled/default

5. 测试并重载

1
2
sudo nginx -t
sudo systemctl reload nginx

🗂️ 目录结构图

1
2
3
4
5
6
7
8
9
10
/etc/nginx/
├── nginx.conf # 主配置文件
├── sites-available/ # 可用站点配置
│ ├── default
│ └── hexo-blog
├── sites-enabled/ # 已启用站点(符号链接)
│ └── hexo-blog -> ../sites-available/hexo-blog
├── conf.d/ # 额外配置文件
├── modules-enabled/ # 已启用模块
└── modules-available/ # 可用模块

📊 日志文件位置

Nginx 日志对于排查问题非常重要:

1
2
3
4
5
6
7
8
# 访问日志
/var/log/nginx/access.log

# 错误日志
/var/log/nginx/error.log

# 实时查看日志
sudo tail -f /var/log/nginx/error.log

查看日志

1
2
3
4
5
6
7
8
# 查看最近 50 行
sudo tail -50 /var/log/nginx/access.log

# 实时跟踪日志
sudo tail -f /var/log/nginx/access.log

# 搜索特定内容
sudo grep "404" /var/log/nginx/access.log

⚠️ 常见问题

1. 配置修改后不生效

原因:没有重载 Nginx

解决

1
2
sudo nginx -t          # 先测试配置
sudo systemctl reload nginx

2. 权限错误

原因:Nginx 用户无法访问网站目录

解决

1
2
sudo chown -R www-data:www-data /var/www/hexo-blog
sudo chmod -R 755 /var/www/hexo-blog

3. 端口被占用

原因:80 端口已被其他程序占用

解决

1
2
sudo lsof -i :80
sudo netstat -tlnp | grep :80

4. 符号链接错误

原因:sites-enabled 中的链接不正确

解决

1
2
3
4
5
# 删除错误链接
sudo rm /etc/nginx/sites-enabled/wrong-config

# 重新创建正确链接
sudo ln -s /etc/nginx/sites-available/hexo-blog /etc/nginx/sites-enabled/

🔐 配置 HTTPS(可选)

使用 Let’s Encrypt 免费 SSL 证书:

安装 Certbot

1
sudo apt install certbot python3-certbot-nginx

获取证书

1
sudo certbot --nginx -d example.com -d www.example.com

配置会自动更新

Certbot 会自动修改 Nginx 配置,添加 HTTPS 支持。

自动续期

1
2
3
4
5
# 测试续期
sudo certbot renew --dry-run

# 设置定时任务(通常已自动配置)
sudo systemctl list-timers | grep certbot

💡 最佳实践

  1. 修改前先备份

    1
    sudo cp /etc/nginx/sites-available/hexo-blog /etc/nginx/sites-available/hexo-blog.bak
  2. 测试后再重载

    1
    sudo nginx -t && sudo systemctl reload nginx
  3. 使用有意义的文件名

    • hexo-blog
    • config1
  4. 定期检查日志

    1
    sudo tail -100 /var/log/nginx/error.log
  5. 保持配置简洁

    • 一个站点一个配置文件
    • 使用注释说明复杂配置

📚 相关资源


总结

Nginx 配置文件位置总结:

用途 文件路径
主配置 /etc/nginx/nginx.conf
站点配置 /etc/nginx/sites-available/
已启用站点 /etc/nginx/sites-enabled/
日志文件 /var/log/nginx/

记住这三个关键命令:

1
2
3
sudo nginx -t              # 测试配置
sudo systemctl reload nginx # 重载配置
sudo tail -f /var/log/nginx/error.log # 查看错误日志

希望这篇文章能帮助你更好地理解和管理 Nginx 配置!


Happy Configuring! 🔧