1.nginx安装
1.1.yum安装nginx
# yum安装 自动解决依赖:
yum install epel-release -y
yum install nginx -y
# 检查是否安装成功
rpm -qa |grep nginx
[root@nian-ys ~]# rpm -qa |grep nginx
nginx-1.20.1-10.el7.x86_64
nginx-filesystem-1.20.1-10.el7.noarch
# 可以用service启动服务
service nginx start
systemctl enable nginx
[root@nian-ys ~]# ps aux|grep nginx
root 20926 0.0 0.0 112812 976 pts/0 S+ 21:13 0:00 grep --color=auto nginx
root 30714 0.0 0.0 39308 1084 ? Ss Aug11 0:00 nginx: master process /usr/sbin/nginx
nginx 30715 0.0 0.1 39696 2064 ? S Aug11 0:00 nginx: worker process
nginx 30716 0.0 0.1 39696 2064 ? S Aug11 0:00 nginx: worker process
1.2.编译安装nginx(脚本安装一步到位)
[root@nginx2 ~]# vim onekey_install_ngnx_2023.sh
[root@nginx2 ~]# cat onekey_install_nginx_2023.sh # 安装nginx,记录过程
#!/bin/bash
# 安装依赖
yum install epel-release -y
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim wget -y
# 判断gala用户是否存在,没有新建用户
id gala || useradd gala
# 新建文件夹 nginx
mkdir /nginx
cd /nginx
# 下载nginx的源码包
curl -O http://nginx.org/download/nginx-1.25.2.tar.gz
# 解压源码包
tar xf nginx-1.25.2.tar.gz
# 进入解压后的目录
cd nginx-1.25.2
#编译前的设置
./configure --prefix=/usr/local/gala --user=gala --group=gala --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_gunzip_module
if (( $? != 0 ));then
echo "请检查你的依赖是否安装好"
exit
fi
# 编译
make -j 2
# 编译安装
make install
# 配置PATH变量
PATH=/usr/local/gala/sbin/:$PATH
# 永久配置PATH变量
echo 'PATH=/usr/local/gala/sbin/:$PATH' >> /etc/bashrc
# 启动nginx
nginx
# 设置开机启动
echo "/usr/local/gala/sbin/nginx" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local
# 在另一台测试
[root@test nginx-1.25.2]# source one_key_nginx.sh
make[1]: 离开目录“/nginx/nginx-1.25.2”
[root@test nginx-1.25.2]# ps aux| grep nginx
root 11763 0.0 0.0 46236 1168 ? Ss 22:12 0:00 nginx: master process nginx
gala 11764 0.0 0.0 46696 1916 ? S 22:12 0:00 nginx: worker process
root 11790 0.0 0.0 112824 988 pts/0 S+ 22:15 0:00 grep --color=auto nginx
[root@test nginx-1.25.2]# nginx -s stop # 关闭nginx
[root@test nginx-1.25.2]# ps aux| grep nginx
root 11794 0.0 0.0 112824 988 pts/0 S+ 22:16 0:00 grep --color=auto nginx
1.3.nginx编译前配置的介绍
[root@nginx nginx-1.25.2]# ./configure --help #要在指定文件夹执行此命令或者加绝对路径
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
--user=USER set non-privileged user for
worker processes
--group=GROUP set non-privileged group for
worker processes
可以看你需要开启的nginx的功能设置
--with-threads enable thread pool support 开启线程池功能的支持 --》默认没有开启
--without-select_module disable select module 禁用select模块功能 --》默认是开启的
--prefix=/usr/local/gala 指定安装路径
--user=gala --group=gala 指定启动nginx进程的用户和组
--with-http_ssl_module 开启https功能模块的支持
--with-http_v2_module 开启http 2.0的支持
--with-http_stub_status_module 开启nginx的状态统计功能--》可以知道有哪些人在访问nginx,并发数量都可以知道
--with-stream enable TCP/UDP proxy module 支持4层反向代理功能
--with-http_gunzip_module 支持压缩功能
1.4 用service或systemctl管理编译安装的nginx(只是拓展,此前的脚本已经设置开机自启)
这是接着前面的nginx -s stop之后执行的。
[root@test nginx-1.25.2]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
#PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
#ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/gala/sbin/nginx -t
ExecStart=/usr/local/gala/sbin/nginx
ExecReload=/usr/local/gala/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@test nginx-1.25.2]# systemctl daemon-reload
[root@test nginx-1.25.2]# service nginx start
Redirecting to /bin/systemctl start nginx.service
[root@test nginx-1.25.2]# ps aux |grep nginx
root 11841 0.0 0.0 46232 1160 ? Ss 22:21 0:00 nginx: master process /usr/local/gala/sbin/ngin
gala 11842 0.0 0.0 46696 1912 ? S 22:21 0:00 nginx: worker process
root 11844 0.0 0.0 112824 988 pts/0 S+ 22:21 0:00 grep --color=auto nginx
[root@test ~]# systemctl disable nginx
[root@test ~]# reboot
[root@test ~]# ps aux|grep nginx
root 987 0.0 0.0 46232 1168 ? Ss 22:34 0:00 nginx: master process /usr/local/gala/sbin/nginx
gala 989 0.0 0.0 46696 1920 ? S 22:34 0:00 nginx: worker process
root 1532 0.0 0.0 112824 988 pts/0 S+ 22:35 0:00 grep --color=auto nginx
记得你开机自启的设置,如果你想用systemctl disable nginx, 去/etc/rc.local要注释掉这个命令 /usr/local/gala/sbin/nginx, 否则开机还是会自启。见下面的操作。就没有自启了
[root@test ~]# vim /etc/rc.local
touch /var/lock/subsys/local
#/usr/local/gala/sbin/nginx
[root@test ~]# reboot
root@test ~]# ps aux|grep nginx
root 1532 0.0 0.0 112824 988 pts/0 S+ 22:31 0:00 grep --color=auto nginx
1.5 介绍nginx的主配置文件
#user nobody; #使用nobody用户启动nginx的worker进程, 开始的./config的设置的用户启进程即gala用户
[root@test ~]# ps aux|grep nginx
root 1593 0.0 0.0 46236 1168 ? Ss 19:52 0:00 nginx: master process nginx
gala 1594 0.0 0.0 46696 1916 ? S 19:52 0:00 nginx: worker process
root 1597 0.0 0.0 112824 988 pts/0 S+ 19:52 0:00 grep --color=auto nginx
worker_processes 4; 指定工作进程的数量,这个数值建议和cpu的核心数量一致
[root@test ~]# cat /proc/cpuinfo |grep core 四个
core id : 0
cpu cores : 4
core id : 1
cpu cores : 4
core id : 2
cpu cores : 4
core id : 3
cpu cores : 4
[root@test ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
[root@test ~]# top 然后按1出现cpu编号
top - 20:04:06 up 15 min, 1 user, load average: 0.02, 0.04, 0.08
Tasks: 134 total, 1 running, 133 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.4 us, 0.4 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
#error_log logs/error.log; 指定错误日志的路径
error_log logs/error.log notice; 是notice以上日志级别都记录
#error_log logs/error.log info; 是info以上日志级别都记录
loglevel
The kernel log levels are:
0 (KERN_EMERG) EMERG. emergency 紧急 --》系统不能使用了
The system is unusable.
1 (KERN_ALERT) ALERT. 告警
Actions that must be taken care of immediately.
2 (KERN_CRIT)
Critical conditions. CRIT. 严重 Critical
3 (KERN_ERR)
Noncritical error conditions. ERR. 程序因为某个错误导致不能运行,只要是error以上的级别都会导致程序启动或者是运行失败
4 (KERN_WARNING)
Warning conditions that should be taken care of. WARNING 警告 ,不会影响程序的正常运行,只是提醒而已
5 (KERN_NOTICE)
Normal, but significant events. NOTICE 正常的记录,有点影响力的事件
6 (KERN_INFO)
Informational messages that require no action. INFO. 普通的信息都记录
7 (KERN_DEBUG) 调试模式: 程序执行的整个过程都记录,方便开发者调试程序
Kernel debugging messages, output by the kernel if the developer enabled debugging at compile time.
#pid logs/nginx.pid; 记录master进程的pid号的文件
[root@test ~]# cat /usr/local/gala/logs/nginx.pid
1593
[root@test ~]# ps aux|grep nginx
root 1593 0.0 0.0 46236 1168 ? Ss 19:52 0:00 nginx: master process nginx
#ngingx的大并发事件处理问题-->事件处理--》一个用户访问过来可以理解为一个事件的发生
events {
worker_connections 1024; #一个worker进程可以并行处理1024个用户的连接 总并发数= worker进程数* 单个worker进程的连接数(一个线程)
}
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"'; 访问日志记录的内容--》main 是日志文件的格式的名字 如果server开启access_log功能必须要开启这个功能,不然会报错。
$remote_addr 是nginx内部的变量
$request 是http请求报文里的request报文里的url
$status 响应报文里的状态码
$body_bytes_sent 响应报文的body部分发生的数据
$http_referer 从那个网站跳转过来的 reference 参考/引用 --》引流
$http_user_agent 客户端使用的浏览器是那个
$http_x_forwarded_for 从那个代理服务器转发过来的
access_log logs/access.log main; 指定访问日志的路径和名字
sendfile on;
#tcp_nopush on;
gzip on; 开启网页压缩功能,nginx服务器在给客户端发生网页的时候,先对发送的内容进行压缩,然后再发送,这样可以提速。客户端的浏览器接受到数据后自动解压缩
#keepalive_timeout 0;
keepalive_timeout 65; 用户建立连接65秒后如果没有继续访问,就会进行4次挥手进行断开。 ---》nginx服务器会主动发起断开--》释放资源--》为后面的用户来访问提供更加多的资源
一个server对应一个网站(虚拟主机),可以配置多个server,也就是有多个网站
server {
listen 80; 网站监听80端口
server_name www.test.com; 网站的对应的域名
#charset koi8-r;
access_log logs/test.com.access.log main;
#定义路由,本质上就是对某个url的访问的控制
location / {
root html; 指定网页根目录:指定到html目录下查找网页 --》理解为网页存放在那个文件夹下
index.html index.htm; 指定网站的首页文件
}
error_page 404 /404.html; 定义出现404错误的时候,访问的网页/404.html
error_page 500 502 503 504 /50x.html; 定义出现500 502 503 504 错误的时候,访问的网页/50x.html
location = /50x.html {
root html;
}
}
2. 基于域名的虚拟主机的配置
[root@test ~]# cd /usr/local/gala/
[root@test gala]# ls
client_body_temp proxy_temp
conf sbin
fastcgi_temp scgi_temp
html uwsgi_temp
logs
[root@test gala]# cd conf
[root@test conf]# ls
fastcgi.conf
fastcgi.conf.default
fastcgi_params
fastcgi_params.default
koi-utf
koi-win
mime.types
mime.types.default
nginx.conf
nginx.conf.default
scgi_params
scgi_params.default
uwsgi_params
uwsgi_params.default
win-utf
[root@test conf]# mkdir -p /bak && cd /bak 如果你怕等会弄错可以把配置文件备份
[root@test bak]# cd -
/usr/local/gala/conf
[root@test conf]# cp nginx.conf /bak/nginx.conf.bak
[root@test conf]# cd -
/bak
[root@test bak]# ls
nginx.conf.bak
[root@test conf]# vim nginx.conf
# 改动的配置
worker_processes 4;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"-$pid-$nginx_version-$server_name';
access_log logs/access.log main;
gzip on;
# 在第一个server后加
server {
listen 80;
server_name www.test.com;
access_log logs/test.access.log main;
location / {
root html/test;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@test conf]# vim nginx.conf
[root@test conf]# nginx -t # 查看nginx.conf的配置是否有误
nginx: the configuration file /usr/local/gala/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/gala/conf/nginx.conf test is successful
[root@test conf]# nginx -s reload # 重新加载配置文件,master进程不产生新的进程,worker进程会产生新的进程, 可以ps aux查看前后的进程号
[root@test test]# vim index.html
Hello, World! WELCOME TO CHINA!
[root@test test]# echo "404 not found" >> 404.html
[root@test test]# echo "50x" >> 50x.html
去windows系统的C:\Windows\System32\drivers\etc\目录下修改加192.168.249.153 www.test.com,或者linux系统的/etc/hosts文件下加192.168.249.153 www.test.com,然后访问www.test.com. 然后访问www.test.com/666就是404页面。
3.安装nginx流量监控的一个模块vts(虚拟主机的流量监控)
3.1.隐藏nginx服务的版本号和实现简单的状态统计功能
root@test test]# nginx -V
nginx version: nginx/1.25.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/gala --user=gala --group=gala --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_gunzip_module
--with-http_stub_status_module 有这个模块就行
[root@test test]# cd ..
[root@test html]# cd ../conf/
[root@test conf]# vim nginx.conf
在http配置里添加
server_tokens off; 可以隐藏nginx版本号
server {
listen 80;
server_name www.test.com;
access_log logs/test.access.log main;
location / {
root html/test;
index index.html index.htm;
}
location /local_status { # 开启--with-http_stub_status_module模块,
# 用于实时监控nginx的网络连接
stub_status;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@test conf]# nginx -t
nginx: the configuration file /usr/local/gala/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/gala/conf/nginx.conf test is successful
[root@test conf]# nginx -s reload
提供以下状态信息:
Active connections当前活动的客户端连接数,包括Reading,Writing,Waiting之和。#最重要的参数,可以看出nginx服务器是否繁忙
Accepts 接受的客户端连接总数。
handled已处理的连接总数。通常,参数值与accepts 除非达到某些资源限制(例如, worker_connections限制)相同。
Requests 客户端请求总数。
请求可以在原来已有的连接基础上进行,不需要再次连接
Reading nginx正在读取请求报文的当前连接数。 --》处理请求报文
Writing nginx正在将响应写回到客户端的当前连接数。 --》处理响应报文
Waiting 当前等待请求的空闲客户端连接数 --》已经建立连接的人,nginx服务器等待它们再次发起请求
3.2.安装vts并热升级nginx
如果你用了service 管理编译安装的nginx,在你热升级的时候,要把nginx.service 文件移动到别的地方,然后执行命令systemctl daemon-reload,不然kill -12是没有用的。
https://gitee.com/mirrors/nginx-module-vts/repository/archive/master.zip
去这里下载nginx-module-vts-master.zip
然后用Xftp上传到服务器或虚拟机上
[root@test ~]# ls
anaconda-ks.cfg nginx-module-vts-master.zip one_key_nginx.sh
[root@test ~]# cd /nginx/
[root@test nginx]# ls
nginx-1.25.2 nginx-1.25.2.tar.gz
[root@test nginx]# rm -rf nginx-1.25.2 # 这里想热升级是另一个版本的nginx, 如果不是,这俩步骤可以不做
[root@test nginx]# tar xf nginx-1.25.2.tar.gz
[root@test nginx]# cd nginx-1.25.2
[root@test nginx-1.25.2]# yum install unzip -y
[root@test nginx-1.25.2]# unzip /root/nginx-module-vts-master.zip
[root@test nginx-1.25.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man nginx-module-vts-master README src
[root@test nginx-1.25.2]# ./configure --prefix=/usr/local/gala --user=gala --group=gala --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_gunzip_module --add-module=nginx-module-vts-master
#我的nginx-module-vts-master模块在当前文件夹下面,如果不是请接绝对路径
[root@test nginx-1.25.2]# make -j 2 #接-j就是几个进程一起编译,看cpu数决定。
[root@test nginx-1.25.2]# cd objs/
[root@test objs]# ls
addon autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@test objs]# cd /usr/local/gala/sbin/
[root@test sbin]# ls
nginx
[root@test sbin]# mv nginx nginx.bak
[root@test sbin]# ls
nginx.bak
[root@test sbin]# cd -
/nginx/nginx-1.25.2/objs
[root@test objs]# cp -r nginx /usr/local/gala/sbin/
[root@test objs]# cd -
/usr/local/gala/sbin
[root@test sbin]# ls
nginx nginx.bak
[root@test sbin]# ps aux |grep nginx
root 1569 0.0 0.0 46236 1208 ? Ss 22:56 0:00 nginx: master process nginx
gala 1570 0.0 0.0 46724 1956 ? S 22:56 0:00 nginx: worker process
gala 1571 0.0 0.0 46724 1956 ? S 22:56 0:00 nginx: worker process
gala 1572 0.0 0.0 46724 1956 ? S 22:56 0:00 nginx: worker process
gala 1573 0.0 0.0 46724 1956 ? S 22:56 0:00 nginx: worker process
root 1582 0.0 0.0 112824 988 pts/0 S+ 22:57 0:00 grep --color=auto nginx
[root@test sbin]# kill -12 1569
[root@test sbin]# ps aux |grep nginx
root 1569 0.0 0.0 46236 1320 ? Ss 22:56 0:00 nginx: master process nginx
gala 1570 0.0 0.0 46724 1956 ? S 22:56 0:00 nginx: worker process
gala 1571 0.0 0.0 46724 1956 ? S 22:56 0:00 nginx: worker process
gala 1572 0.0 0.0 46724 1956 ? S 22:56 0:00 nginx: worker process
gala 1573 0.0 0.0 46724 1956 ? S 22:56 0:00 nginx: worker process
root 1583 0.0 0.0 46372 3492 ? S 22:57 0:00 nginx: master process nginx
gala 1584 0.0 0.0 46904 2000 ? S 22:57 0:00 nginx: worker process
gala 1585 0.0 0.0 46904 2000 ? S 22:57 0:00 nginx: worker process
gala 1586 0.0 0.0 46904 2000 ? S 22:57 0:00 nginx: worker process
gala 1587 0.0 0.0 46904 2000 ? S 22:57 0:00 nginx: worker process
root 1589 0.0 0.0 112828 988 pts/0 R+ 22:57 0:00 grep --color=auto nginx
[root@test sbin]# kill -3 1569
[root@test sbin]# ps aux |grep nginx
root 1583 0.0 0.0 46372 3492 ? S 22:57 0:00 nginx: master process nginx
gala 1584 0.0 0.0 46904 2000 ? S 22:57 0:00 nginx: worker process
gala 1585 0.0 0.0 46904 2000 ? S 22:57 0:00 nginx: worker process
gala 1586 0.0 0.0 46904 2000 ? S 22:57 0:00 nginx: worker process
gala 1587 0.0 0.0 46904 2000 ? S 22:57 0:00 nginx: worker process
root 1591 0.0 0.0 112824 988 pts/0 S+ 22:59 0:00 grep --color=auto nginx
[root@test conf]# vim nginx.conf
#tcp_nopush on;
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
每个server里面加一个
location /status{
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
[root@test conf]# nginx -t
nginx: the configuration file /usr/local/gala/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/gala/conf/nginx.conf test is successful
[root@test conf]# nginx -s reload
最好访问www.test.com/status得到的画面