
本文共 11235 字,大约阅读时间需要 37 分钟。
上一节我们介绍了nginx.conf的配置文件的3个模块内容,分别是main,events,stream(请自行脑图我画的图–对就是猪脸的那个)。本节我们进行http模块的详细讲解。那在讲解之前我们在来回顾下那张美图。


1.HTTP
此模块设计到的内容比较多,也算是nginx中较为核心的模块,此模块可以嵌套多个server,配置代理,缓存、第三方模块等绝大多数功能和配置。日常处理相应配置中,此模块操作的内容最多,也最容易配置错误,所以在配置的时候,要仔细仔细在仔细。
这里我们以一个配置fastdfs的配置块作为讲解(完整的http模块配置内容):
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;tcp_nopush on;tcp_nodelay on;client_body_buffer_size 512k;proxy_connect_timeout 5;proxy_read_timeout 60;proxy_send_timeout 5;proxy_buffer_size 16k;proxy_buffers 4 64k;proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;proxy_temp_path /data/nginx/proxy_temp;proxy_cache_path /data/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;proxy_cache_path /data/nginx/proxy_cache_image levels=1:2 keys_zone=content_image:20m inactive=1d max_size=100m;proxy_cache_bypass $http_secret_header;gzip on;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_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript;gzip_vary on; #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"' # '"$upstream_cache_status" "$http_host"'; log_format main '$remote_addr|$remote_user|[$time_local]|"$request"|' '$status|$body_bytes_sent|$http_host|"$http_referer"|' '"$http_user_agent"|$http_x_forwarded_for|$upstream_cache_status|' '"$upstream_http_content_type"|$request_time|$upstream_response_time|$bytes_sent|$request_length|' '"$upstream_addr"|' '"$gzip_ratio"'; #gzip on; server { listen 80; server_name fastdfs.skyfans.top; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; }location /group1/M00 { root /data/fastdfs/storage_23001/data; ngx_fastdfs_module; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }include ./conf.d/*.conf; # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}
1.1 http主内容
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; sendfile on;tcp_nopush on;tcp_nodelay on;client_body_buffer_size 512k;proxy_connect_timeout 5;proxy_read_timeout 60;proxy_send_timeout 5;proxy_buffer_size 16k;proxy_buffers 4 64k;proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;proxy_temp_path /data/nginx/proxy_temp;proxy_cache_path /data/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;proxy_cache_path /data/nginx/proxy_cache_image levels=1:2 keys_zone=content_image:20m inactive=1d max_size=100m;proxy_cache_bypass $http_secret_header;gzip on;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_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript;gzip_vary on; #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"' # '"$upstream_cache_status" "$http_host"'; log_format main '$remote_addr|$remote_user|[$time_local]|"$request"|' '$status|$body_bytes_sent|$http_host|"$http_referer"|' '"$http_user_agent"|$http_x_forwarded_for|$upstream_cache_status|' '"$upstream_http_content_type"|$request_time|$upstream_response_time|$bytes_sent|$request_length|' '"$upstream_addr"|' '"$gzip_ratio"'; #gzip on;}
1.1.1 include mime.types;
include表示纳入mime.types文件的配置,也就是加载mime.types的配置内容。从图中我们不难看出,在nginx安装完毕后,在conf文件夹中就直接有mime.types文件。


我们先来看看这个文件的内容(内容有点多,这里我们截取部分内容进行展示)


1.1.2 default_type application/octet-stream;
默认文件类型,当浏览器访问到未定义的扩展名的内容的时候,就默认为下载该文件。
1.1.3 server_names_hash_bucket_size 128;
指令在服务器名称的哈希表中分配hash表的大小。默认情况下,此值取决于处理器缓存线的大小
保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。默认的值大小: server_names_hash_bucket_size 32/64/1281.1.4 client_header_buffer_size 32k;
来自客户端的请求头设置headerbuffer大小。对于绝大多数请求,1K的缓冲区大小已经完全足够。
但是,如果请求头中有一个大cookie,或者请求来自wap客户端,则头不能放在1K中,因此,请求头或请求头的行不完全位于该缓冲区nginx分配一个较大的缓冲区,可以使用指令large_client_header_buffers设置较大缓冲区的大小。1.1.5 large_client_header_buffers 4 32k;
从客户端请求读取的大标题分配缓冲区的最大数量和大小。请求行不能大于一个缓冲区的大小,如果客户端发送一个更大的头nginx返回错误“请求URI太大”(414)。请求的最长头行也不能超过一个缓冲区的大小,否则客户端将得到错误“Bad request”(400)。缓冲区只在需要时分开。
默认情况下,一个缓冲区的大小等于页面的大小,这取决于4K或8K的配置,如果在工作请求结束时连接转换为state keep alive,那么这些缓冲区将被释放。1.1.6 client_max_body_size 8m;
允许通过nginx上传文件的大小,如果大小大于给定的大小,则客户端将得到错误“请求实体太大”(413)的错误。
1.1.7 sendfile on;
开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
1.1.8 tcp_nopush on;
允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用。
1.1.9 tcp_nodelay on;
允许或禁止使用套接字选项。
1.1.10 client_body_buffer_size 512k;
客户端请求正文缓冲区大小的最大字节数。如果请求体大于缓冲区,则整个请求体或部分将写入临时文件。默认大小等于两页大小,具体取决于平台,大小为8K或16K。
1.1.11 proxy_connect_timeout 5;
nginx跟后端服务器连接超时时间(代理连接超时时间)。
1.1.12 proxy_read_timeout 60;
连接成功后等候后端服务器响应时间,也可以说是后端服务器处理请求的时间。
1.1.13 proxy_send_timeout 5;
后端服务器数据回传时间。
1.1.14 proxy_buffer_size 16k;
设置代理服务器(nginx)保存用户头信息的缓冲区大小
1.1.15 proxy_buffers 4 64k;
设置用于读取应答,从被代理服务器发来的的缓冲区数目和大小,默认情况也为分页大小
1.1.16 proxy_busy_buffers_size 128k;
高负荷下设置的缓冲大小。
内容太多,看着累挺!插张图片缓解下疲劳的眼睛。

1.1.17 proxy_temp_file_write_size 128k;
设定缓存文件夹大小,防止一个工作进程在传递文件时阻塞导致时间太长。
1.1.18 proxy_temp_path /data/nginx/proxy_temp;
为存储承载从代理服务器接收到的数据的临时文件定义目录,后面可以添加数字定义支持几级子目录结构。
1.1.19 proxy_cache_path /data/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
Nginx对访问过的内容在Nginx服务器本地建立副本,这样在一段时间内再次访问该数据,就不需要通过Nginx服务器再次向后端服务器发出请求,所以能够减少Nginx服务器与后端服务器之间的网络流量,减轻网络拥塞,同时还能减小数据传输延迟,提高访问速度。同时,当后端服务器宕机时,Nginx服务器上的副本资源还能够回应相关的用户请求,这样能够提高后端服务器的可用性。
1.1.20 proxy_cache_path /data/nginx/proxy_cache_image levels=1:2 keys_zone=content_image:20m inactive=1d max_size=100m;
配置nginx缓存图片内容。
1.1.21 proxy_cache_bypass $http_secret_header;
指定不使用缓存返回应答的条件,如果指定的变量中至少有一个为非空,或者不等于“0”,这个应答将不从缓存中返回。
1.1.22 gzip on;
开启gzip压缩输出,进行大文件压缩后在展示。
1.1.23 gzip_min_length 1k;
最小压缩文件大小。
1.1.24 gzip_buffers 4 16k;
压缩缓冲区。
1.1.25 gzip_http_version 1.0;
使用的压缩版本。
1.1.26 gzip_comp_level 2;
压缩等级。可配置等级1-9,1为最小的压缩最快 但是消耗cpu。
1.1.27 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript;
gzip压缩内容包含的文件类型。默认已经包含textml,可以不用写。
1.1.28 gzip_vary on;
启用应答头"Vary: Accept-Encoding"
1.1.29 sendfile on;
指定是否使用sendfile内容来调用来传输文件。
sendfile内容调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝。1.1.30 keepalive_timeout 65;
设置nginx http连接超时时间。
护眼模式开启!!!

输入日志模块(为了方便看,我把内容重新粘贴下来呢!)
log_format main '$remote_addr|$remote_user|[$time_local]|"$request"|' '$status|$body_bytes_sent|$http_host|"$http_referer"|' '"$http_user_agent"|$http_x_forwarded_for|$upstream_cache_status|' '"$upstream_http_content_type"|$request_time|$upstream_response_time|$bytes_sent|$request_length|' '"$upstream_addr"|' '"$gzip_ratio"';
通过如上内容,应用配置了一个名为日志格式化的规则,供我们查找排查问题是要,此内容一般都会输出在access_log的日志中。至于日志格式输出内,请按照实际项目需求进行设置。为了方便理解,我们采用明了的表格形式进行说明各个参数内容。
参数 | 作用说明 | 备注说明 |
---|---|---|
$remote_addr | 客户端地址 | 若将Nginx服务器作为Web服务器使用,在他的前面存在负载均衡设备时,通过此项将不能获取到客户端的真实IP地址了。 |
$remote_user | 客户端访问的用户名称 | |
$time_local | 访问时间和时区 | |
$request | 请求的URI和HTTP协议 | |
$status | HTTP请求状态 | |
$body_bytes_sent | 发送给客户端文件内容大小统计 | |
$http_host | 请求地址,即浏览器中访问的输入地址 | |
$http_referer | url跳转来源 | |
$http_user_agent | 用户终端浏览器等信息 | |
$http_x_forwarded_for | 获取 HTTP 请求端真实 IP地址 | |
$upstream_cache_status | 显示缓存的状态 | |
$upstream_http_content_type | 响应的内容格式获取 | |
$request_time | 相应时间 | |
$bytes_sent | :nginx返回给客户端的字节数,包括响应头和响应体。 | |
$upstream_response_time | 请求过程中,upstream所获取的响应时间 | |
$request_length | 请求的字节数统计(包括请求行、请求头和请求体) | |
$upstream_addr | 后台upstream的地址内容,即真正提供服务的主机地址 | |
$gzip_ratio | 获取到gzip的压缩比率指令 | 多用于调试使用,一般不进行配置 |
哇哇哇,感觉内容好多,好累!!!


发表评论
最新留言
关于作者
