Nginx部署
资源列表
操作系统 | 配置 | 主机名 | IP |
---|---|---|---|
CentOS 7 | 2G 4核 | nginx | 192.168.93.101 |
Windows | 2G 4核 | 无 | 192.168.93.100 |
基础环境
- 关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
- 关闭内核安全机制
[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled
[root@localhost ~]# sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
- 修改主机名
[root@localhost ~]# hostnamectl set-hostname nginx
一.安装Nginx
1.1、安装依赖、创建特定用户
[root@nginx ~]# yum -y install pcre-devel zlib-devel gcc-c++ gcc
[root@nginx ~]# useradd -M -s /sbin/nologin nginx
1.2、编译安装
[root@nginx ~]# tar -zxvf nginx-1.12.0.tar.gz -C /usr/src/
[root@nginx ~]# cd /usr/src/nginx-1.12.0/
[root@nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
# --prefix=/usr/local/nginx:指定nginx安装到哪个目录下,如/usr/local/nginx
# --user=nginx:nginx运行的用户
# --group=nginx:nginx运行组账户
# ----with-http_stub_status_module:状态统计模块
[root@nginx nginx-1.12.0]# make && make install
#创建软链接,可以更好的使用nginx
[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.0]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 4月 1 08:26 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
1.3、Nginx的运行控制
1.1.3、检查配置文件
#如果要检测其他位置的配置文件,可以使用“-c”选项来执行路径
#nginx.conf主配置文件默认位于安装目录下的conf/子目录中
[root@nginx ~]# 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 successfu
1.1.4、启动、停止Nginx
#开启nginx之前要确保服务器没有其他web或者80端口已经启用
[root@nginx ~]# nginx #直接执行nginx就相当于启动nginx
[root@nginx ~]# netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3956/nginx: master
#主程序nginx支持标准的进行信号,通过kill或killall命令发送HUP信息表示重载配置,QUIT信号表示退出进程,KILL信号表示杀死进程(通过“-s”选项指定信息种类)
[root@nginx ~]# yum -y install psmisc-22.20-17.el7.x86_64 #安装killall命令
[root@nginx ~]# killall -s HUP nginx #重载nginx
[root@nginx ~]# killall -s QUIT nginx #关闭nginx
[root@nginx ~]# cat /usr/local/nginx/logs/nginx.pid #nginxPID存放位置,开启nginx服务器才会有PID号,也可以通过PID来关闭nginx服务
3992
1.1.5、添加Nginx系统服务
[root@nginx ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server 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)
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -anpt | grep nginx
else
echo "Nginx is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
esac
exit 0
[root@nginx ~]# chmod +x /etc/init.d/nginx
[root@nginx ~]# chkconfig --add nginx
#使用systemctl管理nginx之前,先用killall -s QUIT nginx关闭nginx
[root@nginx ~]# killall -s QUIT nginx
[root@nginx ~]# systemctl status nginx
● nginx.service - SYSV: Nginx Server Control Script
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
1.4、配置Nginx.conf
在Nginx服务器的主配置文件/usr/local/nginx/conf/nginx.conf,包括全局配置、I/O事件配置和HTTP配置这三大内容,配置语句的格式为“关键字 值;”(末尾以分号表示结束),以“#”开始表示注释
1.4.1、全局配置
全局配置部分包括Nginx服务的运行用户、工作进程数、错误日志、PID存放位置等基本设置
#user nobody; #运行用户,实际用户nginx配置时指定的,未指定用户默认使用nobody
worker_processes 1; #工作进程数量,可根据服务器CPU或多核处理器来参考指定工作进程数
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID文件的位置
1.4.2、I/O事件配置
使用“events {}”界定标记,用来指定Nginx进程的I/O响应模型、每个进程的连接数等设置,
events {
use epoll; #使用epoll模型
worker_connections 1024; #每进程处理4096个链接
}
#2.6及以上的nginx版本,建议使用epoll模型以提高性能;每个进程的连接数应根据实际需要来定,一般在10000以下(默认1024)
1.4.3、HTTP配置
使用“http {}”界定标记,包括访问日志、HTTP端口、网页目录、默认字符集、连接保持,以及后面要讲到的虚拟Web主机、PHP解析等一系列设置,其中大部分配置语句都包含在子界定标记“server {}”内
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; #访问日志位置
sendfile on; #支持文件发送(下载)
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #连接保持事件
#gzip on;
server { Web服务的监听配置
listen 80; #监听地址及端口,也可以改为192.168.93.101:80
server_name www.bt.com; #网站名称
charset utf-8; #网页的默认字符集
location / { #根目录配置
root html; #网站根目录位置,默认是安装Nginx安装目录,根据需要也改为/var/www/html等其他路径
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
1.4.4、访问状态统计
Nginx内置了HTTP_STUB_STATUS状态统计模块,用来反馈当前Web访问情况。配置编译参数时可添加–with-http_stub_status_module来启用此模块支持
[root@nginx conf]# nginx -V #查看是否配置时指定状态模块
http {
server {
listen 80;
server_name www.bt.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
location /status { #访问位置/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
}
}
[root@nginx ~]# systemctl start nginx
#访问http://192.168.93.101/status或http://www.bt.com/status
Active connections: 1表示当前的活跃连接数(1)
server accepts handled requests:表示已经处理的连接信息,分别表示已经处理的连接数(0)、成功的TCP握手次数(1)、已经处理的请求数(0)
如果想看到更直接的效果,可以多访问几次
二.Nginx访问控制
2.1、基于授权的控制访问
2.1.1、基于授权的访问控制简介
配置步骤与Apache基本一致
- 概括为以下几个步骤
- 生成用户密码认证文件。
2.修改主配置文件响应的目录,添加认证配置项。
3.重启服务,访问测试
2.1.2、基于授权的访问控制步骤
(1)使用htpasswd生成用户认证文件
[root@nginx ~]# yum -y install httpd-tools #安装htpasswd软件
[root@nginx ~]# htpasswd -c /usr/local/nginx/passwd.db test
New password:
Re-type new password:
Adding password for user test
[root@nginx ~]# cat /usr/local/nginx/passwd.db
test:$apr1$w3EqIUYC$t3P0qM.xUTAy/qy8/nxu//
#-c在指定目录下生成一个新的配置文件,
#htpasswd /usr/local/nginx/passwd.db wzh 这个命令格式可以向用户认证文件添加一个新的用户
(2)修改密码文件权限为400,调整权限
[root@nginx ~]# chmod 400 /usr/local/nginx/passwd.db
[root@nginx ~]# chown nginx /usr/local/nginx/passwd.db
[root@nginx ~]# ll -d /usr/local/nginx/passwd.db
-r-------- 1 nginx root 85 4月 1 09:41 /usr/local/nginx/passwd.db
(3)修改主配置文件nginx.conf,添加认证配置项
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.bt.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
#想让哪个网站被访问的时候弹出认证框就写入到哪个网站的location里面,也可以写入到状态统计的location里面,
auth_basic "secret"; #指定认证提示信息
auth_basic_user_file /usr/local/nginx/passwd.db; #指定存储用户名和密码的文件路径
}
}
(4)检测语法、重启服务。
[root@nginx ~]# nginx -t
[root@nginx ~]# systemctl restart nginx
(5)用浏览器访问,验证控制效果
需要输入用户名和密码进行访问
2.2、基于客户端的访问控制
2.2.1、基于客户端的访问控制简介
基于客户端的访问控制是通过客户端IP地址,决定是否允许对网页访问。
- Nginx基于客户端的访问控制要比Apache简单,规则如下
-
deny IP/IP段:决绝某个IP或IP段的客户端访问。
-
allow IP/IP段:允许某个IP或IP段的客户端访问。
-
规则从上往下执行,如匹配则停止,不再往下匹配。
2.2.2、基于客户端的访问控制步骤
(1)修改主配置文件,添加响应的配置项
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.bt.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
#auth_basic "secret";
#auth_basic_user_file /usr/local/nginx/passwd.db;
#允许策略
#想对什么网站做限制,就把限制加入到location里面,同样也可以加入到状态统计的location里面
deny 192.168.93.100; #拒绝93.100
allow all; #允许其他所有IP地址
}
[root@nginx ~]# systemctl restart nginx
(2)重启服务器访问网址
三.Nginx虚拟主机
虚拟主机提供了在同一台服务器,同一组Nginx进程上运行多个网站的功能。跟Apache一样,Nginx也可以配置多种类型的虚拟主机,分别是基于IP、基于端口、基于域名
每个虚拟Web站点拥有独立的“server {}”配置段,各自监听的IP地址、端口号也可以单独指定,当然网站名称也是不同的
3.1、基于域名的虚拟主机
3.1.1、修改windows客户机的hosts文件
#文件路径C:\Windows\System32\drivers\etc\hosts
192.168.93.101 www.bt.com
192.168.93.101 www.test.com
3.1.2、准备网站目录
[root@nginx ~]# mkdir -p /var/www/html/btcom #创建www.bt.com的根目录
[root@nginx ~]# mkdir -p /var/www/html/testcom #创建www.test.com的根目录
[root@nginx ~]# echo "www.bt.com" >> /var/www/html/btcom/index.html
[root@nginx ~]# echo "www.test.com" >> /var/www/html/testcom/index.html
3.1.3、修改主配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#删除文件中的所有server {}段,加入2个新的server{}段,对应2个域名
server { #加入www.bt.com对应的站点
listen 80;
server_name www.bt.com; #本server段域名
charset utf-8;
access_log logs/www.bt.access.log; #日志文件
location / {
root /var/www/html/btcom; #www.bt.com的工作目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server { #加入www.test.com对应的站点
listen 80;
server_name www.test.com; #本server的域名
charset utf-8;
access_log logs/www.test.access.log; #日志文件
location / {
root /var/www/html/testcom; #www.test.com的工作目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
[root@nginx ~]# 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@nginx ~]# systemctl restart nginx
3.1.4、访问页面
3.2、基于IP的虚拟主机
3.2.1、准备多个网卡
[root@nginx ~]# ifconfig ens33:0 192.168.93.128/24 #创建临时IP地址,重启网卡则失效
[root@nginx ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.93.128 netmask 255.255.255.0 broadcast 192.168.93.255
ether 00:0c:29:cd:7a:7a txqueuelen 1000 (Ethernet)
3.2.2、需改nginx配置文件
#以/var/www/html/testcom和/var/www/html/btcom,修改Nginx的配置文件,使基于IP的虚拟主机生效
server {
listen 192.168.93.101:80; #监听192.168.93.101
server_name 192.168.93.101:80;
}
server {
listen 192.168.93.128:80; #监听192.168.93.128
server_name 182.168.93.128:80;
}
[root@nginx ~]# systemctl restart nginx
3.2.3、访问网页
3.3、基于端口的虚拟主机
3.3.1、选择不同的端口
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.93.101:1111; #监听1111端口
server_name 192.168.93.101:1111;
}
server {
listen 192.168.93.101:8888; #监听8888端口
server_name 182.168.93.101:8888;
}
[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# netstat -anpt | grep nginx
tcp 0 0 192.168.93.101:6666 0.0.0.0:* LISTEN 15190/nginx: master
tcp 0 0 192.168.93.101:8888 0.0.0.0:* LISTEN 15190/nginx: master