Nginx服务
Nginx服务基础
Nginx是一款高性能、轻量级Web服务软件。它具有下面特点:
- 稳定性高
- 系统资源消耗低
- 对HTTP并发连接的处理能力高
- 单台物理服务器可支持30 000~50 000个并发请求
nginx和apache区别:消耗同样的资源处理更多的事。
Nginx编译安装
准备工作
关闭防火墙,配置域名
systemctl stop firewalld.service .
systemctl disable firewalld.service
setenforce 0
vim /etc/resolv.conf
上传nginx软件包并安装依赖包
[root@www opt]# yum -y install gcc gcc-c++ pcre-devel zlib-devel make
解压并安装Nginx
[root@www opt]# tar zxvf nginx-1.12.2.tar.gz
[root@www opt]# cd nginx-1.12.2/
[root@www nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \ //管理用户的身份
--group=nginx \ //管理用户的组
--with-http_stub_status_module //http状态统计模块
[root@www nginx-1.12.2]# make && make install
检查、启用、添加系统管理
创建nginx用户
[root@www nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@www nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@www nginx-1.12.2]# ll /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 6月 22 23:54 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@www 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
启动nginx服务,启动前需要检查httpd服务是否关闭。否则打不开。
[root@www nginx-1.12.2]# nginx
[root@www nginx-1.12.2]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 45254/nginx: master
停止nginx 服务
[root@www nginx-1.12.2]# cat /usr/local/nginx/logs/nginx.pid //首先查看nginx的PID号
45254
[root@www nginx-1.12.2]# kill -s QUIT <PID号> //友好杀死,等同于-3
重载
kill -s HUP <PID号> //等同于 -1
kill 选项
-s:指定信号种类
HUP:重载配置
QUIT:退出进程
添加nginx服务为系统控制(systemctl和service)
方式一:编译脚本设置nginx服务
[root@www nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#这是一个控制nginx服务运行的脚本
NGINX="/usr/local/nginx/sbin/nginx" #nginx开启命令位置
PID="/usr/local/nginx/logs/nginx.pid" #nginx服务进程PID位置
case "$1" in
start)
$NGINX;; #开启nginx
stop)
kill -s QUIT $(cat $PID);; #杀死nginx进程号,停止服务
restart)
$0 stop && $0 start;; #重启服务
reload)
kill -s HUP $(cat $PID);; #重载服务
*)
echo "正确输入:$0 {start|stop|restart|reload}"
exit 1
esac
exit 0
添加权限
chmod +x /etc/init.d/nginx
[root@www nginx-1.12.2]# chkconfig --add nginx //添加为系统服务
方式二:使用systemctl管理
[root@www nginx]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx ##描述
After=network.target ##描述服务类别
[Service]
Type=forking ##后台运行类型
PIDFile =/usr/local/nginx/logs/nginx.pid ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx ##启动服务
ExecReload=/bin/kill -s HUP $MAINPID ##根据PID重载配置
ExecStop=/bin/kill -s QUIT $MAINPID ##根据PID终止进程
PrivateTmp=true ##开启
[Install]
WantedBy=multi-user.targe ##启动级别
[root@www nginx]# chmod 754 /lib/systemd/system/nginx.service
#设置754权限是一种安全优化
在浏览器上测试查看
Nginx配置文件
[root@www nginx]# vim /usr/local/nginx/conf/nginx.conf
全局配置
I/O时间配置
可通过以下命令查看连接数
ulimit -a :查看进程的最大连接数数
ulimit -n :临时调整进程最大连接数
HTTP配置
访问状态统计
[root@www conf]# vim nginx.conf
修改主机域名
本地域名映射
[root@www conf]# echo "192.168.233.105 www.kyxzw.com" >> /etc/hosts
//配置本地域名映射
Nginx虚拟主机
基于域名的虚拟主机
1.添加域名
[root@www ~]# vim /etc/hosts
同一个IP可以匹配多个域名
2.创建benet和accp网页文件
[root@www ~]# mkdir /usr/local/nginx/html/benet
[root@www ~]# vim /usr/local/nginx/html/benet/index.html
<h1> www.benet.com </h1>
[root@www ~]# mkdir /usr/local/nginx/html/accp
[root@www ~]# vim /usr/local/nginx/html/accp/index.html
<h1> www.accp.com </h1>
3.修改配置文件,把配置文件中的server{}中编写
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log; //日志文件
location / {
root /usr/local/nginx/html/benet; //工作目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.access.log;
location / {
root /usr/local/nginx/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
检测配置文件的语法
[root@www ~]# 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
4.访问域名
基于IP的虚拟主机
1)添加虚拟IP地址
[root@www ~]# ifconfig ens33:0 192.168.233.115
[root@www ~]# ifconfig
2)修改nginx配置文件
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.233.105:80;
server_name 192.168.233.105:80;
......
server {
listen 192.168.233.115:80;
server_name 192.168.233.115:80;
......
4)访问网页
基于端口的虚拟主机
1)选择不同的端口,将多个端口映射到同一个IP地址
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.233.105:80;
server_name 192.168.233.105:80;
......
server {
listen 192.168.233.105:8080;
server_name 192.168.233.105:8080;
......
2)访问网页
Nginx访问控制
基于授权的访问控制
1)使用htpasswd生成用户密码认证文件
[root@www ~]# yum -y install httpd-tools.x86_64 // htpasswd命令需要下载httpd-tools软件包
[root@www ~]# htpasswd -c /usr/local/nginx/passwd.db xzw //生成用户密码认证文件,该用户可以是非系统用户
New password:
Re-type new password:
Adding password for user xzw
[root@www ~]# cat /usr/local/nginx/passwd.db
xzw:$apr1$kJmR.sGI$QWR7pEKX1r7GK1aN6p6pk0
2)修改密码权限
[root@www ~]# chmod 400 /usr/local/nginx/passwd.db
[root@www ~]# chown nginx /usr/local/nginx/passwd.db
[root@www ~]# ll -d /usr/local/nginx/passwd.db
-r--------. 1 nginx root 42 6月 23 17:40 /usr/local/nginx/passwd.db
3)修改主配置文件nginx.conf
location / {
root /usr/local/nginx/html/benet;
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
index index.html index.htm;
}
location / {
root /usr/local/nginx/html/accp;
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
index index.html index.htm;
}
4)重启,访问网页
[root@www ~]# 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@www ~]# systemctl restart nginx
基于客户端的访问控制
查看本机的IP和虚拟机win10的IP地址
本机IP
虚拟机IP
1)修改主配置文件nginx.conf
listen 192.168.233.105:80;
server_name 192.168.233.105:80;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /usr/local/nginx/html/benet;
index index.html index.htm;
deny 192.168.233.150; //拒绝192.168.233.150
allow all; //放行其他所有IP
2)重启服务器,访问网址。
[root@www ~]# systemctl restart nginx
本机:192.168.233.1
虚拟机:192.168.233.150
8080网段我没有设置访问控制。因此虚拟机可以访问。