目录
一、Nginx
一款高性能、轻量级Web服务软件
1.稳定性高
2.系统资源消耗低.
3.对HTTP并发连接的处理能力高
单台物理服务器可支持30000 ~ 50000个并发请求
二、监控
1.准备环境
yum -y install gcc gcc-c++ pcre-devel zlib-devel make
2.创建用户
useradd -M -s /sbin/nologin nginx
3. 解压并移动
unzip nginx-module-vts-master.zip
mv nginx-module-vts-master /usr/local/
ls /usr/local/
4.编译安装
cd /opt
tar xzvf nginx-1.15.9.tar.gz
cd nginx-1.15.9/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--add-module=/usr/local/nginx-module-vts-master/
add module 添加模块
make && make install
5.路径优化
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
6.确认
nginx -t 检查配置文件是否配置正确
7.修改配置文件
cd /usr/lib/systemd/system
vim nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.targe
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl start nginx
systemctl status nginx
cd /usr/local/nginx/conf
vim nginx.conf
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" } }';
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
nginx -t 检查配置文件是否配置正确
systemctl status nginx
systemctl restart nginx
netstat -natp | grep nginx 检测存活
8.访问并监控
三、nginx网站服务配置
cd /usr/local/nginx/conf/
vim nginx.conf
location / {
root html;
index index.html index.htm;
deny 192.168.22.128; 禁止访问
allow all;
}
systemctl restart nginx
location / {
root html;
index index.html index.htm;
allow all;
deny 192.168.22.128; 允许访问
}
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4: :1
localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.22.128 wwow . ls.com www . accp.com
mkdir -p /var/www/html/ls /var/www/html/accp
cd /var/www/html/
ls
echo "<h1>hellow</h1>" > ls/index.html
echo "<h1>hellow</h1>" > accp/index.html
ls *
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.accp.com;
charset utf-8;
access_log logs/accp.access.log main;
location / {
root /var/www/html/accp;
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.ls.com;
charset utf-8;
access_log logs/ls.access.log main;
location / {
root /var/www/html/ls;
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;
}
nginx -t
systemctl restart nginx
curl www.ls.com
curl www.accp.com
cd /var/www/html/
mv ls benet8080
mv accp benet80
cd benet80
vim index.html
cd benet8080
vim index.html
cd /usr/local/nginx/conf/
vim nginx.conf
server {
listen 192.168.22.128:80;
server_name www.benet.com;
charset utf-8;
access_log logs/benet80.access.log main;
location / {
root /var/www/html/benet80;
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.22.128:8080;
server_name www.benet.com;
charset utf-8;
access_log logs/benet8080.access.log main;
location / {
root /var/www/html/benet8080;
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
cd /usr/local/nginx/conf/
vim nginx.conf
http {
include mime.types;
default_type application/octet-stream;
vhost_traffic_status zone;
server_tokens off; 关闭版本号
systemctl restart nginx
curl -I 192.168.22.128
vim /opt/nginx-1.15.9/src/core/nginx.h
cd /opt/nginx-1.15.9/src/core/
vim nginx.h
cd /opt/nginx-1.15.9/
./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
或者
> --add-module=/usr/local/nginx-module-vts-master/
vim nginx.conf
http {
include mime.types;
default_type application/octet-stream;
vhost_traffic_status zone;
server_tokens on; 开启版本号
systemctl restart nginx
curl -I 192.168.22.128
vim nginx.conf
location ~ \.(gif|jpg|jepg|png|bmp|ico)$ {
root html;
expires 1d;
}
nginx -t
cd ..
cd html/
vim index.html
<img srce"a .ipg"/> 添加内容