Nginx

2019-01-08  本文已影响0人  你说你要个夏天

Nginx介绍

简介

Nginx("engine x")是一个高性能的HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。官方测试nginx能够支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。2011年6月1日,nginx 1.0.4发布。

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:新浪、网易、腾讯等。

功能:web服务器、web reverse proxy、smtp reverse proxy

Nginx和apache的比较

1. nginx相对于apache的优点:

轻量级,同样起web服务,比apache占用更少的内存及资源 ;

抗并发,nginx处理请求是异步非阻塞的,而apache则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能 ;

高度模块化的设计,编写模块相对简单 ; 社区活跃,各种高性能模块出品迅速。

2. apache 相对于nginx 的优点:

rewrite ,比nginx的rewrite强大 ; 模块超多,基本想到的都可以找到 ; 少bug,nginx的bug相对较多。

3. Nginx配置简洁,Apache复杂 。

4. 最核心的区别在于apache是同步多进程模型,一个连接对应一个进程;nginx是异步的,多个连接(万级别)可以对应一个进程。

单个tomcat支持的最高并发

解决高并发和单个服务器过载问题

Tengine安装

Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。

1. 解压安装包

[root@node001 local]# tar xf tengine-2.1.0.tar.gz

2. 进入tengine目录

[root@node001 local]# cd tengine-2.1.0/

3. 查看README文件,找到安装方法

To install Tengine, just follow these three steps:

$ ./configure

$ make

# make install

4. 执行configure文件,指定安装目录

[root@node001 tengine-2.1.0]# ./configure --prefix=/usr/local/nginx

使用如下命令查看更多安装选项

[root@node01 tengine-2.1.0]# ./configure --help

--help                                print this message

--prefix=PATH                   set installation prefix

--sbin-path=PATH             set nginx binary pathname

--conf-path=PATH              set nginx.conf pathname

--error-log-path=PATH       set error log pathname ……

5. 报错:

./configure: error: C compiler cc is not found

6. 安装gcc,再执行configure文件

[root@node001 tengine-2.1.0]# yum install gcc

7. 报错:

./configure: error: the HTTP rewrite module requires the PCRE library.

8. 查看PCRE有哪些版本

[root@node001 tengine-2.1.0]# yum search pcre

pcre-devel.i686 : Development files for pcre

pcre-devel.x86_64 : Development files for pcre

pcre-static.x86_64 : Static library for pcre

pcre.x86_64 : Perl-compatible regular expression library pcre.i686 : Perl-compatible regular expression library

9. 选择安装开发版,系统自动识别安装什么位数的软件 [root@node001 tengine-2.1.0]# yum install pcre-devel

10. 再执行configure文件

[root@node001 tengine-2.1.0]# ./configure --prefix=/usr/local/nginx

11. 报错:

./configure: error: SSL modules require the OpenSSL library.

12. 根据pcre的经验,安装OpenSSL开发版 [root@node001 tengine-2.1.0]# yum install openssl-devel

13. 再执行configure文件

[root@node001 tengine-2.1.0]# ./configure --prefix=/usr/local/nginx

看到如下信息说明configure文件执行成功:

14. 执行make命令

[root@node001 tengine-2.1.0]# make

15. 执行make install命令

[root@node001 tengine-2.1.0]# make install

16. 将nginx文件放到/etc/init.d目录下,并做修改 [root@node001 tengine-2.1.0]# vi /etc/init.d/nginx nginx="/usr/local/nginx/sbin/nginx" NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

17. 给nginx文件赋予执行权限

[root@node001 tengine-2.1.0]# chmod +x nginx

18. 启动服务

[root@node001 sbin]# service nginx start

19. 验证是否启动

[root@node001 sbin]# service nginx status nginx (pid 6510 6508) is running...

20. 去网页验证,看到如下页面说明nginx安装成功!

Nginx配置解析

nginx.conf配置文件的结构

……

events{

……

}

http {

……

server{

……

}

server{

……

}

}

全局的配置

user nobody; #定义Nginx运行的用户和用户组

说明:

[root@node01 tengine-2.1.0]# ps -fe | grep nginx root 1367 1335 0 13:18 pts/1 00:00:00 vi nginx.conf root 1608 1 0 14:32 ? 00:00:00 nginx: master process /opt/sxt/nginx/sbin/nginx –c /opt/sxt/nginx/conf/nginx.conf nobody 1610 1608 0 14:32 ? 00:00:00 nginx: worker process root 1626 1097 0 14:45 pts/0 00:00:00 grep nginx [root@node01 tengine-2.1.0]# service nginx status nginx (pid 1610 1608) 正在运行...

master process不负责处理客户端连接请求,负责对worker process的监管,而worker process负责处理客户端请求。Nginx支持热加载和热升级,比如更新了配置文件后执行reload命令,master会开出一个新进程去读取更新过的配置文件,而worker进程继续保持从旧请求的连接,直到旧进程死亡,新进程会与新请求连接。master process由root启动,worker process由nobody启动,权限较小。

worker_processes 1; #nginx进程数,建议设置为等于虚拟机CPU总核心数

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]

pid logs/nginx.pid; #进程文件

event下的一些配置及其意义

use epoll;

#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];

#epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型

#如果跑在FreeBSD上面,就用kqueue模型。

worker_connections 1024; #单个进程最大连接数(最大连接数=连接数*进程数)

#假设worker_processes为8

#系统可以打开的最大文件数和内存大小成正比

#查看自己的系统可以打开的最大文件数 cat /proc/sys/fs/file-max :97318

#并发连接总数小于系统可以打开的文件总数,这样就在操作系统可以承受的范围之内

#选择最大连接数为80000

#在设置了反向代理的情况下,根据经验,最大连接数应该再除以4,就是20000

#所以单个进程最大连接数为20000/8 = 2500

#同时应该调整系统的单个进程可以打开的文件数

#使用ulimit –a查看到open file =1024

#应该调整系统的单个进程最大打开文件数(该数值x进程数<=97318)

#ulimit -SHn 10000

http下的一些配置及其意义

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 on;

#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off.

原理,比如Nginx接受用户对某文件的请求,nginx不能直接读取磁盘的内容,需要经过内核的调用,nginx告诉内核需要读取x文件,内核会读取x文件到内核的内存中,在把这个文件copy到nginx的内存堆中,nginx得知数据已经准备好,会再把这个文件发给内核,内核切片之后发送给用户。当并发数过大时太耗费资源,所以这个选项的存在是为了减少文件在两个内存之间的copy,提高效率。

keepalive_timeout 0; #长连接超时时间,单位是秒(与keeplived软件无关)

#gzip on; #开启gzip压缩输出

server下的一些配置及其意义

listen 80; #监听的IP和地址

server_name www.bbb.com; #主机名

location / {

root /opt; #网页文件存放的目录

index index.html index.htm;

#默认首页文件,顺序从小到右,如果找不到index.html,则index.htm为首页

autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。

}

上一篇 下一篇

猜你喜欢

热点阅读