简介
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个IMAP/POP3/SMTP 代理服务器 。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发的。Igor 将源代码以类BSD许可证的形式发布。自Nginx 发布四年来,Nginx 已经因为它的稳定性、丰富的功能集、 示例配置文件和低系统资源的消耗而闻名了。目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。 新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx。
为什么选择Nginx
Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性:
在高连接并发的情况下,Nginx是Apache服务器不错的替代品: Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一. 能够支持高达 50,000 个并发连接数的响应, 感谢Nginx为我们选择了 epoll and kqueue 作为开发模型.
Nginx作为负载均衡服务器: Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP代理 服务器对外进行服务. Nginx采用C进行编写, 不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多.
作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器), Last.fm 描述了成功并且美妙的使用经验.
Nginx 是一个 [#installation 安装] 非常的简单 , 配置文件 非常简洁(还能够支持perl语法), Bugs 非常少的服务器: Nginx 启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动. 你还能够 不间断服务的情况下进行软件版本的升级 .
Nginx 的诞生主要解决C10K问题
基本的HTTP服务器特性
处理静态文件,索引文件以及自动索引;打开文件描述符缓存;
使用缓存加速反向代理;简单负载均衡以及容错;
远程FastCGI,uwsgi,SCGI,和memcached服务的缓存加速支持;简单的负载均衡以及容错;
模块化的架构。过滤器包括gzip压缩、ranges支持、chunked响应、XSLT,SSI以及图像缩放。在SSI 过滤器中,一个包含多个SSI的页面,如果经由FastCGI或反向代理处理,可被并行处理;
支持SSL,TLS SNI。
其他的HTTP服务器特性
基于名字和IP的虚拟主机;
Keep-alive和pipelined连接支持;
灵活的配置;
重新加载配置以及在线升级时,不需要中断正在处理的请求;
自定义访问日志格式,带缓存的日志写操作以及快速日志轮转;
3xx-5xx错误代码重定向;
重写(rewrite)模块:使用正则表达式改变URI;
根据客户端地址执行不同的功能;
基于客户端IP地址和HTTP基本认证机制的访问控制;
支持验证HTTP referer;
支持PUT、DELETE、MKCOL、COPY以及MOVE方法;
支持FLV流和MP4流;
速度限制;
来自同一地址的同时连接数或请求数限制;
嵌入Perl语言。
邮件代理服务器特性
使用外部HTTP认证服务器重定向用户到IMAP/POP3后端;
使用外部HTTP认证服务器认证用户后重定向连接到内部SMTP后端;
支持的认证方式:
POP3: USER/PASS, APOP, AUTH LOGIN/PLAIN/CRAM-MD5;
IMAP: LOGIN, AUTH LOGIN/PLAIN/CRAM-MD5;
SMTP: AUTH LOGIN/PLAIN/CRAM-MD5;
SSL支持;
STARTTLS和STLS支持。
架构和扩展性
一个主进程和多个工作进程,工作进程以非特权用户运行;
支持的事件机制:kqueue(FreeBSD 4.1+)、epoll(Linux 2.6+)、rt signals(Linux 2.2.19+)、/dev/poll(Solaris 7 11/99+)、event ports(Solaris 10)、select以及poll;
众多支持的kqueue特性包括EV_CLEAR、EV_DISABLE(临时禁止事件)、NOTE_LOWAT、EV_EOF,可用数据的数量,错误代码;
支持sendfile(FreeBSD 3.1+, Linux 2.2+, Mac OS X 10.5+)、sendfile64(Linux 2.4.21+)和sendfilev(Solaris 8 7/01+);
文件AIO(FreeBSD 4.3+, Linux 2.6.22+);
DIRECTIO (FreeBSD 4.4+, Linux 2.4+, Solaris 2.6+, Mac OS X);
支持Accept-filters(FreeBSD 4.1+, NetBSD 5.0+)和 TCP_DEFER_ACCEPT(Linux 2.4+);
10000个非活跃的HTTP keep-alive连接仅占用约2.5M内存;
尽可能避免数据拷贝操作。
Nginx 工作原理
Nginx会按需同时运行多个进程:一个主进程(master)和几个工作进程(worker),配置了缓存时还会有缓存加载器进程(cache loader)和缓存管理器进程(cache manager)等。所有进程均是仅含有一个线程,并主要通过“共享内存”的机制实现进程间通信。主进程以root用户身份运行,而worker、cache loader和cache manager均应以非特权用户身份运行。
主进程主要完成如下工作:
读取并验正配置信息;
创建、绑定及关闭套接字;
启动、终止及维护worker进程的个数;
无须中止服务而重新配置工作特性;
控制非中断式程序升级,启用新的二进制程序并在需要时回滚至老版本;
重新打开日志文件;
编译嵌入式perl脚本;
worker进程主要完成的任务包括:
接收、传入并处理来自客户端的连接;
提供反向代理及过滤功能;
nginx任何能完成的其它任务;
注,如果负载以CPU密集型应用为主,如SSL或压缩应用,则worker数应与CPU数相同;如果负载以IO密集型为主,如响应大量内容给客户端,则worker数应该为CPU个数的1.5或2倍。
安装配置Nginx
安装启动nginx
环境:
系统:Centos6.5(2.6.32-431.el6.x86_64) 关闭防火墙和Selinux
IP: eth0 172.16.19.20/16
软件版本(EPEL源):nginx-1.0.15-5.el6.x86_64.rpm
# ntpdate 172.16.0.1
# yum install nginx -y
安装完成后主要生成的配置文件
/etc/rc.d/init.d/nginx
/etc/sysconfig/nginx
/etc/nginx/conf.d/default.conf
/usr/share/nginx/html
/etc/nginx/conf.d/ssl.conf
/var/log/nginx
# service nginx start #启动nginx,服务默认监听在80端口
Starting nginx: [ OK ]
# ss -tnlp |grep nginx
LISTEN 0 128 *:80 *:* users:(("nginx",1962,6),("nginx",1963,6))
配置Nginx
Nginx的代码是由一个核心和一系列的模块组成, 核心主要用于提供Web Server的基本功能,以及Web和Mail反向代理的功能;还用于启用网络协议,创建必要的运行时环境以及确保不同的模块之间平滑地进行交互。不过,大多跟协议相关的功能和某应用特有的功能都是由nginx的模块实现的。这些功能模块大致可以分为事件模块、阶段性处理器、输出过滤器、变量处理器、协议、upstream和负载均衡几个类别,这些共同组成了nginx的http功能。事件模块主要用于提供OS独立的(不同操作系统的事件机制有所不同)事件通知机制如kqueue或epoll等。协议模块则负责实现nginx通过http、tls/ssl、smtp、pop3以及imap与对应的客户端建立会话。
Nginx的核心模块为Main和Events,此外还包括标准HTTP模块、可选HTTP模块和邮件模块,其还可以支持诸多第三方模块。Main用于配置错误日志、进程及权限等相关的参数,Events用于配置IO模型,如epoll、kqueue、select或poll等,它们是必备模块。
Nginx的主配置文件由几个段组成,这个段通常也被称为nginx的上下文,每个段的定义格式如下所示。需要注意的是,其每一个指令都必须使用分号(;)结束,否则为语法错误。
<section> {
<directive> <parameters>;
}
nginx 配置文件结构
Nginx配置文件主要分为4部分:main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)和 location(URL匹配特定位置的设置)。main部分设置的指令将影响其他所有设置;server部分的指令主要用于指定主机和端口;upstream指令主要用于负载均衡,设置一系列的后端服务器;location部分用于匹配网页位置。这四者之间的关系如下:server继承main,location继承server,upstream既不会继承其他设置也不会被继承。如下图,
在这4个部分当中,每个部分都包含若干指令,这些指令主要包含Nginx的主模块指令、事件模块指令、HTTP核心模块指令。同时每个部分还可以使用其他HTTP模块指令,例如Http SSL模块、Http Gzip Static模块和Http Addition模块等。
一、配置虚拟主机:
# cat /etc/nginx/nginx.conf # 在配置文件加入下面内容
server { listen
80
; server_name www.luo.com; location
/
{ root
/
data
/
webapps
/
www; index index.html index.htm; } } server { listen
80
; server_name bbs.luo.com; location
/
{ root
/
data
/
webapps
/
bbs; index index.html index.htm; } }
# service nginx reload
在Windows 7下路径:C:\Windows\System32\drivers\etc\hosts
增加两行:
172.16.19.20 www.luo.com
172.16.19.20 bbs.luo.com
测试:根据域名访问相应的网页
二、配置访问控制、状态检测页面与目录浏览
server { listen
80
; server_name bbs.luo.com; location
/
{ root
/
data
/
webapps
/
bbs; index index.html index.htm; auth_basic
"
Auth Page
"
;
#
基于用户认证
auth_basic_user_file
/
etc
/
nginx
/
.user;
#
deny 192.168.1.107; # 基于IP的访问控制,访问的话网页默认会返回403 Forbidden
#
allow 192.168.1.0/24;
#
deny all;
autoindex on;
#
打开目录浏览功能,主页面不在在时
autoindex_exact_size on; autoindex_localtime on; } location
/
status {
#
开启状态页面信息查看,并以认证用户访问
root
/
; stub_status on; auth_basic
"
NginxStatus
"
; auth_basic_user_file
/
etc
/
nginx
/
.user; } }
#
yum install -y httpd
#
cd /etc/nginx
#
htpasswd -c -m /etc/nginx/.user nginx
New password: nginx Re
-
type new password: Adding password
for
user nginx
#
ll .user
-
rw
-
r
--
r
--
1
root root
44
Sep
26
00
:
16
.user
#
service nginx reload
验证:输入用户名nginx,密码nginx,即可访问。
状态页面信息查看
测试以目录访问网站
三、配置Nginx基于ssl提供https服务
创建CA自签证书
#
cd /etc/pki/CA/
[root
@
node CA]
#
ls
certs crl newcerts private [root
@
node CA]
#
cd private/
[root
@
node private]
#
(umask 077; openssl genrsa 2048 > cakey.pem) #生成私钥
Generating RSA private key,
2048
bit long modulus ......................................................................................................................................................................................
+++
e is
65537
(
0x10001
) [root
@
node private]
#
cd ..
[root
@
node CA]
#
openssl req -new -x509 -key ./private/cakey.pem -out cacert.pem #生成自签证书
You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank
For
some fields there will be a
default
value,
If
you enter '.', the field will be left blank.
-----
Country Name (
2
letter code) [XX]:CN State or Province Name (full name) []:HA Locality Name (eg, city) [
Default
City]:ZZ Organization Name (eg, company) [
Default
Company Ltd]:LUO Organizational Unit Name (eg, section) []:Tech Common Name (eg, your name or your server's hostname) []: [root
@
node CA]
#
touch serial
[root
@
node CA]
#
echo 01 > serial
[root
@
node CA]
#
touch index.txt
[root
@
node CA]
#
ll
total
24
-
rw
-
r
--
r
--
1
root root
1224
Sep
26
00
:
54
cacert.pem drwxr
-
xr
-
x.
2
root root
4096
Nov
22
2013
certs drwxr
-
xr
-
x.
2
root root
4096
Nov
22
2013
crl
-
rw
-
r
--
r
--
1
root root
0
Sep
26
00
:
56
index.txt drwxr
-
xr
-
x.
2
root root
4096
Nov
22
2013
newcerts drwx
------
.
2
root root
4096
Sep
26
00
:
51
private
-
rw
-
r
--
r
--
1
root root
3
Sep
26
00
:
55
serial
生成证书申请
[root
@
node CA]
#
mkdir /etc/nginx/ssl
[root
@
node CA]
#
cd /etc/nginx/ssl
[root
@
node ssl]
#
(umask 077; openssl genrsa 1024 > nginx.key) #生成私钥
Generating RSA private key,
1024
bit long modulus ...............
++++++
.
++++++
e is
65537
(
0x10001
) [root
@
node ssl]
#
openssl req -new -key nginx.key -out nginx.csr
You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank
For
some fields there will be a
default
value,
If
you enter '.', the field will be left blank.
-----
Country Name (
2
letter code) [XX]:CN State or Province Name (full name) []:HA Locality Name (eg, city) [
Default
City]:ZZ Organization Name (eg, company) [
Default
Company Ltd]:LUO Organizational Unit Name (eg, section) []:Tech Common Name (eg, your name or your server's hostname) []: Email Address []: [root
@
node CA]
#
mkdir /etc/nginx/ssl
[root
@
node CA]
#
cd /etc/nginx/ssl
[root
@
node ssl]
#
(umask 077; openssl genrsa 1024 > nginx.key) #生成私钥
Generating RSA private key,
1024
bit long modulus ...............
++++++
.
++++++
e is
65537
(
0x10001
) [root
@
node ssl]
#
openssl req -new -key nginx.key -out nginx.csr
You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank
For
some fields there will be a
default
value,
If
you enter '.', the field will be left blank.
-----
Country Name (
2
letter code) [XX]:CN State or Province Name (full name) []:HA Locality Name (eg, city) [
Default
City]:ZZ Organization Name (eg, company) [
Default
Company Ltd]:LUO Organizational Unit Name (eg, section) []:Tech Common Name (eg, your name or your server's hostname) []: Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
让CA签名并颁发证书
[root
@
node ssl]
#
openssl ca -in nginx.csr -out nginx.crt -days 3650
Using configuration from
/
etc
/
pki
/
tls
/
openssl.cnf Check that the request matches the signature Signature ok Certificate Details: Serial Number:
1
(
0x1
) Validity Not Before: Sep
25
17
:
05
:
46
2014
GMT Not After : Sep
22
17
:
05
:
46
2024
GMT Subject: countryName
=
CN stateOrProvinceName
=
HA organizationName
=
LUO organizationalUnitName
=
Tech commonName
=
node.luo.com X509v3 extensions: X509v3 Basic Constraints: CA:
FALSE
Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier:
23
:DF:A7:
65
:EC:
04
:
18
:FE:
15
:9F:D5:D7:
87
:
51
:AF:CA:6C:6C:F8:1C X509v3 Authority Key Identifier: keyid:CC:
49
:
57
:FB:D2:E8:5F:6F:
46
:
44
:0A:
33
:6E:
43
:F6:
17
:E8:6D:9F:6E Certificate is to be certified
until
Sep
22
17
:
05
:
46
2024
GMT (
3650
days) Sign the certificate
?
[y
/
n]:y
1
out of
1
certificate requests certified, commit
?
[y
/
n]y Write out database with
1
new entries Data Base Updated
然后在/etc/nginx/nginx.conf文件中加上如下代码:
server { listen
443
; server_name localhost; ssl on; ssl_certificate
/
etc
/
nginx
/
ssl
/
nginx.crt; ssl_certificate_key
/
etc
/
nginx
/
ssl
/
nginx.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:
!
aNULL:
!
MD5; ssl_prefer_server_ciphers on; location
/
{ root html; index index.html index.htm; } }
# service nginx restart
# ss -tnl |grep 443
测试https页面