概述
Nginx(engine x)是一个高性能的HTTP和反向代理Web服务器,同时也提供了IMAP/POP3/SMTP服务。
其将源代码以类BSD许可证的形式发布,具有稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗。
Nginx是一款轻量级的Web服务器反向代理服务器及电子邮件代理服务器,在BSD-like协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
特点优点
1)可以高并发连接
2)内存消耗少
3)成本低廉
4)配置文件非常简单
5)支持Rewrite重写
6)内置的健康检查功能
7)节省带宽
8)稳定性高
9)支持热部署
手工编译安装Nginx
//
yum -y install gcc gcc-c++ pcre-devel zlib-devel make
安装编译安装包,开发包,工具
把Nginx-1.12.2压缩包拉进来解压;
//
[root@localhost opt]# ls
nginx-1.12.2 nginx-1.12.2.tar.gz rh
[root@localhost opt]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \ ##安装路径
--user=nginx \ ##指定用户管理
--group=nginx \ ##指定一个组
--with-http_stub_status_moduleuser=nginx --group=nginx --with-http_stub_status_mod ##开启状态统计模块
[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]# useradd -M -s /sbin/nologin nginx ##创建一个不指定家目录的用户
[root@localhost nginx-1.12.2]# 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-1.12.2]# nginx ##启动
;
添加Nginx系统服务
//
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20 ##“-”表示不启用开机启动管理 加“#”是为了chkconfig add nginx 会加载到配置
#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@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx ##提权
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# systemctl stop nginx
[root@localhost nginx-1.12.2]# systemctl start nginx
Nginx配置文件
//
[root@localhost nginx-1.12.2]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak ##备份一下
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
;
映射本地
通过虚拟机浏览器访问域名www.wk.com
访问状态统计
//
[root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -V ##查看已安装的Nginx是否包含http_stub_status_module
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.bak
cp:是否覆盖"nginx.conf.bak"? y
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf;
[root@localhost conf]# systemctl restart nginx.service
浏览器访问 192.168.142.131/status 或 www.wk.com/status
访问控制
设置加密密码
下载生成用户密码认证文件
//
[root@localhost conf]# yum install -y httpd-tools
已安装:
httpd-tools.x86_64 0:2.4.6-97.el7.centos
完毕!
[root@localhost conf]# htpasswd -c /usr/local/nginx/passwd.db wangkang
New password: ##密码
Re-type new password: ##再次输入
Adding password for user wangkang
[root@localhost conf]# chown nginx /usr/local/nginx/passwd.db ##添加nginx管理
[root@localhost conf]# chmod 400 /usr/local/nginx/passwd.db ##设置400权限
修改主配置文件相对应目录,添加认证配置项
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
//
[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
;
浏览器访问 www.wk.com
基于客户端访问控制
vim /usr/local/nginx/conf/nginx.conf
重启Nginx前可以访问
//
[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;
重启Nginx后,不可以访问
其她ip正常访问139.168.142.131
虚拟主机
基于域名访问
//
[root@localhost conf]# vim /etc/hosts;
新建虚拟站点网页文档
//
[root@localhost conf]# mkdir -p /var/www/html/123
[root@localhost conf]# mkdir -p /var/www/html/qwe
[root@localhost conf]# cd /var/www/html/123
[root@localhost 123]# vim index.html
[root@localhost 123]# cd ../qwe
[root@localhost qwe]# vim index.html
修改主配置文件
//
vim /usr/local/nginx/conf/nginx.conf;
//
[root@localhost qwe]# 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 qwe]# systemctl restart nginx
基于端口的虚拟机
创建8080的目录
//
[root@localhost html]# mkdir 1238080
[root@localhost html]# ls
123 1238080 qwe
[root@localhost html]# vim index.html
写入 <h1>www.1238080.com</h1>
进入主配置文件
//
[root@localhost html]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf nginx.conf
[root@localhost conf]# vim nginx.conf
//
[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
基于不同IP访问
创建网站根目录
//
[root@localhost 1238080]# mkdir /var/www/html/qwe100
[root@localhost 1238080]# cd ../qwe100
[root@localhost qwe100]# vim index.html
进入域名解析,删除之前同域名映射
//
[root@localhost qwe100]# vim /etc/hosts
//
[root@localhost qwe100]# ifconfig ens33:0 192.168.142.100 netmask 255.255.255.0 ##新建虚拟IP
[root@localhost qwe100]# vim /usr/local/nginx/conf/nginx.conf
//
[root@localhost qwe100]# 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 qwe100]# systemctl restart nginx