1、Nginx简介
Nginx:
Nginx (“engine x”) 是一个高性能的 HTTP 和反向代理服务器。第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。其将源代码以类 BSD 许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011 年 6 月 1 日,Nginx 1.0.4 发布。
Tengine:
Tengine 是由淘宝网发起的 Web 服务器项目。它在 Nginx 的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine 的性能和稳定性已经在大型的网站如淘宝网、天猫商城等得到了很好的检验。(可以这样理解:淘宝网拿到了 Nginx 源代码之后,进行了功能的填充、优化等等,然后提交给Nginx 官方,但是由于 Nginx 官方相应慢甚至不响应,加上语言沟通的不顺畅,于是淘宝公司就自己打包,在遵循 GPL 的原则上进行二次开发,于是就出了现在的 Tengine 这个版本)
2、Nginx和Apache的差异:
1:nginx是轻量级的,比apache占用更少的内存及资源,抗并发,占有内存少,并发能力强;
2:nginx不采用每客户机一线程的设计模型,而是充分使用异步非阻塞逻辑,削减了上下文调度开销,而apache 则是阻塞型的;
3: nginx是高度模块化的设计,编写模块比apache相对简单,适合做前端响应服务器;
4:apache模块较多,较全面,在处理动态有优势;
5:apache可以通过简单的API扩展,将Perl/Python等解释器编译到服务器中,快速可靠,所以apache少bug,nginx 的bug 相对较多。
Nginx 应用场景
① 静态服务器 (图片、视频服务、文本)
② 低版本在第七层,高版本在第四、第七层都有
③ 反向代理, 负载均衡
④ 缓存服务
3、编译安装Nginx服务
1.关闭防火墙,将安装Nginx所需软件包传到/opt目录下
[root@ux ~]# systemctl stop firewalld
[root@ux ~]# systemctl disable firewalld
[root@ux ~]# setenforce 0
setenforce: SELinux is disabled
2.安装依赖包
[root@ux ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3.创建运行用户、组(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)
[root@ux ~]# useradd -M -s /sbin/nologin nginx
4.编译安装Nginx
[root@ux ~]# cd /opt
[root@ux opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@ux opt]# cd nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \ #指定nginx的安装路径
--user=nginx \ #指定用户名
--group=nginx \ #指定组名
--with-http_stub_status_module #启用 http_stub_status_module 模块以支持状态统计操作
[root@ux nginx-1.12.2]# make && make install
[root@ux nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #让系统识别nginx的操作命令
5.检查、启动、重启、停止 Nginx服务
[root@ux nginx-1.12.2]# nginx -t #检查配置文件是否配置正确
[root@ux nginx-1.12.2]# nginx #启动
cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID号
#停止
kill -3 <PID号>
kill -s QUIT <PID号>
killall -3 nginx
killall -s QUIT nginx #重载
kill -1 <PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx #日志分割,重新打开日志文件
kill -USR1 <PID号> #平滑升级
kill -USR2 <PID号>
信号编号 | 信号名 | 含义 |
---|---|---|
0 | EXIT | 程序退出时收到该信息。 |
1 | HUP | 挂掉电话线或终端连接的挂起信号,这个信号也会造成某些进程在没有终止的情况下重新初始化。 |
2 | INT | 表示结束进程,但并不是强制性的,常用的 “Ctrl+C” 组合键发出就是一个 kill -2 的信号。 |
3 | QUIT | 退出。 |
9 | KILL | 杀死进程,即强制结束进程。 |
11 | SEGV | 段错误。 |
15 | TERM | 正常结束进程,是 kill 命令的默认信号 |
6.添加 Nginx 系统服务
方法一:
[root@ux nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 99 20 // 这是固定格式,2345表示运行级别,之后为开机执行顺序和关机执行顺序
#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
[root@ux nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@ux nginx-1.12.2]# chkconfig --add nginx #添加为系统服务
[root@ux nginx-1.12.2]# systemctl stop nginx
[root@ux nginx-1.12.2]# systemctl start nginx
下面还有添加Nginx系统服务方法二:
[root@ux nginx-1.23.0]# 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
[root@ux nginx-1.23.0]# chmod 754 /lib/systemd/system/nginx.service
[root@ux nginx-1.23.0]# systemctl start nginx.service
[root@ux nginx-1.23.0]# systemctl enable nginx.service
[ Unit ] :服务的说明
Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务
[Service]服务运行参数的设置
Type=forking是后台运行的形式,使用此启动类型应同时指定
PIDFile以便systemd能够跟踪服务的主进程。
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径
[Install]服务安装的相关设置,可设置为多用户
7.Nginx新版本升级
[root@ux opt]# tar -zxvf nginx-1.23.0.tar.gz
[root@ux opt]# cd nginx-1.23.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module
这里显示not found,是因为没有安装openssl openssl-devel,所以会编译失败
安装openssl openssl-devel
[root@ux nginx-1.23.0]# yum install openssl openssl-devel -y
[root@ux nginx-1.23.0]# make
[root@ux nginx-1.23.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old #备份
[root@ux nginx-1.23.0]# cp objs/nginx /usr/local/nginx/sbin/nginx
重启服务
[root@ux nginx-1.23.0]# nginx -V #查看版本
这就说明Nginx系统版本升级成功!
--------认识Nginx服务的主配置文件 nginx.conf--------
vim /usr/local/nginx/conf/nginx.conf
1.全局配置
#user nobody; #运行用户,若编译时未指定则默认为 nobody
worker_processes 4; #工作进程数量,可配置成服务器内核数 * 2,如果网站访问量不大,一般设为1就够用了
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID 文件的位置
2.I/O 事件配置
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),当然具体还要看服务器硬件、网络带宽等物理条件的性
能表现。
3.HTTP 配置
使用“http { }”界定标记,包括访问日志、HTTP 端口、网页目录、默认字符集、连接保
持,以及后面要讲到的虚拟 Web 主机、PHP 解析等一系列设置,其中大部分配置语句都包
含在子界定标记“server { }”内
http {
##文件扩展名与文件类型映射表
include mime.types;
##默认文件类型
default_type application/octet-stream;
##日志格式设定
#log_format main '$remote_addr -
r
e
m
o
t
e
u
s
e
r
[
remote_user [
remoteuser[time_local] “KaTeX parse error: Expected 'EOF', got '#' at position 16: request" ' #̲ …status
b
o
d
y
b
y
t
e
s
s
e
n
t
"
body_bytes_sent "
bodybytessent"http_referer” ’
# ‘“
h
t
t
p
u
s
e
r
a
g
e
n
t
"
"
http_user_agent" "
httpuseragent""http_x_forwarded_for”’;
##访问日志位置
#access_log logs/access.log main;
##支持文件发送(下载)
sendfile on;
##此选项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush on;
##连接保持超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
##gzip模块设置,设置是否开启gzip压缩输出
#gzip on;
##Web 服务的监听配置
server {
##监听地址及端口
listen 80;
##站点域名,可以有多个,用空格隔开
server_name www.kgc.com;
##网页的默认字符集
charset utf-8;
##根目录配置
location / {
##网站根目录的位置/usr/local/nginx/html
root html;
##默认首页文件名
index index.html index.php;
}
##内部错误的反馈页面
error_page 500 502 503 504 /50x.html;
##错误页面配置
location = /50x.html {
root html;
}
}
}
日志格式设定:
r
e
m
o
t
e
a
d
d
r
与
remote_addr与
remoteaddr与http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
h
t
t
p
u
s
e
r
a
g
e
n
t
:记录客户浏览器的相关信息;通常
w
e
b
服务器放在反向代理的后面,这样就不能获取到客户的
I
P
地址了,通过
http_user_agent:记录客户浏览器的相关信息; 通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过
httpuseragent:记录客户浏览器的相关信息;通常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
dd拿到的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(反向代理配置)
1、访问状态配置设置
[root@ux conf]# cd /usr/local/nginx/conf/
[root@ux conf]# vim nginx.conf
##添加stub_status 配置##
location / status { #访问位置为/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
[root@ux conf]# systemctl restart nginx.service #重启服务,访问测试
浏览器访问 192.168.80.10/status
Active connections :表示当前的活动连接数;主动连接:表示当前的活动连接数;
server accepts handled requests :表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的nce握手次数、己处理的请求数。
2、基于授权的访问控制
1.生成用户密码认证文件
[root@ux ~]# cd /usr/local/nginx
[root@ux nginx]# yum install -y httpd-tools
[root@ux nginx]# htpasswd -c /usr/local/nginx/passwd.db zhangsan
[root@ux nginx]# chown nginx /usr/local/nginx/pssswd.db
[root@ux nginx]# chmod 400 /usr/local/nginx/passwd.db
2.修改主配置文件相对应目录,添加认证配置项2.修改主配置文件相对应目录,添加认证配置项
[root@ux conf]# cd /usr/local/nginx/conf/
[root@ux conf]# vim nginx.conf
location / {
......
##添加认证配置##
auth_basic "secret"; #设置密码提示框文字信息
auth_basic_user_file /usr/local/nginx/passwd.db;
}
[root@ux conf]# nginx -t #重启服务,访问测试
[root@ux conf]# systemctl restart nginx.service
点击取消以后,会出现”401“提示
点击确定以后,输入账号密码
3、基于客户端的访问控制
访问控制规则如下:
deny IP/IP段:拒绝某个IP或IP段的客户端访问。
allow IP/IP段:允许某个IP或IP段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。
[root@ux conf]# cd /usr/local/nginx/conf/
[root@ux conf]# vim nginx.conf
location / {
......
##添加控制规则##
allow 192.168.80.10; #允许访问的客户端ip
deny all; #拒绝其他ip客户端访问
}
[root@ux conf]# nginx -t #重启服务,访问测试
[root@ux conf]# systemctl restart nginx.service
4、基于域名的 Nginx虚拟主机
1.为虚拟主机提供域名解析,为虚拟主机准备网页文档
[root@ux conf]# echo "192.168.80.10 www.kgc.com www.benet.com" >> /etc/hosts
[root@ux conf]# mkdir -p /var/www/html/benet
[root@ux conf]# mkdir -p /var/www/html/kgc
[root@ux conf]# echo "<h1>www.kgc.com</h1>" > /var/www/html/kgc/index.html
[root@ux conf]# echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html
2.修改nginx的配置文件
[root@ux conf]# cd /usr/local/nginx/conf/
[root@ux conf]# vim nginx.conf
http {
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/html/kgc;
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.benet.com;
charset utf-8;
access_log logs/www.benet.com.access.log;
location / {
root /var/www/html/benet;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@ux conf]# nginx -t #重启服务,访问测试
[root@ux conf]# systemctl restart nginx.service
5、基于IP的Nginx虚拟主机
[root@ux conf]# ifconfig ens33:0 192.168.80.80 netmask 255.255.255.0
[root@ux conf]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.80.10 netmask 255.255.255.0 broadcast 192.168.80.255
inet6 fe80::d147:1c45:4ac2:43f7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:a9:29:d7 txqueuelen 1000 (Ethernet)
RX packets 5109 bytes 493268 (481.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4190 bytes 618166 (603.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.80.80 netmask 255.255.255.0 broadcast 192.168.
[root@ux conf]# cd /usr/local/nginx/conf/
[root@ux conf]# vim nginx.conf
http {
server {
listen 192.168.80.10:80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/html/kgc;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.80.80:80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.com.access.log;
location / {
root /var/www/html/benet;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@ux conf]# nginx -t #重启服务,访问测试
[root@ux conf]# systemctl restart nginx.service
6、基于端口的 Nginx虚拟主机
[root@ux conf]# cd /usr/local/nginx/conf/
[root@ux conf]# vim nginx.conf
http {
server {
listen 192.168.80.10:8080;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/html/kgc;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.80.10:8888;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.com.access.log;
location / {
root /var/www/html/benet;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@ux conf]# nginx -t #重启服务,访问测试
[root@ux conf]# systemctl restart nginx.service