1.nginx简介(来自百度百科)
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:京东、新浪、网易、腾讯、淘宝等。
2.nginx安装
需要依赖的包:
ssl功能需要openssl库(https://www.openssl.org/)
gzip模块需要zlib库(http://www.zlib.net/)
rewrite模块需要pcre库(http://www.pcre.org/)
Nginx包下载地址:http://nginx.org/en/download.html
我的资源:https://download.youkuaiyun.com/download/pac123321/11306097
我下载的版本如下(系统为阿里云服务器centos 6.5 64位):
openssl-1.0.1s.tar.gz
zlib-1.2.8.tar.gz
pcre-8.38.tar.gz
nginx-1.8.1.tar.gz
首先安装gcc gcc-c++
[root@VM_119_229_centos /]# yun install gcc
[root@VM_119_229_centos /]# yum install gcc-c++
然后通过ftp把软件都传到服务器上,我这里用的是Xftp4
a.安装openssl-1.0.1s
[root@VM_119_229_centos javaenv]# tar -zxvf openssl-1.0.1s.tar.gz
[root@VM_119_229_centos javaenv]# cd openssl-1.0.1s
[root@VM_119_229_centos openssl-1.0.1s]# ./config
[root@VM_119_229_centos openssl-1.0.1s]# make
[root@VM_119_229_centos openssl-1.0.1s]# make isntall
b.安装zlib-1.2.8.tar.gz
[root@VM_119_229_centos javaenv]# tar -xvf zlib-1.2.8.tar.gz
[root@VM_119_229_centos javaenv]# cd zlib-1.2.8
[root@VM_119_229_centos zlib-1.2.8]# ./configure
[root@VM_119_229_centos zlib-1.2.8]# make
[root@VM_119_229_centos zlib-1.2.8]# make install
c.安装pcre-8.38.tar.gz
[root@VM_119_229_centos javaenv]# tar -zxvf pcre-8.38.tar.gz
[root@VM_119_229_centos javaenv]# cd pcre-8.38
[root@VM_119_229_centos pcre-8.38]# ./configure
[root@VM_119_229_centos pcre-8.38]# make
[root@VM_119_229_centos pcre-8.38]# make install
d.安装nginx-1.8.1.tar.gz
[root@VM_119_229_centos javaenv]# tar -zxvf nginx-1.8.1.tar.gz
[root@VM_119_229_centos javaenv]# cd nginx-1.8.1
[root@VM_119_229_centos nginx-1.8.1]#./configure --with-pcre=../pcre-8.38 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.1s
[root@VM_119_229_centos nginx-1.8.1]# make
[root@VM_119_229_centos nginx-1.8.1]# make install
到此全部安装完成。
检测是否安装成功
[root@VM_119_229_centos nginx-1.8.1]# cd /usr/local/nginx/sbin
[root@VM_119_229_centos sbin]# ./nginx -t
出现如下所示提示,表示安装成功。
![]()
启动nginx
[root@VM_119_229_centos sbin]# ./nginx
查看端口
[root@VM_119_229_centos sbin]# netstat -ntlp
修改 nginx/conf 中的 nginx.conf文件,修改内容如下:

重启nginx
[root@VM_119_229_centos sbin]# ./nginx -s reload
Nginx分配服务器策略
1.轮询(默认)
每个请求按照时间顺序逐一分配到不同的后端服务器,如果后端服务器挂掉,能自动剔除。
2.weight
weight代表权重,默认为1,权重越高被分配的客户端越多
3.ip_hash
每个请求按访问ip的hash结果分配,这样每个方可固定访问一个后端服务器。
4.fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
nginx.conf文件解析
1.全局块
从配置文件开始到events块之间的内容,主要会设置一些影响nginx服务器整体运行的配置指令,主要包括配置运行nginx服务器的用户(组)、允许生成的worker process数,进程PID存放路径、日志存放路径和类型以及配置文件的引入等。
比如上面第一行配置的:
worker_processes 1;
这是nginx服务器并发处理服务的关键配置,worker_processes值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。
2.events 块
比如上面的配置:
events {
worker_connections 1024;
}
events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。 上述例子就表示每个 work process 支持的最大连接数为 1024. 这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
一个 nginx 能建立的最大连接 数,应该是 worker_connections * worker_processes。
3.http 块
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。 需要注意的是:http 块也可以包括 http 全局块、server 块。
http 全局块:
http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
server 块:
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了 节省互联网服务器硬件成本。
每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。
而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。
1、全局 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置。
2、location 块
一个 server 块可以配置多个 location 块。
这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称 (也可以是 IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
location 指令说明
该指令用于匹配 URL。 语法如下:
location [ = | ~ | ~* | ^~ | @ ] uri {
}
1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
2、~:用于表示 uri 包含正则表达式,并且区分大小写。
3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字 符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。
本文介绍了Nginx,它是高性能HTTP和反向代理服务器。讲述了其安装过程,需依赖openssl、zlib、pcre库。还介绍了Nginx分配服务器的策略,如轮询、weight等。最后对nginx.conf文件进行解析,包括全局块、events块、http块等内容。
424

被折叠的 条评论
为什么被折叠?



