×
新网 > 虚机资讯 > 正文

Introduction To Nginx

摘要: Nginx是一款高性能的HTTP和反向代理服务器软件 也是一个 IMAP/POP3/SMTP 代理服务器

 

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件代理服务器,并在一个BSD-like 协议下发行

其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.

1 (52).jpg

 

Nginx由内核和模块组成

内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block,而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。


Nginx的模块从结构上分为核心模块、基础模块和第三方模块, HTTP模块、EVENT模块和MAIL模块等属于核心模块,HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基本模块,而HTTP Upstream Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块,用户根据自己的需要开发的模块都属于第三方模块。正是有了这么多模块的支撑,Nginx的功能才会如此强大。


Nginx的模块从功能上分为三类:
Handlers此类模块直接处理请求,并进行输出内容和修改headers信息等操作。handlers处理器模块一般只能有一个。
Filters 此类模块主要对其他处理器模块输出的内容进行修改操作,最后由Nginx输出。
Proxies 就是Nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务比如fastcgi等操作交互,实现服务代理和负载均衡等功能。

 

 

在工作方式上,Nginx分为单工作进程和多工作进程两种模式。在单工作进程模式下,除主进程外,还有一个工作进程,工作进程是单线程的;在多工作进程模式下,每个工作进程包含多个线程。Nginx默认为单工作进程模式。
Nginx的模块直接被编译进Nginx,因此属于静态编译方式。

启动Nginx后,Nginx的模块被自动加载,不像在Apache一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。在解析配置文件时,Nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。

Nginx安装:

./configure
make
make install

默认安装的路径是/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配置注解:

#运行用户 usernobody; #启动进程,通常设置成和cpu的数量相等 worker_processes1; #全局错误日志及PID文件 #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; #工作模式及连接数上限 events{ #epoll是多路复用IO(I/OMultiplexing)中的一种方式, #仅用于linux2.6以上内核,可以大大提高nginx的性能 useepoll; #单个后台workerprocess进程的最大并发链接数 worker_connections1024; #并发总数是worker_processes和worker_connections的乘积 #即max_clients=worker_processes*worker_connections #在设置了反向代理的情况下,max_clients=worker_processes*worker_connections/4为什么 #为什么上面反向代理要除以4,应该说是一个经验值 #根据以上条件,正常情况下的NginxServer可以应付的最大连接数为: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-SHn65535 } http{ #设定mime类型,类型由mime.type文件定义 includemime.types; default_typeapplication/octet-stream; #设定日志格式 log_formatmain\'$remote_addr-$remote_user[$time_local]"$request"\' \'$status$body_bytes_sent"$http_referer"\' \'"$http_user_agent""$http_x_forwarded_for"\'; access_loglogs/access.logmain; #sendfile指令指定nginx是否调用sendfile函数(zerocopy方式)来输出文件, #对于普通应用,必须设为on, #如果用来进行下载等应用磁盘IO重负载应用,可设置为off, #以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfileon; #tcp_nopushon; #连接超时时间 #keepalive_timeout0; keepalive_timeout65; tcp_nodelayon; #开启gzip压缩 gzipon; gzip_disable"MSIE[1-6]."; #设定请求缓冲 client_header_buffer_size128k; large_client_header_buffers4128k; #设定虚拟主机配置 server{ #侦听80端口 listen80; #定义使用www.nginx.cn访问 server_namewww.nginx.cn; #定义服务器的默认网站根目录位置 roothtml; #设定本虚拟主机的访问日志 access_loglogs/nginx.access.logmain; #默认请求 location/{ #定义首页索引文件的名称 indexindex.phpindex.htmlindex.htm; } #定义错误提示页面 error_page500502503504/50x.html; location=/50x.html{ } #静态文件,nginx自己处理 location~^/(images|javascript|js|css|flash|media|static)/{ #过期30天,静态文件不怎么更新,过期可以设大一点, #如果频繁更新,则可以设置得小一点。 expires30d; } #PHP脚本请求全部转发到FastCGI处理.使用FastCGI默认配置. location~.php${ fastcgi_pass127.0.0.1:9000; fastcgi_indexindex.php; fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; includefastcgi_params; } #禁止访问.htxxx文件 location~/.ht{ denyall; } }}

 

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 Confighttp://hi.baidu.com/dc_life/item/c989143f5e1f7789b711db58

 

 

 

 

 

 

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading