Nginx模块功能也是相当的丰富,但对于Apache Web来说,两者之间还是有区别的,大家都知道Nginx模块是直接被编译进了nginx.conf配置文件,而Apache则是被编译成为.SO文件,有些是需要在httpd.conf配置文件中指定是否加载,这样才能激活模块功能。Nginx模块有三个角色,Handlers(处理一个HTTP请求,并产生输出);Filters(处理由一个handler生成的输出);load-balancers(负载均衡器,当后端服务器符合一个以上后,依据算法选择其中一台服务器处理一个HTTP请求),首先会依次介绍Nginx core module(核心模块),Standard HTTP modules(标准的HTTP模块),Optional HTTP module(可选的HTTP模块);至于第三方的Nginx模块,以后会详细的讲解。
无论你选择哪种安装方式,Nginx Core Module(Nginx核心模块),Standard Http Modules(标准的HTTP模块)默认都会被安装,其中Nginx核心模块是必须被安装的,而标准的HTTP模块则可以通过—without关闭相应的模块功能,对于Optional HTTP module(可选的HTTP模块),默认则不会被编译,需要指定相应的参数,激活模块的功能,例如:--with-http_sub_module(Nginx状态功能模块),当一个模块被安装以后,都会附加一些模块指令提供相应的服务,接下来介绍Nginx Core Module一些常用的模块指令与参数。
http://www.evanmiller.org/nginx-modules-guide.html
root
##设置网站请求的根目录;
Server
##设置虚拟服务器配置,可以基于IP地址和域名的形式;
Server_name
##定义虚拟主机的名称;
Location
##根据请求URL,设置站点的请求路径,location可以引用正则表达式;
Listen
##设置服务器接受请求的IP地址和端口;
client_body_buffer_size
##设置客户端请求主体的缓存区大小,默认大小为8k|16k;
client_body_temp_path
##设置客户端请求主体的临时文件存储目录,最多允许三个层次的子目录结构;
client_body_timeout
##定义客户端读取请求主体的超时时间,默认为60s;
client_max_body_size
##设置客户端请求主体所允许的最大数值,默认为1m;
##设置客户端请求头的缓冲区大小,默认为1k,对于大多数的请求,1k字节的缓存区就够了;
large_client_header_buffers
##设置客户端请求头的缓存区最大值,默认为4 8k,处理客户端请求头超过1k字节的请求;
client_header_timeout
##定义客户端读取请求头的超时时间,默认为60s;
error_page
##定义请求错误的URL所返回的状态码,如:404,500,502,503,504,日志类型:debug|info|notice|warn|error|crit;
server_tokens
##定义Httprequest请求错误时,是否反馈Nginx的版本信息;
open_file_cache
##配置文件缓存存储:
1)打开的文件描述符,他们的大小和修改时间;
2)存在的目录信息
3)文件查找错误,例如:为找到文件,没有读取权限,等等;
open_file_cache_errors
##启用或禁用open_file_cache文件查找错误的缓存,默认值on|off;
open_file_cache_min_uses
##设置文件访问期间的最小数,且在缓存期间需要一个文件描述符保持打开状态;
open_file_cache_valid
##设置访问文件缓存的验证时间,默认为60s;
Sendfile
##设置高效文件传输模式,对于普通应用设为on,否则off;
send_timeout
##设置客户端传输响应的超时时间,默认为60s;
tcp_nodelay
##防止网络阻塞
tcp_nopush
##防止网络阻塞
keepalived_timeout
##设置长连接超时时间,默认为75s;
server_names_hash_bucket_size
##设置服务器名称哈希表的大小,可以加快静态数据的处理过程,默认为32|64|128
server_names_hash_max_size
##设置服务器名称哈希表的最大值;加快了通过减少访问内存的数量在处理器的散列键搜索,默认为512
服务器名称 | 系统 | CPU架构 | 内核 | IP地址 | 角色 |
WEB1 | Ubuntu 13.10 | x86_64 | 3.11.0-12-generic | 10.16.10.38 | Web Server |
##提示
前一篇文档介绍了在CentOS下如何安装Nginx的两种方式,现在介绍在Ubuntu下如何安装Nginx,编译安装的方式与CentOS一致,这里不再过多讲解,主要介绍apt-get的安装方式;
#编辑APT数据源列表,末尾添加如下两行:
root@WEB1:~# vim /etc/apt/sources.list deb http://nginx.org/packages/ubuntu/ codename nginx deb-src http://nginx.org/packages/ubuntu/ codename nginx
root@WEB1:~# vim /etc/nginx/nginx.conf
user nginx nginx;
worker_processes 8;
pid /run/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main ‘$http_x_forwarded_for $remote_addr - ‘
‘$remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$gzip_ratio"‘;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
types_hash_max_size 2048;
server_tokens off;
client_body_buffer_size 16k;
client_body_timeout 60s;
client_max_body_size 1m;
client_body_temp_path /data/cache/client_temp 1 2;
client_header_buffer_size 1k;
large_client_header_buffers 4 8k;
client_header_timeout 60s;
server_names_hash_bucket_size 64;
server_name_in_redirect off;
# File Cache
open_file_cache max=1000 inactive=30s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# Logging Settings
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log crit;
location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ { #忽略大小写,匹配以*.jpeg,*.png,*.swf结尾的图片,缓存1天;
expires 1d;
}
location ~* \.(css|js)$ { #忽略大小写,匹配*.css,*.js结尾的文件,缓存1小时;
expires 1h;
}
access_log on; #启用日志功能;
}
root@WEB1:~# mkdir /etc/nginx/vhosts/
root@WEB1:~# vim /etc/nginx/vhosts/www.exiuxiu.com
server {
location / {
root /data/bbs/program; #站点根目录,需要在此目录下创建index.html文件;
index index.html index.htm; #索引文件类型
}
error_page 404 /error/404.html; #定义404错误页面
error_page 500 /error/500.html;
error_page 502 /error/500.html;
}root@WEB1:~# nginx –t #检查语法
nginx: the configuration file/etc/nginx/nginx.conf syntax is ok
nginx: configuration file/etc/nginx/nginx.conf test is successful
root@WEB1:~# /etc/init.d/nginx start
*Starting nginx nginx [ OK ]
root@WEB1:~# netstat –lntp #检查端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local AddressForeign AddressStatePID/Program name
tcp00 0.0.0.0:800.0.0.0:*LISTEN21472/nginx: master
tcp00 0.0.0.0:220.0.0.0:*LISTEN17668/sshd
tcp600 :::22:::*LISTEN17668/sshd
root@WEB1:~# curl -Ihttp://localhost/index.html
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 09 May 2014 06:54:49 GMT
Content-Type: text/html
Content-Length: 44
Last-Modified: Fri, 09 May 2014 06:03:38GMT
Connection: keep-alive
ETag: "536c6fba-2c"
Accept-Ranges: bytes
此篇文档一花费了好几个小时才写出来,希望能够帮得到大家,后期还会有更多的Nginx文档更新,如:Nginx+php-fpm+Opcache,Nginx+keepalived,Nginx+tomcat,Nginx+resin,Nginx+Apache等,请大家多多支持;
本文出自 “人生有梦而精彩” 博客,请务必保留此出处http://cfwlxf.blog.51cto.com/3966339/1408910
Nginx学习指南之模块的应用,布布扣,bubuko.com
原文地址:http://cfwlxf.blog.51cto.com/3966339/1408910