目录
Nginx
编译安装nginx
[root@localhost /]#cd data
#进入到data目录,把nginx包下载到该目录下
[root@localhost data]#yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
#下载nginx包所需要的环境依赖包
[root@localhost data]#useradd -M -s /sbin/nologin nginx
#创建一个nginx用户用于管理
[root@localhost data]#mkdir /apps/nginx -p
#创建一个nginx存放路径包
[root@localhost data]#wget http://nginx.org/download/nginx-1.18.0.tar.gz
#官网下载nginx安装包
[root@localhost data]#ls
nginx-1.18.0.tar.gz
#查看nginx1.18.0安装包
[root@localhost data]#tar xf nginx-1.18.0.tar.gz
#解压nginx1.18.0包
[root@localhost data]#ls
nginx-1.18.0 nginx-1.18.0.tar.gz
#查看解压后的包
[root@localhost data]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
#CHANGES 告诉nginx1.18.0修复了什么bug
#CHANGES.ru 俄文版告诉nginx1.18.0修复了什么bug
#conf 默认的配置文件
#configure 安装脚本
#contrib 提供一个vim编辑器的效果
#html 网页文件
#man man帮助
#src 源码
[root@localhost nginx-1.18.0]#./configure --prefix=/apps/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module
[root@localhost nginx-1.18.0]#make -j2 && make install
#编译
[root@localhost nginx]#ln -s /apps/nginx/sbin/nginx /usr/sbin
#做一个软连接
[root@localhost sbin]#/apps/nginx/sbin/nginx
#绝对路径启动nginx
[root@localhost ~]#chown -R nginx.nginx /apps/nginx
#修改权限
[root@localhost ~]#vim /usr/lib/systemd/system/nginx.service
#建立一个文件
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
#注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
#注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost ~]#systemctl daemon-reload
#重新加载
[root@localhost ~]#systemctl start nginx
#启动nginx
[root@localhost ~]#systemctl status nginx
#查看nginx状态
[root@localhost ~]#systemctl enable nginx
#开机自启动
contrib代码高亮
[root@localhost ~]#cp -r /data/nginx-1.18.0/contrib/vim/* /usr/share/vim/vimfiles/
[root@localhost ~]#vim /data/nginx-1.18.0/conf/nginx.conf
man帮助文档
[root@localhost ~]#cd /data
[root@localhost data]#ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost data]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.18.0]#cd man
[root@localhost man]#ls
nginx.8
[root@localhost man]#man ./nginx.8
#查看文档要加路径
[root@localhost man]#
[root@localhost man]#man /data/nginx-1.18.0/man/nginx.8
#或者使用绝对路径
nginx四大文件
[root@localhost nginx-1.18.0]#cd /apps
[root@localhost apps]#ls
nginx
[root@localhost apps]#cd nginx/
[root@localhost nginx]#ls
conf html logs sbin
- conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。
- html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
- logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
- sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
nginx选项
[root@localhost nginx]#nginx -v
#nginx版本
[root@localhost nginx]#nginx -V
#编译了哪些模块
[root@localhost nginx]#nginx -t
#检测配置文件格式是否正确
[root@localhost nginx]#nginx -T
#打印配置文件
[root@localhost nginx]#nginx -c
#指明加载的配置文件
[root@localhost nginx]#nginx -g
#不使用配置文件里的配置,使用后面跟着的配置
[root@localhost ~]#nginx -g 'user mtwm;'
#以mtwm身份运行,默认是以nginx身份
[root@localhost ~]#nginx -g 'daemon off;'
#前台运行命令
[root@localhost nginx]#nginx -h
[root@localhost nginx]#nginx -?
#nginx帮助文件
[root@localhost nginx]#nginx -s
#发信号
[root@localhost ~]#man /data/nginx-1.18.0/man/nginx.8
#可以通过man 用绝地路径查看nginx帮助手册
-s signal
stop SIGTERM 直接停止
quit SIGQUIT 优雅的退出:有人在访问不会结束进程
reopen SIGUSR1 分割日志
reload SIGHUP 重新加载配置文件
SIGNALS
The master process of nginx can handle the following signals:
SIGINT, SIGTERM Shut down quickly.
SIGHUP Reload configuration, start the new worker process with a new configuration, and gracefully shut down old worker processes.
SIGQUIT Shut down gracefully.
SIGUSR1 Reopen log files.
SIGUSR2 Upgrade the nginx executable on the fly.
SIGWINCH Shut down worker processes gracefully.
While there is no need to explicitly control worker processes normally, they support some signals too:
SIGTERM Shut down quickly.
SIGQUIT Shut down gracefully.
SIGUSR1 Reopen log files.
分割日志
[root@localhost logs]#mv access.log access-24.log
#把旧日志改名为access-24.log
[root@localhost logs]#touch access.log
#创建一个新日志access.log
打开nginx
实时查看新的日志,发现没有日志记录
实时查看旧的日志,发现日志还记录在旧日志中
再次打开nginx
#查看进程端口号
[root@localhost logs]#ps aux |grep nginx
#法一
[root@localhost logs]#cat /apps/nginx/logs/nginx.pid
13007
#法二
#分割日志
[root@localhost logs]#kill -s USR1 13007
#需要进程号
[root@localhost logs]#nginx -s reopen
#不需要进程号
新日志中开始记录
旧日志中没有新记录计入
优雅退出
#服务器7-1
[root@localhost ~]#cd /apps/nginx/
[root@localhost nginx]#ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@localhost nginx]#cd html
[root@localhost html]#ls
50x.html index.html
[root@localhost html]#dd if=/dev/zero of=/apps/nginx/html/m.img bs=1G count=3
[root@localhost html]#pstree -p |grep nginx
|-nginx(13007)---nginx(13008)
[root@localhost html]#nginx -s quit
[root@localhost html]#
[root@localhost html]#pstree -p |grep nginx
|-nginx(13007)---nginx(13008)
[root@localhost html]#
[root@localhost html]#pstree -p |grep nginx
[root@localhost html]#
#服务器7-3
[root@localhost ~]#cd /data
[root@localhost data]#ls
[root@localhost data]#wget --limit-rate=1M http://192.168.174.100/m.img
--2023-08-24 17:29:18-- http://192.168.174.100/m.img
正在连接 192.168.174.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:3221225472 (3.0G) [application/octet-stream]
正在保存至: “m.img”
1% [ ] 60,596,544 1.00MB/s 剩余 50m 15s^C
[root@localhost data]#
飞行升级
#nginx -s reload 重新加载
[root@localhost html]#systemctl start nginx
[root@localhost html]#pstree -p |grep nginx
|-nginx(14104)---nginx(14105)
[root@localhost html]#cat /apps/nginx//conf/nginx.conf |head -n 3
#user nobody;
worker_processes 1;
[root@localhost html]#vim /apps/nginx//conf/nginx.conf
[root@localhost html]#cat /apps/nginx//conf/nginx.conf |head -n 3
#user nobody;
worker_processes 2;
[root@localhost html]#nginx -s reload
[root@localhost html]#pstree -p |grep nginx
|-nginx(14104)-+-nginx(14135)
| `-nginx(14136)
[root@localhost html]#
[root@localhost ~]#curl -I 192.168.174.100
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 25 Aug 2023 07:55:01 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 07:52:10 GMT
Connection: keep-alive
ETag: "64e70c2a-264"
Accept-Ranges: bytes
[root@localhost usr]#ps aux |grep nginx
[root@localhost usr]#wget https://nginx.org/download/nginx-1.20.2.tar.gz -P /usr/local/src/
#下载nginx1.20.2包到/usr/local/src
[root@localhost src]#cd /usr/local/src
[root@localhost src]#ls
nginx-1.20.2.tar.gz
[root@localhost src]#tar xf nginx-1.20.2.tar.gz
[root@localhost src]#ls
nginx-1.20.2 nginx-1.20.2.tar.gz
[root@localhost src]#cd nginx-1.20.2/
[root@localhost nginx-1.20.2]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.20.2]#nginx -V
nginx version: nginx/1.18.0
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=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@localhost nginx-1.20.2]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@localhost nginx-1.20.2]#make -j2
#####不要make install
[root@localhost nginx-1.20.2]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@localhost nginx-1.20.2]#cd objs/
[root@localhost objs]#ls
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@localhost objs]#./nginx -v
nginx version: nginx/1.20.2
[root@localhost objs]#cd /apps/nginx/sbin/
[root@localhost sbin]#ls
nginx
[root@localhost sbin]#mv nginx nginx.bak
[root@localhost sbin]#ls
nginx.bak
#把nginx1.18.0改名成nginx.bak
[root@localhost objs]#cp nginx /apps/nginx/sbin/
[root@localhost objs]#cd /apps/nginx/sbin/
[root@localhost sbin]#ls
nginx nginx.bak
#将nginx1.20.2拷贝到/apps/nginx/sbin/
[root@localhost sbin]#cat /apps/nginx/logs/nginx.pid
1137
[root@localhost sbin]#kill -USR2 1137
[root@localhost sbin]#
[root@localhost sbin]#ps aux |grep nginx
[root@localhost sbin]#cd /apps/nginx/logs/
[root@localhost logs]#ls
access-24.log access.log error.log nginx.pid nginx.pid.oldbin
[root@localhost logs]#cat nginx.pid.oldbin
1137
[root@localhost logs]#cat nginx.pid
6056
[root@localhost logs]#kill -WINCH 1137
[root@localhost logs]#ps aux |grep nginx
回滚
[root@localhost logs]#kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
[root@localhost logs]#ps aux |grep nginx
[root@localhost logs]#kill -QUIT `cat /apps/nginx/logs/nginx.pid`
[root@localhost logs]#ps aux |grep nginx
配置详细解释
修改worker数量
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
worker_processes auto;
[root@localhost ~]#lscpu |grep -i cpu
[root@localhost ~]#nginx -s reload
[root@localhost ~]#ps aux |grep -v grep |grep nginx
关闭版本或修改版本
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
http {
include mime.types;
server_tokens off;
[root@localhost ~]#nginx -t
[root@localhost ~]#nginx -s reload
mime
[root@localhost conf]#cat /apps/nginx/conf/mime.types | head
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
server下的root
root路径格式 指定文件的路径 url
Syntax: root path;
Default:
root html;
Context: http, server, location,
指明软件的根目录
server块构建虚拟主机 实际只有一台 好像很多主机
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf.d*.conf;
[root@localhost ~]#mkdir -p /apps/nginx/conf.d/
[root@localhost ~]#cd /apps/nginx/conf.d
[root@localhost conf.d]#vim pc.conf
server{
listen 80;
server_name www.pc.com;
root /data/html/pc;
}
[root@localhost conf.d]#cp pc.conf m.conf
[root@localhost conf.d]#vim m.conf
server{
listen 80;
server_name www.m.com;
root /data/html/m;
}
[root@localhost conf.d]#cd /data
[root@localhost data]#mkdir -p html/m
[root@localhost data]#mkdir -p html/pc
[root@localhost data]#ls
404.html favicon.ico html nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost data]#cd html
[root@localhost html]#ls
m pc
[root@localhost html]#cd m
[root@localhost m]#echo mmmmmm > index.html
[root@localhost m]#cat index.html
mmmmmm
[root@localhost html]#cd ../pc
[root@localhost pc]#echo pcpcpcpcpcpc > index.html
[root@localhost pc]#cat index.html
pcpcpcpcpcpc
[root@localhost html]#nginx -s reload
[root@localhost ~]#vim /etc/hosts
192.168.174.100 www.pc.com www.m.com
[root@localhost ~]#curl www.pc.com
pcpcpcpcpcpc
[root@localhost ~]#curl www.m.com
mmmmmm
[root@localhost ~]#vim /apps/nginx/conf.d/m.conf
server{
listen 88;
server_name www.m.com;
root /data/html/m;
}
[root@localhost ~]#curl 192.168.174.100
pcpcpcpcpcpc
[root@localhost ~]#curl 192.168.174.100:88
mmmmmm
alias 别名
[root@localhost ~]#vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.pc.com;
location /mdl {
root /data/html;
#相当于追加 将 文件夹mdl追加到/data/html/mdl/index.html
}
}
[root@localhost ~]#cd /data/html/
[root@localhost html]#ls
m pc
[root@localhost html]#mkdir mdl
[root@localhost html]#echo mdlhb > mdl/index.html
[root@localhost html]#cat mdl/index.html
mdlhb
[root@localhost html]#nginx -s reload
[root@localhost ~]#vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.pc.com;
location /mdl {
root /data/html;
#相当于追加 将 文件夹mdl追加到/data/html/mdl/index.html
}
location /hbw {
alias /data/html;
#相当于等价替换 /hbw=/data/html
}
}
[root@localhost ~]#cd /data/html/
[root@localhost html]#ls
m mdl pc
[root@localhost html]#echo hbwnb >> index.html
[root@localhost html]#cat index.html
hbwnb
[root@localhost html]#ls
index.html m mdl pc
[root@localhost html]#nginx -s reload
location
404
[root@localhost data]#vim /apps/nginx/conf/nginx.conf
error_page 404 /404.html;
location = /404.html {
root /data/;
}
[root@localhost data]#vim 404.html
[root@localhost data]#cat 404.html
please call 10010 to solve the problem
[root@localhost data]#nginx -s reload
自定义图标
[root@localhost ~]#wget www.jd.com/favicon.ico
[root@localhost ~]#cp favicon.ico /data/
[root@localhost ~]#cd /data
[root@localhost data]#ls
favicon.ico nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost data]#vim /apps/nginx/conf/nginx.conf
location = /favicon.ico {
root /data/;
}
[root@localhost data]#nginx -s reload