Nginx配置文件nginx.conf配置详解概述
针对新用户常遇到的基础疑问,现对Nginx核心配置文件nginx.conf的各项参数进行系统梳理与详解。
user nginx nginx ;
定义运行Nginx服务的用户及所属组。在Windows环境下无需指定此项。
worker_processes 8;
设定工作进程的数量。该数值应根据硬件资源进行调整,通常建议设置为CPU核心数或CPU数量的两倍。
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
指定错误日志文件的存储路径。
pid logs/nginx.pid;
指定PID文件(进程标识符)的存放位置。
worker_rlimit_nofile 204800;
设定单个进程能够打开的最大文件描述符数量上限。
该指令用于限制每个Nginx进程可打开的文件描述符总数。理论上应为系统最大文件数(ulimit -n)除以Nginx进程数,但由于请求分配并非绝对均匀,最佳实践是与ulimit -n的值保持一致。
在Linux 2.6内核环境下,若开启的文件描述符限制为65535,则worker_rlimit_nofile应相应设置为65535。
鉴于Nginx调度时请求分配到进程的均衡性有限,若设置值过小(如10240),当总并发量达到3-4万时,部分进程可能超出限制,从而导致返回502 Bad Gateway错误。
events
{
use epoll;
指定使用的I/O模型。对于Linux系统推荐使用epoll,FreeBSD系统建议使用kqueue,Windows下则无需特别指定。
补充说明:
与Apache类似,Nginx针对不同操作系统提供了不同的事件处理模型。
A)标准事件模型
Select和poll属于标准事件模型。当当前系统不存在更高效的方法时,Nginx会自动选择select或poll。
B)高效事件模型
Kqueue:适用于FreeBSD 4.1+、OpenBSD 2.9+、NetBSD 2.0以及MacOS X。需注意,在使用双处理器的MacOS X系统上采用kqueue可能导致内核崩溃。
Epoll:适用于Linux内核2.6版本及之后的系统。
/dev/poll:适用于Solaris 7 11/99+、HP/UX 11.22+ (eventport)、IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
Eventport:适用于Solaris 10。为防止内核崩溃问题,建议安装相关安全补丁。
worker_connections 204800;
设定每个工作进程的最大连接数。需根据硬件条件调整,并与工作进程数配合使用,尽量设大但避免使CPU负载达到100%。每台Nginx服务器的理论最大连接数为 worker_processes * worker_connections。
keepalive_timeout 60;
设定Keepalive连接的超时时间。
client_header_buffer_size 4k;
设定客户端请求头部的缓冲区大小。可根据操作系统的分页大小进行设置,一般请求头部不超过1k,但因系统分页通常大于1k,故此处设为分页大小。
可通过命令 getconf PAGESIZE 获取系统的分页大小。
[root@web001 ~]# getconf PAGESIZE
4096
尽管存在client_header_buffer_size超过4k的情况,但该值必须设置为“系统分页大小”的整数倍。
此指令用于为打开的文件指定缓存,默认未启用。max参数指定缓存数量,建议与打开文件数一致;inactive参数指定文件多久未被请求后从缓存中删除。
open_file_cache_valid 80s;
指定检查缓存有效信息的时间间隔。
open_file_cache_min_uses 1;
open_file_cache指令中的inactive参数定义了时间内文件的最少使用次数。若超过此数字,文件描述符将保持打开状态。例如,若某文件在inactive时间内一次未被使用,它将被移除。
## 设定http服务器,利用其反向代理功能提供负载均衡支持
http
{
include mime.types;
设定MIME类型,具体类型由mime.type文件定义。
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';
日志格式设置。
$remote_addr与$http_x_forwarded_for用于记录客户端IP地址;
$remote_user:用于记录客户端用户名;
$time_local:用于记录访问时间与时区;
$request:用于记录请求的URL与HTTP协议;
$status:用于记录请求状态;成功时为200,
$body_bytes_sent :记录发送给客户端的文件主体内容大小;
$http_referer:用于记录从哪个页面链接访问过来;
$http_user_agent:记录客户浏览器的相关信息;
通常Web服务器位于反向代理之后,无法直接获取客户IP,通过$remote_addr获取的是反向代理服务器的IP。反向代理服务器可在转发请求的HTTP头信息中增加x_forwarded_for字段,用以记录原始客户端IP及原请求服务器地址。
access_log logs/host.access.log main;
access_log logs/host.access.404.log log404;
在使用log_format指令设置日志格式后,需通过access_log指令指定日志文件的存放路径;
server_names_hash_bucket_size 128;
# 保存服务器名字的Hash表由server_names_hash_max_size和server_names_hash_bucket_size控制。hash bucket size总是等于hash表的大小,且是一级处理器缓存大小的倍数。减少内存存取次数可加速Hash表键值查找。若hash bucket size等于一级处理器缓存大小,最坏情况下内存查找次数为2次(第一次确定存储单元地址,第二次在存储单元中查找键值)。因此,若Nginx提示需要增大hash max size或hash bucket size,首要任务是增大前一个参数的大小。
client_header_buffer_size 4k;
客户端请求头部的缓冲区大小。可根据系统分页大小设置,一般请求头部不超过1k,但因系统分页通常大于1k,故设为分页大小。分页大小可用命令getconf PAGESIZE取得。
large_client_header_buffers 8 128k;
客户请求头缓冲大小。Nginx默认使用client_header_buffer_size读取Header值,如果
Header过大,它将使用large_client_header_buffers来读取。
open_file_cache max=102400 inactive=20s;
此指令用于指定是否启用缓存。
例:
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
open_file_cache_errors
语法: open_file_cache_errors on | off 默认值: open_file_cache_errors off 使用字段: http, server, location 此指令指定是否在搜索文件时记录Cache错误。
open_file_cache_min_uses
语法: open_file_cache_min_uses number 默认值: open_file_cache_min_uses 1 使用字段: http, server, location 此指令指定在open_file_cache指令无效的参数中,一定时间范围内可使用的最小文件数。若使用更大值,文件描述符在Cache中始终处于打开状态。
open_file_cache_valid
语法: open_file_cache_valid time 默认值: open_file_cache_valid 60 使用字段: http, server, location 此指令指定何时需要检查open_file_cache中缓存项目的有效信息。
client_max_body_size 300m;
设定通过Nginx上传文件的大小限制。
sendfile on;
sendfile指令指定Nginx是否调用sendfile函数(Zero Copy方式)输出文件。对于普通应用,必须设为on。若用于下载等磁盘IO重负载应用,可设为off,以平衡磁盘与网络IO处理速度,降低系统Uptime。
tcp_nopush on;
此选项允许或禁止使用Socket的TCP_CORK选项,仅在使用sendfile时生效。
proxy_connect_timeout 90;
后端服务器连接的超时时间,即发起握手等候响应的超时时间。
proxy_read_timeout 180;
连接成功后,等候后端服务器响应的时间,即进入后端排队等候处理(也可视为后端服务器处理请求的时间)。
proxy_send_timeout 180;
后端服务器数据回传时间,即在规定时间内后端服务器必须传完所有数据。
proxy_buffer_size 256k;
设置从被代理服务器读取的第一部分应答的缓冲区大小,通常包含一个小应答头。默认值为proxy_buffers指令指定的一个缓冲区大小,但可设置为更小。
proxy_buffers 4 256k;
设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认也为分页大小,根据不同操作系统可能是4k或8k。
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
设置在写入proxy_temp_path时数据的大小,以防止工作进程在传递文件时阻塞过久。
proxy_temp_path /data0/proxy_temp_dir;
proxy_temp_path和proxy_cache_path指定的路径必须在同一分区。
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
# 设置内存缓存空间大小为200MB,1天未被访问的内容自动清除,硬盘缓存空间大小为30GB。
keepalive_timeout 120;
Keepalive超时时间。
tcp_nodelay on;
client_body_buffer_size 512k;
若将其设置为较大数值(如256k),无论使用Firefox还是IE浏览器,提交任意小于256k的图片均正常。若注释该指令,使用默认的client_body_buffer_size设置(即操作系统页面大小的两倍,8k或16k),则会出现问题。 无论使用Firefox 4.0还是IE 8.0,提交一个约200k的大图片,均会返回500 Internal Server Error错误。
proxy_intercept_errors on;
表示使Nginx阻止HTTP应答代码为400或更高的应答。
upstream bakend {
server 127.0.0.1:8027;
server 127.0.0.1:8028;
server 127.0.0.1:8029;
hash $request_uri;
}
Nginx的upstream目前支持4种方式的分配。
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,若后端服务器宕机,能自动剔除。
2、weight
指定轮询几率,weight与访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
2、ip_hash
每个请求按访问IP的Hash结果分配,使每个访客固定访问一个后端服务器,可解决Session问题。
例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
3、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
server server1;
server server2;
fair;
}
4、url_hash(第三方)
按访问URL的Hash结果来分配请求,使每个URL定向到同一个后端服务器,后端服务器为缓存时较为有效。
例:在upstream中加入Hash语句,server语句中不能写入weight等其他参数,hash_method为使用的Hash算法。
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
Tips:
upstream bakend{#定义负载均衡设备的Ip及设备状态}{
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载均衡的Server中增加
proxy_pass http://bakend/;
每个设备的状态设置为:
1.down表示当前的Server暂时不参与负载
2.weight越大,负载的权重就越大。
3.max_fails:允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream模块定义的错误。
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup:其他所有的非Backup机器Down或者忙的时候,请求Backup机器。因此这台机器压力最轻。
Nginx支持同时设置多组负载均衡,用于给不同的Server提供服务。
client_body_in_file_only设置为On可以将Client POST过来的数据记录到文件中用于Debug。 client_body_temp_path设置记录文件的目录,最多可设置3层目录。
Location对URL进行匹配,可进行重定向或新的代理、负载均衡。
## 配置虚拟机
server
{
listen 80;
配置监听端口
server_name image.***.com;
配置访问域名
location ~* \.(mp3|exe)$ {
对以“mp3或exe”结尾的地址进行负载均衡
proxy_pass http://img_relay$request_uri;
设置被代理服务器的端口或套接字,以及URL
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
以上三行,目的是将代理服务器收到的用户信息传到真实服务器上
location /face {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
error_page 404 502 = @fetch;
}
location @fetch {
access_log /data/logs/face.log log404;
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
location /image {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
error_page 404 502 = @fetch;
}
location @fetch {
access_log /data/logs/image.log log404;
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
}
## 其他举例
server
{
listen 80;
server_name *.***.com *.***.cn;
location ~* \.(mp3|exe)$ {
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#error_page 404 http://i1.***img.com/help/noimg.gif;
error_page 404 502 = @fetch;
}
location @fetch {
access_log /data/logs/baijiaqi.log log404;
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
}
server
{
listen 80;
server_name *.***img.com;
location ~* \.(mp3|exe)$ {
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#error_page 404 http://i1.***img.com/help/noimg.gif;
error_page 404 = @fetch;
}
#access_log off;
location @fetch {
access_log /data/logs/baijiaqi.log log404;
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
}
server
{
listen 8080;
server_name ngx-ha.***img.com;
location / {
stub_status on;
access_log off;
}
}
server {
listen 80;
server_name imgsrc1.***.net;
root html;
}
server {
listen 80;
server_name ***.com w.***.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.***.com/ ;
}
}
server {
listen 80;
server_name *******.com w.*******.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.*******.com/;
}
}
server {
listen 80;
server_name ******.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.******.com/;
}
}
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#设定查看Nginx状态的地址
location ~ /\.ht {
deny all;
}
#禁止访问.htxxx文件
}
注释:变量
Ngx_http_core_module模块支持内置变量,其名字与Apache的内置变量一致。
首先是说明客户请求Title中的行,例如$http_user_agent, $http_cookie等。
此外还有其他的一些变量
$args 此变量与请求行中的参数相等
$content_length 等于请求行的“Content_Length”的值。
$content_type 等同于请求头部的“Content_Type”的值
$document_root 等同于当前请求的root指令指定的值
$document_uri 与$uri一样
$host 与请求头部中“Host”行指定的值或是Request到达的Server的名字(没有Host行)一样
$limit_rate 允许限制的连接速率
$request_method 等同于Request的Method,通常是“GET”或“POST”
$remote_addr 客户端IP
$remote_port 客户端Port
$remote_user 等同于用户名,由ngx_http_auth_basic_module认证
$request_filename 当前请求的文件的路径名,由root或alias和URI Request组合而成
$request_body_file
$request_uri 含有参数的完整的初始URI
$query_string 与$args一样
$scheme HTTP模式(http, https)尽在要求是评估例如
Rewrite ^(.+)$ $scheme://example.com$; Redirect;
$server_protocol 等同于Request的协议,使用“HTTP/”或“HTTP/
$server_addr Request到达的Server的IP,一般获得此变量的值的目的是进行系统调用。为避免系统调用,有必要在listen指令中指明IP,并使用bind参数。
$server_name 请求到达的服务器名
$server_port 请求到达的服务器的端口号
$uri 等同于当前Request中的URI,可不同于初始值,例如内部重定向时或使用index
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。