正向代理和反向代理的区别
– 正向代理:用户决定访问哪个服务器
– 反向代理:代理决定访问哪个服务器
nginx采用单主进程,多子进程(nginx.conf worker_processes 默认为1)的模型,master管理worker,master接收外部请求或指令,分配给worker去执行,worker关闭时,会等待当前客户连接释放后,才会关闭,多进程虽然会带来额外的内存开销,采用多进程而不采用多线程的原因:
nginx采用的是异步非阻塞,在Linux上默认使用epoll模型
设置worker进程的用户,指的是Linux的用户,会涉及到nginx操作目录或文件的一些权限,默认为nobody
user root;
worker进程工作数设置,一般来说CPU有几个,就设置几个,或者设置为N-1也行
worker_processes 1;
nginx日志级别debug|info|notice|warn|error|erit|alert|emerg错误级别从左到右越来越大。
设置nginx进程pid
pid logs/nginx.pid
events {
# 默认使用epoll
use epoll;
# 每个worker允许连接的客户端最大连接数
worker_connections 10240;
}
http是指令块,针对http网络传输的一些指令配置
http {
}
include引入外部配置,提高可读性,避免单个配置文件过大
include mime.types;
参数名 | 参数意义 |
---|---|
$remote_addr | 客户端IP |
$remote_user | 远程客户端用户名,一般为:’-’ |
$time_local | 时间和时区 |
$request | 请求的URL及method |
$status | 响应状态码 |
$body_bytes_send | 响应客户端内容字节数 |
$http_referer | 记录用户从哪个链接跳转过来的 |
$http_user_agent | 用户所使用的代理,一般来时都是浏览器 |
$http_x_forwarded_for | 通过代理服务器来记录客户端的IP |
sendfile使用高效文件传输,提升传输性能。启用后才能使用tcp_nopush,是指当数据表积累一定大小后才发送,提高了效率。
sendfile on;
tcp_nopush no;
keepalive_timeout设置客户端与服务端请求的超时时间,保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗。
keepalive_timeout 65;
gzip启用压缩,html/js/css压缩传输后会更快
gzip on;
#限制最小压缩,小于1字节的文件不会压缩
gzip_min_length 1;
#定义压缩的级别(压缩比取值范围1-9,值越大,压缩比越大,文件越大,压缩越多,但是cpu使用会越多)
gzip_comp_level 3;
#定义压缩文件的类型
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/json;
server可以在http指令块中设置多个虚拟主机
server {
listen 88;
server_name localhost;
location / {
root html;
index index.html index.htm
}
}
# 该命令会立刻关掉nginx,即使有客户端在和服务器连接
./nginx -s stop
# 等待所有连接(http请求)都关闭后再停止nginx
./nginx -s quit
./nginx -t
./nginx -v
# 展示nginx当前版本号
./nginx -V
# 展示nginx更详细的版本信息
./nginx -h
./nginx -?
./nginx -c
(可能会有多种不同类型配置文件, 可以使用-c 为当前切换指定配置文件 类似 dev.yml/prod.ym)
① 创建一个shell可执行的文件cut_my_log.sh
#!/bin/bash
LOG_PATH="/var/log/nginx/"
RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d+%H:%M)
PID=/var/run/nginx/nginx.pid
mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log
#向Nginx主进程发送信号,用于重新打开日志文件
kill -USR1 `cat $PID`
② 为cut_my_log.sh添加权限
chmod +x cut_my_log.sh
③ 测试日志切割后的结果
./cut_my_log.sh
① 安装定时任务
yum install crontabs
② crontab -e 编辑并且添加一行新的任务;
*/1 * * * * /usr/local/nginx/sbin/cut_my_log.sh
③ 重启定时任务:
service crond restart
service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
crontab -e // 编辑任务
crontab -l // 查看任务列表
定时任务表达式:
cron表达式是,分为5或6个域,每个域代表一个含义,如下所示:
分 | 时 | 日 | 月 | 星期几 | 年(可选) | |
---|---|---|---|---|---|---|
取值范围 | 0-59 | 0-23 | 1-31 | 1-12 | 1-7 | 2019/2020/2021 |
常用表达式:
*/1 * * * *
59 23 * * *
0 1 * * *
静态资源分为两类:
html/css/js
视频,音频,图片
server {
listen 90;
server_name localhost;
location / {
root /home/foodie-shop
index index.html
}
location /imooc {
# root 使用时是拼接上面的,例如: /home/imooc
root /home
}
location /static {
# alias 使用时上面的是下面的别名,例如访问/home/imooc目录下的内容,可以使用/static
alias /home/imooc
}
}
location / {
root /home/foodie-shop
index index.html
}
location = /imooc/img/face1.png {
root /home;
}
#符合图片的显示
location ~* .(GIF|jpg|png|jpeg) {
root /home;
}
#GIF必须大写才能匹配到
location ~ .(GIF|jpg|png|jpeg) {
root /home;
}
location ^~ /imooc/img {
root /home;
}