Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件代理服务器,并在一个BSD-like 协议下发行javascript
其特色是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.php
Nginx由内核和模块组成css
内核的设计很是微小和简洁,完成的工做也很是简单,仅仅经过查找配置文件将客户端请求映射到一个location block,而在这个location中所配置的每一个指令将会启动不一样的模块去完成相应的工做。html
Nginx的模块从结构上分为核心模块、基础模块和第三方模块, HTTP模块、EVENT模块和MAIL模块等属于核心模块,HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基本模块,而HTTP Upstream Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块,用户根据本身的须要开发的模块都属于第三方模块。正是有了这么多模块的支撑,Nginx的功能才会如此强大。java
Nginx的模块从功能上分为三类:
Handlers此类模块直接处理请求,并进行输出内容和修改headers信息等操做。handlers处理器模块通常只能有一个。
Filters 此类模块主要对其余处理器模块输出的内容进行修改操做,最后由Nginx输出。
Proxies 就是Nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务好比fastcgi等操做交互,实现服务代理和负载均衡等功能。node
在工做方式上,Nginx分为单工做进程和多工做进程两种模式。在单工做进程模式下,除主进程外,还有一个工做进程,工做进程是单线程的;在多工做进程模式下,每一个工做进程包含多个线程。Nginx默认为单工做进程模式。
Nginx的模块直接被编译进Nginx,所以属于静态编译方式。linux
启动Nginx后,Nginx的模块被自动加载,不像在Apache同样,首先将模块编译为一个so文件,而后在配置文件中指定是否进行加载。在解析配置文件时,Nginx的每一个模块都有可能去处理某个请求,可是同一个处理请求只能由一个模块来完成。nginx
Nginx安装:正则表达式
./configure
make
make installshell
默认安装的路径是/usr/local/nginx
更多的安装配置
./configure --prefix=/usr/local/nginx
--with-openssl=/usr/include (启用ssl)
--with-pcre=/usr/include/pcre/ (启用正规表达式)
--with-http_stub_status_module (安装能够查看nginx状态的程序)
--with-http_memcached_module (启用memcache缓存)
--with-http_rewrite_module (启用支持url重写)
Nginx配置注解:
#运行用户 user nobody; #启动进程,一般设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #工做模式及链接数上限 events { #epoll是多路复用IO(I/O Multiplexing)中的一种方式, #仅用于linux2.6以上内核,能够大大提升nginx的性能 use epoll; #单个后台worker process进程的最大并发连接数 worker_connections 1024; # 并发总数是 worker_processes 和 worker_connections 的乘积 # 即 max_clients = worker_processes * worker_connections # 在设置了反向代理的状况下,max_clients = worker_processes * worker_connections / 4 为何 # 为何上面反向代理要除以4,应该说是一个经验值 # 根据以上条件,正常状况下的Nginx Server能够应付的最大链接数为:4 * 8000 = 32000 # worker_connections 值的设置跟物理内存大小有关 # 由于并发受IO约束,max_clients的值须小于系统能够打开的最大文件数 # 而系统能够打开的最大文件数和内存大小成正比,通常1GB内存的机器上能够打开的文件数大约是10万左右 # 咱们来看看360M内存的VPS能够打开的文件句柄数是多少: # $ cat /proc/sys/fs/file-max # 输出 34336 # 32000 < 34336,即并发链接总数小于系统能够打开的文件句柄总数,这样就在操做系统能够承受的范围以内 # 因此worker_connections需根据worker_processes进程数目和系统能够打开最大文件总数进行适当地进行设置 # 使得并发总数小于操做系统能够打开的最大文件数目 # 其实质也就是根据主机的物理CPU和内存进行配置 # 固然,理论上的并发总数可能会和实际有所误差,由于主机还有其余的工做进程须要消耗系统资源。 # ulimit -SHn 65535 } http { #设定mime类型,类型由mime.type文件定义 include mime.types; 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"'; access_log logs/access.log main; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件, #对于普通应用,必须设为 on, #若是用来进行下载等应用磁盘IO重负载应用,可设置为 off, #以平衡磁盘与网络I/O处理速度,下降系统的uptime. sendfile on; #tcp_nopush on; #链接超时时间 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #开启gzip压缩 gzip on; gzip_disable "MSIE [1-6]."; #设定请求缓冲 client_header_buffer_size 128k; large_client_header_buffers 4 128k; #设定虚拟主机配置 server { #侦听80端口 listen 80; #定义使用 www.nginx.cn访问 server_name www.nginx.cn; #定义服务器的默认网站根目录位置 root html; #设定本虚拟主机的访问日志 access_log logs/nginx.access.log main; #默认请求 location / { #定义首页索引文件的名称 index index.php index.html index.htm; } # 定义错误提示页面 error_page 500 502 503 504 /50x.html; location = /50x.html { } #静态文件,nginx本身处理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { #过时30天,静态文件不怎么更新,过时能够设大一点, #若是频繁更新,则能够设置得小一点。 expires 30d; } #PHP 脚本请求所有转发到 FastCGI处理. 使用FastCGI默认配置. location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #禁止访问 .htxxx 文件 location ~ /.ht { deny all; } }}
configure 支持下面的选项:
--prefix=<path> - Nginx安装路径。若是没有指定,默认为 /usr/local/nginx。
--sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,若是没有指定,默认为<prefix>/sbin/nginx。
--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。若是没有指定,默认为<prefix>/conf/nginx.conf。
--pid-path=<path> - 在nginx.conf中没有指定pid指令的状况下,默认的nginx.pid的路径。若是没有指定,默认为 <prefix>/logs/nginx.pid。
--lock-path=<path> - nginx.lock文件的路径。
--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的状况下,默认的错误日志的路径。若是没有指定,默认为 <prefix>/logs/error.log。
--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的状况下,默认的访问日志的路径。若是没有指定,默认为 <prefix>/logs/access.log。
--user=<user> - 在nginx.conf中没有指定user指令的状况下,默认的nginx使用的用户。若是没有指定,默认为 nobody。
--group=<group> - 在nginx.conf中没有指定user指令的状况下,默认的nginx使用的组。若是没有指定,默认为 nobody。
--builddir=DIR - 指定编译的目录
--with-rtsig_module - 启用 rtsig 模块
--with-select_module --without-select_module - Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.
//容许或不容许开启SELECT模式,若是 configure 没有找到更合适的模式,好比:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种相似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式
--with-poll_module --without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.
--with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.
//开启HTTP SSL模块,使NGINX能够支持HTTPS请求。这个模块须要已经安装了OPENSSL,在DEBIAN上是libssl
--with-http_realip_module - 启用 ngx_http_realip_module
--with-http_addition_module - 启用 ngx_http_addition_module
--with-http_sub_module - 启用 ngx_http_sub_module
--with-http_dav_module - 启用 ngx_http_dav_module
--with-http_flv_module - 启用 ngx_http_flv_module
--with-http_stub_status_module - 启用 "server status" 页
--without-http_charset_module - 禁用 ngx_http_charset_module
--without-http_gzip_module - 禁用 ngx_http_gzip_module. 若是启用,须要 zlib 。
--without-http_ssi_module - 禁用 ngx_http_ssi_module
--without-http_userid_module - 禁用 ngx_http_userid_module
--without-http_access_module - 禁用 ngx_http_access_module
--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module
--without-http_autoindex_module - 禁用 ngx_http_autoindex_module
--without-http_geo_module - 禁用 ngx_http_geo_module
--without-http_map_module - 禁用 ngx_http_map_module
--without-http_referer_module - 禁用 ngx_http_referer_module
--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 若是启用须要 PCRE 。
--without-http_proxy_module - 禁用 ngx_http_proxy_module
--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
--without-http_memcached_module - 禁用 ngx_http_memcached_module
--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module
--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module
--without-http_browser_module - 禁用 ngx_http_browser_module
--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module
--with-http_perl_module - 启用 ngx_http_perl_module
--with-perl_modules_path=PATH - 指定 perl 模块的路径
--with-perl=PATH - 指定 perl 执行文件的路径
--http-log-path=PATH - Set path to the http access log
--http-client-body-temp-path=PATH - Set path to the http client request body temporary files
--http-proxy-temp-path=PATH - Set path to the http proxy temporary files
--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
--without-http - 禁用 HTTP server
--with-mail - 启用 IMAP4/POP3/SMTP 代理模块
--with-mail_ssl_module - 启用 ngx_mail_ssl_module
--with-cc=PATH - 指定 C 编译器的路径
--with-cpp=PATH - 指定 C 预处理器的路径
--with-cc-opt=OPTIONS - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".
--with-cpu-opt=CPU - 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
--without-pcre - 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也须要 PCRE 。
--with-pcre=DIR - 指定 PCRE 库的源代码的路径。
--with-pcre-opt=OPTIONS - Set additional options for PCRE building.
--with-md5=DIR - Set path to md5 library sources.
--with-md5-opt=OPTIONS - Set additional options for md5 building.
--with-md5-asm - Use md5 assembler sources.
--with-sha1=DIR - Set path to sha1 library sources.
--with-sha1-opt=OPTIONS - Set additional options for sha1 building.
--with-sha1-asm - Use sha1 assembler sources.
--with-zlib=DIR - Set path to zlib library sources.
--with-zlib-opt=OPTIONS - Set additional options for zlib building.
--with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro
--with-openssl=DIR - Set path to OpenSSL library sources
--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building
--with-debug - 启用调试日志
--add-module=PATH - Add in a third-party module found in directory PATH
在不一样版本间,选项可能会有些许变化,请老是使用 ./configure --help 命令来检查一下当前的选项列表。
参考文献:
Nginx安装配置简介:http://www.blogjava.net/cenly60/archive/2008/12/12/245965.html
Nginx基本配置与参数说明:http://www.nginx.cn/76.html
Nginx Config http://hi.baidu.com/dc_life/item/c989143f5e1f7789b711db58