一、Nginx 服务基础
关于Nginx
一款高性能、轻量级Web服务软件
稳定性高 系统资源消耗低 对HTTP并发连接的处理能力高
单台物理服务器可支持30 000 ~ 50 000个并发请求
实验报告
资源列表
操作系统 配置 主机 IP CentOS7.3.1611 2C4G nginx 192.168.72.154
基础环境
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed - i "s/^SELINUX=.*/SELINUX=disabled/g" / etc/ selinux/ config
hostnamectl set - hostname nginx
一、编译安装Nginx
安装支持软件
yum - y install pcre- devel zlib- devel gcc+ + gcc
创建运行用户、组
useradd - M - s / sbin/ nologin nginx
二、源码编译及安装
1、解包
tar zxf nginx- 1.12 .0 . tar. gz - C / usr/ src/
cd / usr/ src/ nginx- 1.12 .0 /
2、配置编译安装
. / configure - - prefix= / usr/ local/ nginx - - user= nginx - - group= nginx - - with - http_stub_status_module
make & & make install
3、优化执行路径
ln - s / usr/ local/ nginx/ sbin/ nginx / usr/ local/ sbin/
ls - l / usr/ local/ sbin/ nginx
三、Nginx 的运行控制
1、检查配置文件
[ root@nginx ~ ]
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
2、启动、停止 Nginx
[ root@nginx nginx- 1.12 .0 ]
conf html logs sbin
[ root@nginx nginx- 1.12 .0 ]
[ root@nginx nginx- 1.12 .0 ]
LISTEN 0 128 * : 80 * : * users: ( ( "nginx" , pid= 12266 , fd= 6 ) , ( "nginx" , pid= 12265 , fd= 6 ) )
[ root@nginx nginx- 1.12 .0 ]
[ root@nginx nginx- 1.12 .0 ]
3、添加 Nginx 系统服务
[ root@nginx nginx- 1.12 .0 ]
[ root@nginx nginx- 1.12 .0 ]
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
chmod + x / etc/ init. d/ nginx
chkconfig - - add nginx
systemctl status nginx
四、Nginx访问控制
1、访问状态统计
[ root@nginx ~ ]
location / status {
stub_status on;
}
[ root@nginx ~ ]
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@nginx ~ ]
2、基于账号密码的访问控制
yum install - y httpd- tools
htpasswd - c / usr/ local/ nginx/ passwd. db test
chmod 400 / usr/ local/ nginx/ passwd. db
chown nginx / usr/ local/ nginx/ passwd. db
[ root@nginx ~ ]
- r- - - - - - - - 1 nginx root 43 7 月 16 09 : 46 / usr/ local/ nginx/ passwd. db
[ root@nginx ~ ]
auth_basic "secret" ;
auth_basic_user_file / usr/ local/ nginx/ passwd. db;
[ root@nginx ~ ]
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@nginx ~ ]
3、基于客户端的访问控制
[ root@nginx ~ ]
deny 192.168 .72 .1 ;
allow all ;
[ root@nginx ~ ]
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@nginx ~ ]
[ root@nginx ~ ]
Active connections: 1
server accepts handled requests
5 5 7
Reading: 0 Writing: 1 Waiting: 0
五、Nginx 虚拟主机
1、基于域名的虚拟主机
[ root@nginx ~ ]
server {
listen 80 ;
server_name www. kgc01. com;
location / {
root html/ kgc01;
index index. html index. htm;
}
}
server {
listen 80 ;
server_name www. kgc02. com;
location / {
root html/ kgc02;
index index. html index. htm;
}
}
[ root@nginx ~ ]
50x. html index. html
[ root@nginx ~ ]
[ root@nginx ~ ]
[ root@nginx ~ ]
[ root@nginx ~ ]
[ root@nginx ~ ]
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@nginx ~ ]
[ root@nginx ~ ]
[ root@nginx ~ ]
127.0 .0 .1 localhost localhost. localdomain localhost4 localhost4. localdomain4
: : 1 localhost localhost. localdomain localhost6 localhost6. localdomain6
192.168 .72 .154 www. kgc01. com www. kgc02. com
[ root@nginx ~ ]
This is kgc01
[ root@nginx ~ ]
This is kgc02
2、基于 IP 的虚拟主机
[ root@nginx ~ ]
ens33: flags= 4163 < UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
inet 192.168 .72 .154 netmask 255.255 .255 .0 broadcast 192.168 .72 .255
inet6 fe80: : a0df: 8b6: 704a: 2632 prefixlen 64 scopeid 0x20 < link>
ether 00 : 0c: 29 : c9: ff: b1 txqueuelen 1000 ( Ethernet)
RX packets 64702 bytes 85119679 ( 81.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10465 bytes 1088462 ( 1.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[ root@nginx ~ ]
[ root@nginx ~ ]
ens33: 0 : flags= 4163 < UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
inet 192.168 .72 .110 netmask 255.255 .255 .0 broadcast 192.168 .72 .255
ether 00 : 0c: 29 : c9: ff: b1 txqueuelen 1000 ( Ethernet)
[ root@nginx ~ ]
server {
listen 192.168 .72 .154 ;
server_name 192.168 .72 .154 ;
location / {
root html/ kgc01;
index index. html index. htm;
}
}
server {
listen 192.168 .72 .110 ;
server_name 192.168 .72 .110 ;
location / {
root html/ kgc02;
index index. html index. htm;
}
}
[ root@nginx ~ ]
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@nginx ~ ]
[ root@nginx ~ ]
This is kgc01
[ root@nginx ~ ]
This is kgc02
3、基于端口的虚拟主机
[ root@nginx ~ ]
server {
listen 192.168 .72 .154 : 81 ;
server_name 192.168 .72 .154 : 81 ;
location / {
root html/ kgc01;
index index. html index. htm;
}
}
server {
listen 192.168 .72 .110 : 82 ;
server_name 192.168 .72 .110 : 82 ;
location / {
root html/ kgc02;
index index. html index. htm;
}
}
[ root@nginx ~ ]
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@nginx ~ ]
[ root@nginx ~ ]
This is kgc01
[ root@nginx ~ ]
This is kgc02