nginx.conf 怎么配置
时间: 2024-12-10 13:16:31 浏览: 41
nginx.conf 是 Nginx 服务器的配置文件,用于设置服务器的各种参数和行为。以下是一个基本的 nginx.conf 配置示例:
```nginx
# 全局设置
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 日志设置
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Gzip 压缩
gzip on;
gzip_disable "msie6";
# 虚拟主机配置
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# 处理静态文件
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
# 错误页面
error_page 404 /404.html;
location = /404.html {
internal;
}
}
# 其他服务器配置
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
```
### 配置说明
1. **全局设置**:
- `user`: 设定 Nginx 工作进程的用户。
- `worker_processes`: 工作进程数,通常设置为 CPU 核心数。
- `pid`: 进程 ID 文件路径。
2. **events**:
- `worker_connections`: 每个工作进程的最大连接数。
3. **http**:
- `sendfile`: 启用或禁用 `sendfile()` 调用。
- `tcp_nopush`: 启用或禁用 TCP_NOPUSH 套接字选项。
- `tcp_nodelay`: 启用或禁用 TCP_NODELAY 套接字选项。
- `keepalive_timeout`: 保持连接超时时间。
- `types_hash_max_size`: 类型哈希表的最大大小。
4. **虚拟主机配置**:
- `listen`: 监听的端口。
- `server_name`: 服务器名称,可以是域名或 IP 地址。
- `root`: 网站根目录。
- `index`: 默认首页文件。
- `location`: URL 匹配规则。
- `try_files`: 尝试查找文件。
- `expires`: 缓存过期时间。
5. **错误页面**:
- `error_page`: 自定义错误页面。
- `location`: 错误页面的路径。
###
阅读全文
相关推荐






