Nginx 基础配置参数详解
以下是具体的配置代码示例:
user nfsnobody nfsnobody; #指定运行 Nginx 进程的用户和用户组
worker_processes 8; #设定启动的工作衍生进程数量
error_log /usr/local/web/nginx.0.8.15/nginx_error.log crit; #定义错误日志文件的存储路径及级别
pid /usr/local/web/nginx.0.8.15/nginx.pid; #指定存放主进程 PID 的文件路径
worker_rlimit_nofile 65535; #设置单个进程最大打开文件描述符的数量限制
events
{
use epoll; #指定使用的事件模型,Linux 2.6 及以上内核通常推荐使用 epoll
worker_connections 65535; #设定每个工作进程允许的最大并发连接数
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128; #设置服务器名称哈希表的桶大小
client_header_buffer_size 32k; #定义客户端请求头部数据的缓冲区大小
large_client_header_buffers 4 32k; #设定处理大型请求头部的最大缓冲区数量及大小
client_max_body_size 8m; #限制客户端单次请求的最大主体大小为 8MB
sendfile on; #启用高效文件传输模式
tcp_nopush on;
keepalive_timeout 60; #设置长连接的超时时间为 60 秒
tcp_nodelay on;
fastcgi_connect_timeout 300; #以下部分为 FastCGI 模块的相关配置
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on; #以下部分为 Gzip 压缩模块的配置
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server
{
listen 80; #指定服务监听的端口号
server_name www.test2.com; #定义绑定的主机名或域名
index index.html index.htm index.php; #设定默认访问的首页文件列表
root /usr/local/web/www/; #指定网站根目录的文件路径
error_page 404 403 http://admin.test.com/404.htm; #自定义 404 和 403 错误的跳转页面
location ~ .*\.(php|php5)?$ #匹配 PHP CGI 模块的请求
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location / #URL 重写相关的配置模块
{
index index.php;
if (!-f $request_filename){
rewrite (.*) /admin.php;
}
try_files $uri $uri/ /index.html;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#以下为日志模块配置,用于记录 Nginx 访问日志
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /usr/local/web/nginx.0.8.15/logs/access.log access;
}
}