在Debian系统上,Nginx的配置文件通常位于/etc/nginx/nginx.conf
。以下是一个基本的Nginx配置文件示例,包括一些常见的配置项:
# 用户和组设置
user www-data;
worker_processes auto;
# 错误日志路径
error_log /var/log/nginx/error.log warn;
# 运行时进程文件路径
pid /var/run/nginx.pid;
# 事件模块配置
events {
worker_connections 1024; # 每个工作进程的最大连接数
}
# HTTP服务器配置
http {
include /etc/nginx/mime.types; # MIME类型文件
default_type application/octet-stream; # 默认MIME类型
# 日志格式设置
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 访问日志路径
access_log /var/log/nginx/access.log main;
# 发送文件缓存设置
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# Gzip压缩设置
gzip on;
gzip_disable "msie6";
# 虚拟主机配置
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
# 安全设置
server_tokens off;
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}
用户和组设置:
user www-data;
:指定Nginx运行的用户和组。worker_processes auto;
:自动设置工作进程的数量。错误日志:
error_log /var/log/nginx/error.log warn;
:指定错误日志的路径和日志级别。运行时进程文件:
pid /var/run/nginx.pid;
:指定PID文件的路径。事件模块:
events
块中的worker_connections
设置每个工作进程的最大连接数。HTTP服务器配置:
include /etc/nginx/mime.types;
:包含MIME类型文件。default_type application/octet-stream;
:设置默认的MIME类型。log_format main
:定义日志格式。access_log /var/log/nginx/access.log main;
:指定访问日志的路径和格式。sendfile on;
:启用高效文件传输。tcp_nopush on;
:优化TCP推送。tcp_nodelay on;
:禁用Nagle算法。keepalive_timeout 65;
:设置长连接超时时间。gzip on;
:启用Gzip压缩。gzip_disable "msie6";
:禁用对IE6的Gzip压缩。虚拟主机配置:
include /etc/nginx/conf.d/*.conf;
:包含/etc/nginx/conf.d/
目录下的所有配置文件。include /etc/nginx/sites-enabled/*;
:包含/etc/nginx/sites-enabled/
目录下的所有配置文件。在/etc/nginx/sites-available/
目录下创建一个新的配置文件,例如example.com.conf
:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
}
创建一个符号链接以启用该虚拟主机配置:
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
在重新加载Nginx之前,测试配置文件是否有语法错误:
sudo nginx -t
如果没有错误,重新加载Nginx以应用更改:
sudo systemctl reload nginx
这样,你就完成了一个基本的Nginx配置文件的编写和部署。根据实际需求,你可以进一步调整和扩展配置。