Nginx配置文件功能注释

最近在学配置Nginx做均衡负载,首先最重要的是Nginx的配置文件,参考网上查到的资料,并结合实践,

将该配置文件的详细功能注释在这里做下笔记,方便今后查阅。
 
官方的Nginx Wiki(英文)   http://wiki.nginx.org/Configuration
 
环境:FreeBSD8.0+Nginx1.0.10_1
配置文件位置:   /usr/local/etc/nginx(所有配置文件都在此文件夹)
可执行文件位置: /usr/local/sbin/nginx
服务所在位置:   /usr/local/etc/rc.d/nginx
 
启动:     /usr/local/etc/rc.d/nginx start
停止:     /usr/local/etc/rc.d/nginx stop
重启:     /usr/local/etc/rc.d/nginx restart
测试配置: /usr/local/etc/rc.d/nginx configtest
重载配置: /usr/local/etc/rc.d/nginx reload
服务状态: /usr/local/etc/rc.d/nginx status
 
主配置文件nginx.conf注释:
 
#运行服务的用户及用户组 
user www www;

#服务开启的进程数 
worker_processes 5;

#全局错误日志及PID文件 
error_log logs/error.log notice; 
pid logs/nginx.pid;

#限定允许打开的文件的文件描述符长度,包括配置文件、日志文件等,如不指定,则使用操作系统默认值worker_rlimit_nofile 8192; 
 
#工作模式及连接数上限 
events { 
#工作模式有:
select(标准模式), 
      poll(标准模式), 
kqueue(高效模式,适用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
epoll(高效模式,适用Linux 2.6+,SuSE 8.2,), 
        /dev/poll(高效模式,适用Solaris 7 11/99+,HP/UX 11.22+)
eventport(IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+) 
 use epoll;

 #单个进程允许的最大连接数,总连接数为:worker_processes*worker_connections   
 worker_connections 1024;
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持 
http { 
 #设定配置文件位置,这里的conf是指nginx.conf所在的目录,也可以用绝对路径指定其他地方的配置文件 
 include conf/mime.types; 
 include conf/fastcgi.conf; 
 
 #默认类型-8进制文件流 
 default_type application/octet-stream;

 #全局首页类型 
 index index.html index.htm index.php 
 
 #设定日志格式 
 log_format main 'remoteaddrremote_user [$time_local] ' 
         '"request"status $bytes_sent ' 
         '"httpreferer""http_user_agent" ' 
 '"$gzip_ratio"'; 

 log_format download 'remoteaddrremote_user [$time_local] ' 
 '"request"status $bytes_sent ' 
 '"httpreferer""http_user_agent" ' 
 '"httprange""sent_http_content_range"';

 #是否激活sendfile()函数,比默认模式更有效率 
 sendfile on;

 #将HTTP响应头压缩到一个包中发送,仅在sendfile开启时才能配合使用 
 tcp_nopush on;

 #指定保存服务器名称的hash表的大小,存在多个域名或虚拟主机时指定
 server_names_hash_bucket_size 128;

 #设定用户请求header缓冲区大小 
 client_header_buffer_size 10k; 
 large_client_header_buffers 4 4k;

 #开启gzip模块,要求安装gzip 在运行./config时要指定 
 gzip on; 
 gzip_min_length 1100; 
 gzip_buffers 4 8k; 
 gzip_types text/plain; 
 output_buffers 1 32k; 
 postpone_output 1460;
 
 #设定访问日志 
 access_log logs/access.log main; 
 client_header_timeout 3m; 
 client_body_timeout 3m; 
 send_timeout 3m; 
 sendfile on; 
 tcp_nopush on; 
 tcp_nodelay on; 
 keepalive_timeout 65;

 #设定负载均衡的服务器列表 
 upstream backserver { 
#weigth参数表示权值,权值越高被分配到的几率越大 
#本例是指在同一台服务器,多台服务器改变ip即可 
server 127.0.0.1:8081 weight=5; 
server 127.0.0.1:8082; 
server 127.0.0.1:8083;
 } 

 #设定虚拟主机,默认为监听80端口 
 server { 
 listen 80; 
 server_name test.com www.test.com
 charset utf8; 
 
 #设定本虚拟主机的访问日志 
 access_log logs/test.com.log main;

 #如果访问 /images/*, /js/*, /css/* 资源,则直接取本地文件,不用转发。
 #但如果文件较多效果不是太好。 
 location ~ ^/(images|js|css)/ { 
 root /usr/local/testweb; 
 expires 30m; 
 }

 #对 "/" 启用负载均衡 
 location / { 
 proxy_pass http://backserver
 proxy_redirect off; 
 proxy_set_header Host $host; 
 proxy_set_header X-Real-IP $remote_addr; 
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   client_max_body_size 10m; 
 client_body_buffer_size 128k; 
 proxy_connect_timeout 90; 
 proxy_send_timeout 90; 
 proxy_read_timeout 90; 
 proxy_buffer_size 4k; 
 proxy_buffers 4 32k; 
 proxy_busy_buffers_size 64k; 
 proxy_temp_file_write_size 64k; 
 }

 #设定查看Nginx状态的地址,在运行./config 要指定,默认是不安装的。 
 location /NginxStatus { 
 stub_status on; 
 access_log on; 
 auth_basic "NginxStatus"; 
 
 #是否要通过用户名和密码访问,测试时可以不加上。
 #conf/htpasswd 文件的内容用apache提供的htpasswd工具来产生即可 
 #auth_basic_user_file conf/htpasswd; 
 } 
}
posted @   joshua317  阅读(646)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示