一款高性能、轻量级Web服务软件
●稳定性高
●系统资源消耗低
●对HTTP并发连接的处理能力高
●单台物理服务器可支持30 000 ~ 50000个并发请求
●占用内存少,并发能力强
[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@localhost ~]# useradd -M -S /sbin/nologin nginx '-M 不创建家目录,-s表示登陆环境'
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_ status_ module '开启stub_status状态统计模块'
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin 'nginx命令执行路径优化'
[root@locaThost nginx-1.12.0]# ls -l /usr/local/sbin/nginx
[root@localhost ~]# nginx -t '检查语法'
[root@localhost ~]# nginx '启动'
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7180/nginx: master
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks http://localhost '显示"Welcome to nginx!"页面,表明Nginx服务已经正常运行'
[root@localhost ~]# killall -s HUP nginx '-s选项指定信号种类,HUP信号表示重载配置'
[root@localhost ~]# killall -s QUIT nginx 'QUIT信号表示退出进程'
[root@localhost ~]# vim /lib/systemd/system/nginx.service '添加使用systemctl工具进行管理'
[Unit]
Description=nginx '描述'
After=network.target '描述服务类别'
[Service]
Type=forking '//后台运行形势'
PIDFile =/usr/local/nginx/logs/nginx.pid 'PID文件位置'
ExecStart=/usr/local/nginx/sbin/nginx '启动服务'
ExecReload=/usr/bin/kill -S HUP $MAINPID '根据PID重载配置'
ExecStop=/usr/bin/kill -S QUIT $MAINPID '根据PID终止进程'
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# cd /etc/inid.d '或者添加使用service工具进行管理'
[root@localhost init.d]# ls
[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
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 35 nginx on
#user nobody;
worker_ processes 1;
#error_ log logs/error.log;
#pid logs/nginx.pid;
events {
use epoll;
worker connections 4096;
}
http {
....
access_log logs/access.log main;
sendfile on;
...
keepalive_ _timeout 65;
server {
listen 80;
server name localhost;
charset utf-8;
location / {
root html;
index index.html index.php; }
error_ page 500 502 503 504 /50x.html;
location = /50x.html {
root html; }}
}
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz -C /opt
[root@localhost ~]# cd /opt/nginx-1.12.2
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ zlib-devel pcre pcre-devel -y
'安装支持软件'
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
'-M表示不创建家目录,-s表示环境'
[root@localhost nginx-1.12.2]# id 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]# vim /etc/init.d/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 '$1表示位置变量'
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 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[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]# chmod +x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# systemctl stop firewalld
[root@localhost nginx-1.12.2]# setenforce 0
配置编译参数时添加–with-http stub status module
nginx -V查看已安装的Nginx是否包含HTTP_ STUB _STATUS模块
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
[root@localhost conf]# vim nginx.conf
location / {
root html;
index index.html index.htm;
}
location /status {
stub_status on;
access_log off;
} '在"server"这里面插入的这4行'
[root@localhost conf]# iptables -F
[root@localhost conf]# systemctl stop firewalld.service
[root@localhost conf]# setenforce 0
Nginx支持的虚拟主机有三种
●基于域名的虚拟主机
●基于IP的虚拟主机
●基于端口的虚拟主机
通过"server{}" 配置段实现
[root@localhost ~]# cd /var/
[root@localhost var]# ls
account cache db games kerberos local log nis preserve spool tmp
adm crash empty gopher lib lock mail opt run target yp
[root@localhost var]# mkdir www
[root@localhost var]# cd www/
[root@localhost www]# ls
[root@localhost www]# mkdir benet kevin
[root@localhost www]# cd benet/
[root@localhost benet]# vim index.html
<h1>this is benet0 web/<h1>
[root@localhost benet]# cd ../kevin/
[root@localhost kevin]# vim index.html
<h1>this is kevin web/<h1>
[root@localhost www]# yum install bind -y
[root@localhost www]# vim /etc/named.conf
[root@localhost www]# vim /etc/named.rfc1912.zones
[root@localhost www]# cd /var/named/
[root@localhost named]# cp -p named.localhost kevin.com.zone
[root@localhost named]# vim kevin.com.zone
[root@localhost named]# cp -p kevin.com.zone benet.com.zone
[root@localhost named]# systemctl start named
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf '修改Nginx.conf配置文件'
server {
server_name www.kevin.com;
location / {
root /var/www/kevin;
index index.html index.php;
}
}
server {
server_name www.benet.com;
location / {
root /var/www/benet;
index index.html index.php;
}
}
[root@localhost named]# service nginx stop
[root@localhost named]# service nginx start
## 4.3:基于端口的虚拟web主机
```handlebars
- 配置步骤
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.47:6666;
server name 20.0.0.47:6666;
......}
server {
listen 20.0.0.47:8888;
server name 20.0.0.47:8888;
......}
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.47:80;
server name 20.0.0.47:80;
....}
server {
listen 20.0.0.51:80;
server name 20.0.0.51:80;
....}
配置步骤与Apache基本一致
●生成用户密码认证文件
●修改主配置文件对相应目录,添加认证配置项
●重启服务,访问测试
[root@localhost conf]# yum install httpd-tools -y
[root@localhost conf]# htpasswd -c /usr/local/nginx/passwd.db test
New password:
Re-type new password:
Adding password for user test
[root@localhost conf]# chmod 400 /usr/local/nginx/passwd.db
[root@localhost conf]# chown nginx /usr/local/nginx/passwd.db
[root@localhost conf]# ll -d /usr/local/nginx/passwd.db
-r--------. 1 nginx root 43 8月 7 15:35 /usr/local/nginx/passwd.db
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
location / {
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
通过客户端IP地址,决定是否允许对页面访问
配置规则
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配
配置步骤
修改主配置文件nginx.conf,添加相应配置项
除主机20.0.0.47之外允许其他客户端访问
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
location/ {
deny 20.0.0.47;
allow all;
}