Nginx (“engine x”) 是一个高性能的 HTTP 和反向代理服务器。Nginx 是由 Igor Sysoev 为俄罗斯访问点开量第二的 Rambler.ru 站发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。其将源代码以类 BSD 许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011 年 6 月 1 日,Nginx 1.0.4 发布。Nginx 是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,单台物理服务器可支持30 000~50 000个并发请求。并在一个 BSD-like 协议下发行。由俄罗斯的程序设计师 Igor Sysoev 所开发,供俄国大型的入口网站及搜索引擎 Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上 Nginx 的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用 Nginx 网站用户有:百度 BWS、新浪、网易、腾讯等。
Tengine 是由淘宝网发起的 Web 服务器项目。它在 Nginx 的基础上,针对大访问量网站的需求,添
加了很多高级功能和特性。Tengine 的性能和稳定性已经在大型的网站如淘宝网、天猫商城等得到了很好的检验。(可以这样理解:淘宝网拿到了 Nginx 源代码之后,进行了功能的填充、优化等等,然后提交给Nginx 官方,但是由于 Nginx 官方相应慢甚至不响应,加上语言沟通的不顺畅,于是淘宝公司就自己打包,在遵循 GPL 的原则上进行二次开发,于是就出了现在的 Tengine 这个版本)
一款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高----单台物理服务器可支持30000-50000个并发请求
NG并发连接能力受以下两个因素影响:①CPU个数 ②本地物理服务器系统的最大文件打开数
①静态服务器(图片、视频服务、文本)
②动态服务
③方向代理,负载均衡
④缓存服务
Nginx是一个甚于事件的Web服务器,Apache是一个基于流程的服务器;
Nginx所有请求都由一个线程处理,Apache单个线程处理单个请求;
Nginx避免子进程的概念,Apache是基于子进程的;
Nginx在内存消耗和连接方面更好,Apache在内存消耗和连接方面一般;
Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于CPU和内存等硬件;
Nginx支持热部署,Apache不支持热部署;
Nginx对于静态文件处理具有更高效率,Apache相对一般;
Nginx在反向代理场景具有明显优势,Apache相对一般。
Nginx 与 Apache 的区别
1、二者最核心的区别在于 Apache 是同步多进程模型,一个连接对应一个进程;Nginx 是异步的,多个连接(万级别)可以对应一个进程 。Nginx 处理静态文件好, 耗费内存少. 但无疑 Apache 仍然是目前的主流, 有很多丰富的特性. 所以还需要搭配着来. 当然如果能确定 Nginx 就适合需求, 那么使用 Nginx 会是更经济的方式。
2、Nginx 的负载能力比 Apache 高很多。最新的服务器也改用 Nginx 了。而且 Nginx 改完配置能 -t 测试一下配置有没有问题。
3、Apache 重启的时候发现配置出错了,会很崩溃,改的时候都会非常小心翼翼现在看有好多集群站,前端 Nginx 抗并发,后端 Apache 集群, 配合得也不错。
4、Nginx 处理动态请求是鸡肋,一般动态请求要 Apache 去做,Nginx 只适合静态和反向。
5、从经验来看,Nginx 是很不错的前端服务器,负载性能很好,Nginx 用 webbench 模拟 10000 个静态文件请求毫不吃力。 Apache 对 php 等语言的支持很好,此外 Apache 有强大的支持网络,发展时间相对 Nginx 更久,bug 少但是 Apache 有先天不支持多核心处理负载鸡肋的缺点,建议使用 Nginx 做前端,后端用 Apache。大型网站建议用 Nginx 自代的集群功能。
6、大部分情况下 Nginx 都优于 Apache,比如说静态文件处理、PHP-CGI 的支持、反向代理功能、前端 Cache、维持连接等。在 Apache+PHP(prefork)模式下,如果 PHP 处理慢或者前端压力很大的情况下,很容易出现 Apache 进程数飙升,从而拒绝服务的现象。
7、Apache 在处理动态有优势,Nginx 并发性比较好,CPU 内存占用低,如果 rewrite 频繁,那还是 Apache 吧!
8、一般来说,需要性能的 web 服务,用 Nginx 。如果不需要性能只求稳定,那就 Apache 吧。
Apache 与 Nginx 优缺点比较
1、Nginx 相对于 Apache 的优点
轻量级,同样 web 服务,比 Apache 占用更少的内存及资源;抗并发,Nginx 处理请求是异步非阻塞的,而 Apache 则是阻塞型的,在高并发下 Nginx 能保持低资源低消耗高性能;高度模块化的设计,编写模块相对简单;社区活跃,各种高性能模块出品迅速啊;Nginx 本身就是一个反向代理服务器,Nginx 支持7层负载均衡;Nginx 可能会比 Apache 支持更高的并发,Nginx 配置文件写得很简洁,正则配置让很多事情变得简单运行效率高,占用资源少,代理功能强大,很适合做前端响应服务器!
2、Apache 相对于 Nginx 的优点
Rewrite功能 比 Nginx 的强大;模块超多,基本想到的都可以找到;Bug 少,Nginx 的 Bug 相对较多;超稳定,Aapche 依然是大部分公司的首先,因为其成熟的技术和开发社区,以及非常不错的性能。
nginx的http七层代理,其实它工作在OSI七层模型的应用层。由于其可以解析http协议,可以根据URI进行请求的分发,具有很大的灵活性,但是协议的解析存在性能的消耗。为了能获取更高的代理性能,nginx支持了四层代理,即传输层,就是我们常说的TCP/UDP层,没有协议解析,就是简单的TCP/UDP转发,代理性能突飞猛进,该功能依赖于ngx_http_upstream_module和ngx_stream_upstream_module,互联网公司将其作为入口代理来工作。
[root@localhost opt]# systemctl stop firewalld
[root@localhost opt]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost opt]# setenforce 0
#nginx的配置及运行需要pcre、zlib等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件。
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
#Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限
useradd -M -s /sbin/nologin nginx
[root@king opt]# tar zxf nginx-1.12.2.tar.gz #解包
[root@king opt]# cd nginx-1.12.2/
[root@king nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \ #指定nginx的安装路径
> --user=nginx \ #指定用户名
> --group=nginx \ #指定组名
> --with-http_stub_status_module #启用 http_stub_status_module 模块以支持状态统计操作
make -j2 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #让系统识别nginx的操作命令
nginx -t #检查配置文件是否配置正确
#启动
nginx
#停止
cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID号
kill -3
kill -s QUIT
killall -3 nginx
killall -s QUIT nginx
#重载
kill -1
kill -s HUP
killall -1 nginx
killall -s HUP nginx
#日志分割,重新打开日志文件
kill -USR1
#平滑升级
kill -USR2
信号编号 | 信号名 | 含义 |
---|---|---|
0 | EXIT | 程序退出时收到该信息。 |
1 | HUP | 挂掉电话线或终端连接的挂起信号,这个信号也会造成某些进程在没有终止的情况下重新初始化。 |
2 | INT | 表示结束进程,但并不是强制性的,常用的 “Ctrl+C” 组合键发出就是一个 kill -2 的信号。 |
3 | QUIT | 退出。 |
9 | KILL | 杀死进程,即强制结束进程。 |
11 | SEGV | 段错误。 |
15 | TERM | 正常结束进程,是 kill 命令的默认信号 |
[root@king opt]# tar zxf nginx-1.22.0.tar.gz
[root@king opt]# cd nginx-1.22.0/
[root@king nginx-1.22.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module
yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel
./configure #依赖包安装完之后,重新执行启动命令
make #再次进行编译
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
cp objs/nginx /usr/local/nginx/sbin/nginx
nginx
nginx -v
方法一:
vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
chkconfig --add nginx #添加为系统服务
systemctl stop nginx
systemctl start nginx
service nginx start|stop|restart|reload
系统服务 服务名称 启动模式
方法二:
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
===============================================================================
[Unit]:服务的说明
Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务
[Service]服务运行参数的设置
Type=forking是后台运行的形式,使用此启动类型应同时指定
PIDFile以便systemd能够跟踪服务的主进程。
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径
[Install]服务安装的相关设置,可设置为多用户
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
#user nobody; #运行用户,若编译时未指定则默认为 nobody
worker_processes 1; #工作进程数量,可配置成服务器内核数 * 2
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID 文件的位置
events {
use epoll; #使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 4096; #每个进程处理 4096 个连接
}
#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.
/etc/security/limits.conf
#epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。
若工作进程数为 8,每个进程处理 4 096 个连接,则允许 Nginx 正常提供服务的连接数已超过 3 万个(4 096×8=32 768),当然具体还要看服务器硬件、网络带宽等物理条件的性能表现。
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;
##此选项允许或禁止使用socke的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush on;
##连接保持超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
##gzip模块设置,设置是否开启gzip压缩输出
#gzip on;
##Web 服务的监听配置
server {
##监听地址及端口
listen 80;
##站点域名,可以有多个,用空格隔开
server_name www.lic.com;
##网页的默认字符集
charset utf-8;
##根目录配置
location / {
##网站根目录的位置/usr/local/nginx/html
root html;
##默认首页文件名
index index.html index.htm;
}
##内部错误的反馈页面
error_page 500 502 503 504 /50x.html;
##错误页面配置
location = /50x.html {
root html;
}
}
}
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
location常见配置指令,root、alias、proxy_pass
root(根路径配置):root /var/www/html
请求www.kgc.com/test/1.html,会返回文件/var/www/html/test/1.html
alias(别名配置):alias /var/www/html
请求www.kgc.com/test/1.html,会返回文件/var/www/html/1.html
proxy_pass(反向代理配置)
/usr/local/nginx/sbin/nginx -V #查看已安装的Nginx是否包含HTTP_STUB_STATUS模块
cat /opt/nginx-1.20.2/auto/options | grep YES #可查看nginx已安装的所有模块
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak #复制备份
vim /usr/local/nginx/conf/nginx.conf #编辑主配置文件
......
http {
......
server {
listen 80;
server_name www.jxx.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
##添加 stub_status 配置##
location /status { #访问位置为/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
}
}
systemct restart nginx
浏览器访问20.0.0.55/status
Active connection : 表示当前的活动连接数;
server accepts handled requests:表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、已处理的请求数。
可curl -s http://20.0.0.55/status 结合awk与if语句进行性能监控。
[root@localhost conf]# yum install -y httpd-tools
[root@localhost conf]# htpasswd -c /usr/local/nginx/passwd.db jiaodie
New password:
Re-type new password:
Adding password for user jiaodie
[root@localhost conf]# chown nginx /usr/local/nginx/passwd.db
[root@localhost conf]# chmod 400 /usr/local/nginx/passwd.db
server {
36 listen 80;
37 server_name www.jxx.com;
38
39 charset utf-8;
40
41 #access_log logs/host.access.log main;
42
43 location / {
44 auth_basic "secret";
45 auth_basic_user_file /usr/local/nginx/passwd.db;
46 root html;
47 index index.html index.htm;
48 }
49 location /status{
50 stub_status on;
51 access_log off;
52 }
[root@localhost conf]# nginx -t
[root@localhost conf]# systemctl restart nginx.service
浏览器访问 20.0.0.55
deny IP/IP段:拒绝某个IP或IP段的客户端访问。
allow IP/IP段:允许某个IP 或IP段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# systemctl restart nginx.service
或:
echo "20.0.0.55 www.huigenb.com www.jxlj.com" >> /etc/hosts
mkdir -p /var/www/html/huigenb
mkdir -p /var/www/html/jxlj
echo "www.huigenb.com
" > /var/www/html/huigenb/index.html
echo "www.jxlj.com
" > /var/www/html/jxlj/index.html
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.huigenb.com;
charset utf-8;
access_log logs/www.huigenb.access.log;
location / {
root /var/www/html/huigenb;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.jxlj.com;
charset utf-8;
access_log logs/www.jxlj.access.log;
location / {
root /var/www/html/jxlj;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost huigeNB]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost huigeNB]# systemctl restart nginx.service
ifconfig ens33:0 20.0.0.10 netmask 255.255.255.0
vim /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.55:80;
server_name www.huigenb.com;
charset utf-8;
access_log logs/www.huigenb.access.log;
location / {
root /var/www/html/huigenb;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 20.0.0.10:80;
server_name www.jxlj.com;
charset utf-8;
access_log logs/www.jxlj.access.log;
location / {
root /var/www/html/jxlj;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost huigeNB]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost huigeNB]# systemctl restart nginx.service
vim /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.55:80;
server_name www.huigenb.com;
charset utf-8;
access_log logs/www.huigenb.access.log;
location / {
root /var/www/html/huigenb;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 20.0.0.55:8080;
server_name www.jxlj.com;
charset utf-8;
access_log logs/www.jxlj.access.log;
location / {
root /var/www/html/jxlj;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost huigeNB]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost huigeNB]# systemctl restart nginx.service
server {
listen 20.0.0.55:8080;
server_name www.jxlj.com;
charset utf-8;
access_log logs/www.jxlj.access.log;
location / {
root /var/www/html/jxlj;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
###### 浏览器验证:
[root@localhost huigeNB]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost huigeNB]# systemctl restart nginx.service
[外链图片转存中...(img-YrBpR1LX-1658644838832)]
[外链图片转存中...(img-ugWeubOo-1658644838832)]