这里写目录标题
一、Nginx定义
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器,并在一个BSD-like协议下发行。由俄罗斯的程序设计师lgor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好
Nginx相较于Apache\lighttpd具有占有内存少,稳定性高等优势,并且依靠并发能力强,丰富的模块库以及友好灵活的配置而闻名。在Linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作系统上采用类似于Epoll的高效事件模型kqueue
nginx特点:
- 稳定性高
- 系统资源消耗低
- 对HTTP并发连接的处理能力高
- 单台物理服务器可支持30 000~50 000个并发请求
二、编译安装Nginx
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar zxf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module ###统计状态模块
[root@localhost nginx-1.12.2]# make && make install
[root@localhost ~]# useradd -M -s /sbin/nologin nginx ###创建一个不可登录的程序用户
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin ###优化执行路径
[root@localhost ~]# nginx ###启动服务
[root@localhost ~]# netstat -anpt | grep nginx ###查看nginx服务是否开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16429/nginx: master
[root@localhost ~]# killall -s QUIT nginx ###选项-s QUIT等于-3 停止服务
[root@localhost ~]# netstat -anpt | grep nginx
[root@localhost ~]# killall -s HUP nginx ###选项-s HUP等于-1 重新加载
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16488/nginx: master
[root@localhost ~]# vi /etc/init.d/nginx ###制作管理脚本
#!/bin/bash
#chkconfig: 35 20 80
#description: nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
killall -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
killall -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|reload|status}"
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
在浏览器访问
2.1nginx访问状态统计
1、修改配置文件
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx ###修改#user nobody为user nginx nginx
error_log logs/error.log info ###去除#号
events {
use epoll; ###新增此行,默认使用select/poll
worker_connections 1024; ###表示一个工作进程允许1024个连接
}
location ~ /status { ###配置统计功能
stub_status on;
access_log off;
} ###在server模块里的error_page上面增加
[root@localhost ~]# 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 ~]# nginx -V ###查看版本号及开启的模块
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost ~]# killall -s HUP nginx ###重载nginx的配置文件
[root@localhost ~]# systemctl start nginx ###开启nginx服务
可以在查看的状态
active connections:活跃的连接数量;
server accepts handled requests:总共处理n个连接,成功创建n次握手,共处理n个请求;
reading:读取客户端的连接数;
writing:响应数据到客户端的数量;
waiting:开启keep-alive的情况下,这个值等于active-(reading+writing),意思就是Nginx已经处理完正在等候下一次指令的驻留地址。
2.2Nginx访问控制实验
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/.passwd.db jifan
New password:
Re-type new password:
Adding password for user jifan
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# cat .passwd.db ##查看存放用户名和密码的文件
test:$apr1$vHVaACQT$i1sRjEd2M59E4EJfpxliA.
[root@localhost ~]# chmod 400 /usr/local/nginx/.passwd.db ## 修改密码文件的权限为400
[root@localhost ~]# chown nginx /usr/local/nginx/.passwd.db
将所有者修改为 nginx ,设置nginx的运行用户能够读取
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
allow 20.0.0.13/24; ###允许本机访问
deny all; ###拒绝所有
auth_basic "secret"; ###验证方式为密码验证
auth_basic_user_file /usr/local/nginx/passwd.db; ###密码所在文件
}
三、创建nginx的虚拟机
3.1 基于域名的虚拟主机
1、修改配置文件
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.aa.com; ##添加的域名
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/aa; ##网页所在的地址
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 80;
server_name www.ab.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/ab;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# 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 ~]# systemctl restart nginx ###重启nginx服务
[root@localhost ~]# vi /etc/hosts
20.0.0.11 www.aa.com www.ab.com
[root@localhost ~]# mkdir -p /var/www/aa
[root@localhost ~]# mkdir -p /var/www/ab
[root@localhost aa]# vi index.html
<html><body><h1>This is test1!</h1></body></html>
[root@localhost ab]# vi index.html
<html><body><h1>This is test2!</h1></body></html>
3.2 基于IP地址的虚拟主机
(1)添加一张网卡
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.13:80; ##添加IP
server_name www.aa.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/aa;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 192.168.100.13:80; ##添加的另一个网卡网址
server_name www.ab.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/ab;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# 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 ~]# systemctl restart nginx
[root@localhost ~]# vi /etc/hosts
20.0.0.11 www.aa.com
192.168.100.13 www.ab.com
注意!!!!!
如果输入的两个IP打开的是同一个网址的话,并且配置文件没有错误的情况下!我们需要先
关闭 killall -s QUIT nginx,在开启 nginx,然后再重新刷新配置文件 killall -s HUP nginx ,在用浏览器测试就没有问题了
3.3 基于端口的虚拟主机
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.13:80; ##添加IP
server_name www.aa.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/aa;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 192.168.100.13:8080; ##添加的另一个网卡网址
server_name www.ab.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/ab;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# systemctl restart nginx