Nginx,是一款高性能、 轻量级Web服务软件、稳定性高系统资源消耗低的服务,对HTTP并发连接的处理能力高,单台物理服务器可支持30000 ~ 50000个并发请求。ngnix接收到动态资源请求时,可使用PHP中的FPM模块相互配合,或者将访问PHP页面的Web请求转交给Apache服务器去处理。
相比Apache, Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。
既可以在内部直接支持Rails和PHP,也可以支持作为HTTP代理服务器对外进行服务。Nginx 采用C语言进行编写,不论是系统资源开销还是CPU使用效率都比Perlbal 要好的多。
Nginx同时也是-一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。Nginx安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。
[root@localhost ~]# systemctl stop firewalld 关闭防火墙
[root@localhost ~]# setenforce 0
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.10.53/share /abc 把windows共享出的share文件 挂载到 /abc下
[root@localhost ~]# cd /abc
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt 解压源码包
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ pcre* expat-devel zlib-devel -y 下载nginx所需的的环境包
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module 开启stub_status状态统计模块
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin 创建软连接 让系统识别命令
[root@localhost nginx-1.12.2]# nginx -t 检查配置文件语法是否正确
[root@localhost nginx-1.12.2]# cd /etc/init.d
[root@localhost init.d]# vim nginx 创建编辑制作管理脚本
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x nginx 给nginx执行权限
[root@localhost init.d]# chkconfig --add nginx 把nginx加入service管理器 ,被所能管理
[root@localhost init.d]# chkconfig --level 35 nginx on 设置开机自启动
[root@localhost init.d]# service nginx start 开启nginx服务
[root@localhost init.d]# netstat -ntap | grep nginx 查看nginx端口是否开启
[root@localhost conf]# yum install bind -y 下载DNS的软件包
[root@localhost conf]# vim /etc/named.conf 更改主配置文件
listen-on port 53 { any; };
allow-query { any; };
[root@localhost conf]# vim /etc/named.rfc1912.zones 更改区域配置文件
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
[root@localhost conf]# cd /var/named
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone
NS @
A 127.0.0.1
www IN A 192.168.17.157
[root@localhost named]# cp -p kgc.com.zone accp.com.zone
[root@localhost named]# systemctl start named
[root@localhost named]# cd /usr/local/nginx/conf
[root@localhost conf]# mv nginx.conf nginx.conf.bak
[root@localhost conf]# grep -v "#" nginx.conf.bak > nginx.conf
[root@localhost conf]# vim nginx.conf
/50x.htmli:
“ 在 location = /50x.html {
root html;
}
}
" 后添加下面一段:
server {
server_name www.kgc.com;
location / {
root /var/www/kgc;
index index.html index.htm;
}
}
server {
server_name www.accp.com;
location / {
root /var/www/accp;
index index.html index.htm;
}
}
[root@localhost named]# cd /var
[root@localhost var]# mkdir -p www/kgc www/accp
[root@localhost var]# echo "this is kgc" > www/kgc/index.html
[root@localhost var]# echo "this is accp" > www/accp/index.html
[root@localhost var]# service nginx restart
[root@localhost init.d]# yum install bind -y
[root@localhost init.d]# vim /etc/named.conf
listen-on port 53 { any; };
allow-query { any; };
[root@localhost init.d]# vim /etc/named.rfc1912.zones
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
[root@localhost init.d]# cd /var/named
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone
NS @
A 127.0.0.1
www IN A 192.168.17.157
[root@localhost named]# systemctl start named
[root@localhost named]# cd /usr/local/nginx/conf
[root@localhost conf]# ln -s /usr/local/nginx/conf/nginx.conf /etc/
[root@localhost conf]# vim /etc/nginx.conf
/50x.html查找:
在 “ location = /50x.html {
root html;
}
}
” 后添加下面一段:
server {
listen 192.168.17.129:80;
server_name www.kgc.com;
location / {
root /var/www/kgc;
index index.html index.htm;
}
}
server {
listen 192.168.17.129:8080;
server_name www.kgc.com;
location / {
root /var/www/kgc02;
index index.html index.htm;
}
}
[root@localhost named]# cd /var
[root@localhost var]# mkdir -p www/kgc www/kgc02
[root@localhost var]# cd www
[root@localhost www]# ls
kgc kgc02
[root@localhost www]# echo "this is kgc" > kgc/index.html
[root@localhost www]# echo "this is a kgc0202" > kgc02/index.html
[root@localhost www]# systemctl start named
[root@localhost www]# service nginx stop
[root@localhost www]# service nginx start
[root@localhost init.d] cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
/50x.html查找:
在 “ location = /50x.html {
root html;
}
} ”
后添加下面一段:
server {
listen 192.168.17.129:80;
location / {
root /var/www/kgc;
index index.html index.htm;
}
}
server {
listen 192.168.17.234:80;
location / {
root /var/www/accp;
index index.html index.htm;
}
}
[root@localhost conf]# cd /var/
[root@localhost var]# mkdir -p www/kgc www/accp 递归创建站点目录
[root@localhost var]# cd www
[root@localhost www]# echo "this is kgckgc" > kgc/index.html 编辑写入首页内容
[root@localhost www]# echo "this is accpaccp" > accp/index.html 编辑写入首页内容
[root@localhost www]# service nginx restart
[root@localhost www]# netstat -ntap | grep nginx
Nginx本身做的工作实际很少,当它接到一个HTTP 请求时,它仅仅 是通过查找配置文件将此次请求映射到一个location block,而此location 中所配置的各个指令则会启动不同的模块去完成工作,因此模块可以看做Nginx 真正的劳动工作者。
通常一个location 中的指令会涉及一个handler 模块和多个filter 模块(当然,多个location可以复用同一个模块)。handler模块负责处理请求,完成响应内容的生成,而filter模块对响应内容进行处理。用户根据 自己的需要所开发的模块都属于第三方模块。正是有了这么多模块的支撑,Nginx 的功能才会如此强大。
核心模块: HTTP模块、EVENT模块和MAIL模块;
基础模块: HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块;
第三方模块: HTTP Upstream RequestHash模块、Notice 模块和HTTP AccessKey模块。
Nginx的模块从功能上分为如下三类:
Handlers (处理器模块):此类模块直接处理请求,并进行输出内容和修改headers 信息等操作。Handlers处理器模块一般只 能有一一个; Filters (过滤器模块):此类模块主要对其他处理器模块输出的内容进行修改操作,最后由Nginx 输出; Proxies (代理类模块):此类模块是Nginx 的HTTP Upstream之类的模块,这些模块主要与后端一些服务比如FastCGI等进行交互,实现服务代理和负载均衡等功能。