Nginx是一款高性能的、轻量化的Web服务软件
它具有
1、稳定性高
2、对系统资源消耗低
3、对HTTP并发处理能力高 (单台物理服务器理论上可以支持3万~5万个并发请求)实际2~3w
systemctl stop firewalld 关闭防火墙
systemctl disable firewalld 禁止防火墙开机自启
setenforce 0 关闭核心防护
安装依赖环境
yum -y install gcc gcc-c++ pcre-devel zlib-devel make
创建运行用户和组并将需要安装的软件包上传到/opt下
useradd -M -s /sbin/nologin nginx
tar xzvf nginx-1.15.9.tar.gz
cd nginx-1.15.9/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install ##编译安装
路径优化
使 Nginx 服务器的运行更加方便以便管理员直接执行“nginx”命令就可以调用 Nginx 的主程序
cp -r /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
添加nginx系统服务
为了使 Nginx 服务的启动、停止、重载等操作更加方便,可以编写基于 CentOS 7.6 的
Nginx 服务控制文件使用 systemctl 工具来进行管理,CentOS7.6 系统的管理习惯
vim /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=/usr/bin/kill -s HUP $MAINPID ###根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl start nginx.service #开启
systemctl enable nginx.service #开启开机自启
在浏览器输入自己的IP地址查看
关闭防火墙
systemctl stop firewalld 关闭防火墙
systemctl disable firewalld 禁止防火墙开机自启
setenforce 0 关闭核心防护
安装依赖环境
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel make
创建运行用户和组并将需要安装的软件包上传到/opt下,然后解压
useradd -M -s /sbin/nologin nginx
tar -xvf nginx-1.15.9.tar.gz
unzip nginx-module-vts-master.zip
mv nginx-module-vts-master /usr/local/
进行配置
cd nginx-1.15.9/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--add-module=/usr/local/nginx-module-vts-master
./configure
--prefix=/usr/local/nginx
--add-module=/usr/local/nginx-module-vts-master
make && make install ##编译安装
路径优化
使 Nginx 服务器的运行更加方便以便管理员直接执行“nginx”命令就可以调用 Nginx 的主程序
cp -r /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
添加nginx系统服务
为了使 Nginx 服务的启动、停止、重载等操作更加方便,可以编写基于 CentOS 7.6 的
Nginx 服务控制文件使用 systemctl 工具来进行管理,CentOS7.6 系统的管理习惯
vim /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=/usr/bin/kill -s HUP $MAINPID ###根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl start nginx #开启服务
修改配置文件
cd /usr/local/nginx/conf/
vim nginx.conf
#在http模块进行修改 修改内容为:默认日志文件格式
vhost_traffic_status_zone;
log_format main '{ "@timestamp": "$time_local", '
'"@fields": { '
'"uri":"$request_uri",'
'"url":"$uri",'
'"upstream_addr":"$upstream_addr",'
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"body_bytes_sent": "$body_bytes_sent", '
'"host":"$host",'
'"server_addr":"$server_addr",'
'"request_time": "$request_time", '
'"request_time":"$request_time",'
'"status":"$status",'
'"request": "$request", '
'"request_method": "$request_method", '
'"size":$body_bytes_sent,'
'"upstream_time":"$upstream_response_time"'
'"http_referrer": "$http_referer", '
'"body_bytes_sent":"$body_bytes_sent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_user_agent": "$http_user_agent" } }';
然后在 server模块中再加入一个
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
nginx -t #检测一下是否正确
systemctl restart nginx #重启一下服务
打开 http://192.168.116.134/status
访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问
自上而下执行。如匹配则停止,不再往下匹配
vim /usr/local/nginx/conf/nginx.conf #修改配置文件
##禁止192.168.116.129的IP访问
systemctl restart nginx ##重启服务
访问被拒绝
为虚拟主机提供域名解析
vim /etc/hosts
准备虚拟站点网页文档
mkdir -p /var/www/html/yan
mkdir -p /var/www/html/yun
cd /var/www/html
[root@localhost html]# echo "xiaoyan
" > /var/www/html/yan/index.html
[root@localhost html]# echo "yunyun
" > /var/www/html/yun/index.html
修改nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.yan.com; #设置域名
charset utf-8;
access_log logs/yan.access.log;
location / {
root /var/www/html/yan; #设置工作目录
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.yun.com;
charset utf-8;
access_log logs/yun.access.log;
location / {
root /var/www/html/yun;
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重启服务
systemctl restart nginx
测试
vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.116.134:80;
server_name www.yan.com;
charset utf-8;
access_log logs/yan.access.log;
location / {
root /var/www/html/yan;
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.116.134:8080;
server_name www.yun.com;
charset utf-8;
access_log logs/yun.access.log;
location / {
root /var/www/html/yun;
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重启服务
systemctl restart nginx
测试
192.168.116.134
192.168.116.134:8080
vim /etc/hosts
ifconfig ens33:0 192.168.116.140 netmask 255.255.255.0
#创建虚拟网卡
修改配置文件
vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.116.134;
server_name www.yan.com;
charset utf-8;
access_log logs/yan.access.log;
location / {
root /var/www/html/yan;
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.116.140;
server_name www.yun.com;
charset utf-8;
access_log logs/yun.access.log;
location / {
root /var/www/html/yun;
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
systemctl restart nginx #重启服务
测试