学习b记 · 第二阶段
就简单的说了一下nginx服务,配置了一下
四、nginx_http
1、回顾一下昨天的知识SSH,免秘钥无法连接排查思路
1.确定自己的公钥是否成功发送到了对方
2.比对两端的公钥
3.位置必须正确家目录/.ssh/
4.认证文件的名称必须正确(使用ssh-copy-id肯定正确的,手动的需要检查)
5.权限排查
1)验证文件权限必须是600
[root@web01 ~]# ll .ssh/authorized_keys
-rw------- 1 root root 390 Dec 6 12:08 .ssh/authorized_keys
2).ssh目录权限必须是700
[root@web01 ~]# ll -d .ssh
drwx------ 2 root root 48 Dec 6 12:08 .ssh
3)/root目录权限必须是550
[root@web01 .ssh]# ll -d /root
dr-xr-x---. 5 root root 213 Dec 5 15:40 /root
http
1、http的请求和响应的头部信息
1)请求头
Accept: text/html # 请求文件类型
Accept-Encoding: gzip, deflate # 请求编码
Accept-Language: zh-CN,zh;q=0.9 # 请求语言
Cache-Control: no-cache # 缓存
Connection: keep-alive # 长连接
Host: 10.0.0.7 # 服务器地址
If-Modified-Since: Thu, 07 Dec 2023 02:41:49 GMT # 请求的当前页面最后修改的时间
If-None-Match: "657130ed-10" # 请求标识
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 # 客户端信息
2)响应头
HTTP/1.1 304 Not Modified # 响应http版本号 响应状态码
Server: nginx/1.20.1 # 响应服务端服务版本 优化需要隐藏
Date: Thu, 07 Dec 2023 02:58:41 GMT # 时间
Last-Modified: Thu, 07 Dec 2023 02:41:49 GMT # 最后修改时间
Connection: keep-alive # 响应长连接
ETag: "657130ed-10" # 服务端响应标识
2、http状态码
2xx(成功)表示成功处理了请求的状态码=
200(成功)服务器已成功处理了请求。
3开头的状态码(重定向)
3xx(重定向)表示要完成请求,需要进一步操作。通常这些状态代码用来重定向。
301 永久性重定向,表示资源已被分配了新的 URL
302 临时性重定向,表示资源临时被分配了新的 URL
304 (未修改)自从上次请求后,请求网页未修改过。服务器返回此响应时,不会返回网页内容
401 表示发送的请求需要有通过HTTP认证的认证信息
403 请求的文件不存在,目录存在
404 请求的资源目录不存在
500 (服务器内部错误)服务器遇到错误,无法完成请求
502 网关错误
503 表示服务器处于停机维护或超负载,无法处理请求
504 服务器超时
nginx
1、Nginx的安装方式
1)源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐 4.规范 5.便于管理)
直接去网上下载想要版本的包就行
2)epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)
这个方法需要配置epel源,配置完了直接yum安装就完了
3)官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读)
安装Nginx依赖
[root@web ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
·配置官方yum源
[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
·安装Nginx服务
[root@web ~]# yum install nginx -y
·启动并设置开机自启
[root@web ~]# systemctl enable nginx
[root@web ~]# systemctl start nginx
验证是否启动成功
端口检测
[root@web ~]# netstat -lntup|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1221/nginx: master
检测进程
[root@web ~]# ps -ef|grep nginx
root 1221 1 0 16:54 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 1222 1221 0 16:54 ? 00:00:00 nginx: worker process
服务运行了之后直接百度linux ip就行了
2、Nginx启停
1)Nginx启动
[root@web ~]# /usr/sbin/nginx
[root@web ~]# systemctl start nginx
2)Nginx停止
[root@web ~]# /usr/sbin/nginx -s stop
[root@web ~]# systemctl stop nginx
3)Nginx重启
[root@web ~]# systemctl restart nginx
4)Nginx重载
[root@web ~]# /usr/sbin/nginx -s reload
[root@web ~]# systemctl reload nginx
#配置文件在/etc/nginx/nginx.conf 今天就是简单用一下其他的就不介绍了
3、网站配置,新增nginx配置文件
[root@web01 conf.d]# cat /etc/nginx/conf.d/game.conf
server {
listen 80;#监听端口
server_name game.root.com;#域名也可以用IP地址
location / {#映射文件
root /code;#默认运行网站文件位置
index index.html;#默认运行文件名称
}
}
4、运行
1)得先把windows的hosts文件修改一下 添加一条记录 ip 域名,不会整百度去
2)浏览器访问域名就可以了,要是不喜欢就自己整个静态的网页
5、Nginx配置多个虚拟主机
通常在企业中可能会有很多业务系统,那么多套业务服务如何使用Nginx配置?
基于多IP方式、基于多端口方式、基于多域名方式三种方法,下面就简单写一下思路,具体实现就不写了配置文件不知道怎么写就照着原来的抄。
1).配置多网卡多IP的方式
server {
...
listen 10.0.0.10:80;
...
}
server {
...
listen 10.0.0.11:80;
...
}
2)配置单网卡多IP的方式
#添加一个IP
[root@web01 ~]# ip addr add 10.0.0.11/24 dev eth0
# 虚拟机配置方案
[root@web01 ~]# cat /etc/nginx/conf.d/addr1.conf
server {
...
listen 10.0.0.10:80;
...
}
[root@web01 ~]# cat /etc/nginx/conf.d/addr2.conf
server {
...
listen 10.0.0.11:80;
...
}
3)Nginx多端口虚拟主机方式,具体配置如下#用这个来做,感觉这个最简单
#准备了三个游戏解压改名字game1、2、3
[root@web01 html]# ll
total 18724
drwxr-xr-x 2 root root 40 Dec 7 07:09 file
drwxr-xr-x 6 root root 107 Dec 7 07:17 game1
drwxr-xr-x 4 root root 86 Dec 7 07:18 game2
drwxr-xr-x 5 root root 56 Dec 7 2023 game3
-rw-r--r-- 1 root root 1610051 Dec 7 07:31 HTML5实现中国象棋游戏.zip
-rw-r--r-- 1 root root 7902976 Dec 7 01:58 小霸王_FC怀旧游戏机-HTML源码.zip
-rw-r--r-- 1 root root 1388400 Dec 7 06:43 暴打皮卡丘.zip
-rw-r--r-- 1 root root 8265498 Dec 7 07:11 植物大战僵尸.zip
1
[root@web01 ~]# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html/game1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
}
2
[root@web01 ~]# cat /etc/nginx/conf.d/default82.conf
server {
listen 82;
server_name localhost;
location / {
root /usr/share/nginx/html/game2;
index index.html index.htm;
}
}
3
[root@web01 ~]# cat /etc/nginx/conf.d/default83.conf
server {
listen 83;
server_name localhost;
location / {
root /usr/share/nginx/html/game3;
index index.html index.htm;
}
}
#最后直接重启服务就完了,游戏放网盘里了
#链接:https://pan.baidu.com/s/1eIlxcPySmiL0N9kIFwyZkw?pwd=kxxw
#提取码:kxxw
[root@web01 conf.d]# systemctl restart nginx
4)基于多域名方式
创建对应的**web站点目录以及程序代码
[root@web01 ~]# mkdir /soft/code/{server1,server2}
[root@web01 ~]# echo "server1" > /code/server1/index.html
[root@web01 ~]# echo "server2" > /code/server2/index.html
配置不同域名的虚拟主机
[root@web02 ~]# cat /etc/nginx/conf.d/server1.conf
server {
listen 80;
server_name 1.oldboyedu.com;
root /code/server1;
index index.html;
...
}
[root@web01 ~]# cat /etc/nginx/conf.d/server2.conf
server {
...
listen 80;
server_name 2.oldboyedu.com;
root /code/server2;
index index.html;
}
nginx日志管理
Nginx有非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令定义格式。
log_format详解
在nginx默认的配置文件中,log_format已经将日志格式定死,但是我们可不可以修改呢?
1.log_format的作用是定义日志格式语法
# 配置语法: 包括: error.log access.log
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http
2.nginx默认日志格式语法如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
3.Nginx日志格式允许包含的内置变量
$remote_addr # 记录客户端IP地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录ISO8601标准格式下的本地时间
$request # 记录请求的方法以及请求的http协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数
$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端IP地址
$request_length # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
# 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客 户端真实的IP地址。
# $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
# 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。
4.access_log日志配置语法
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
5.Nginx Access日志配置实践
server {
listen 80;
server_name code.oldboy.com;
#将当前的server网站的访问日志记录至对应的目录,使用main格式
access_log /var/log/nginx/code.oldboy.com.log main;
location / {
root /code;
}
#当有人请求改favicon.ico时,不记录日志
location /favicon.ico {
access_log off;
return 200;
}
}
nginx日志切割
使用logrotate切割日志
[root@nginx conf.d]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily # 每天切割日志
missingok # 日志丢失忽略
rotate 52 # 日志保留52天
compress # 日志文件压缩
delaycompress # 延迟压缩日志
notifempty # 不切割空文件
create 640 nginx adm # 日志文件权限
sharedscripts
postrotate # 切割日志执行的命令
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
日志切割后的效果
[root@oldboy ~]# ll /var/log/nginx/
total 4044
-rw-r----- 1 www adm 54438 Oct 12 03:28 access.log-20181012.gz
-rw-r----- 1 www adm 28657 Oct 13 03:48 access.log-20181013.gz
-rw-r----- 1 www adm 10135 Oct 12 03:28 error.log-20181130.gz
-rw-r----- 1 www adm 7452 Oct 13 03:48 error.log-20181201.gz
nginx日志监控
goaccess 日志监控
第一步: 安装
[root@web01 ~]# yum -y install goaccess
第二步: 配置
[root@web01 ~]# vim /etc/goaccess/goaccess.conf
time-format %H:%M:%S
date-format %d/%b/%Y
\# NCSA Combined Log Format
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
第三步: 通过命令行测试
[root@web01 ~]# goaccess -f /var/log/nginx/access.log
第四步: 希望把页面存储下来使用浏览器进行访问
vim go.oldboy.com
server {
listen 80;
server_name go.oldboy.com;
location / {
root /code/log;
index index.html;
}
}
{root@web01 ~]# mkdir /code/log
[root@web01 ~]# nohup goaccess -f /var/log/nginx/access.log -o /code/log/index.html -p /etc/goaccess/goaccess.conf --real-time-html &