- nginx重载:nginx -s reload
- nginx热部署:
1、kill -USR2 nginx进程ID
2、kill -WINCH nginx进程ID
3、因为原有的nginx的master进程不会主动退出,为了版本回退,保留它。
- 日志切割:
1、mv access.log access.log.bak
2、../sbin/nginx -s reopen
3、这样太麻烦了,我们可以写一个shell脚本,crontab定时去执行脚本,
按天切割日志,格式为access_2020-04-01.log:
#!/bin/bash
LOGS_PATH=/usr/local/nginx/logs/history
CUR_LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv $(CUR_LOGS_PATH)/access.log $(LOGS_PATH)/access_$(YESTERDAY).log
mv $(CUR_LOGS_PATH)/error.log $(LOGS_PATH)/error_$(YESTERDAY).log
## 向Nginx主进程发送USR1信号。USR1信号是重新打开日志文件
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
- nginx反向代理/负载均衡
详见:http://www.linuxe.cn/post-182.html
https://blog.csdn.net/qq_32625839/article/details/82184739
【反向代理】
http {
server {
listen 80;
server_name www.localhost.com;
location / {
proxy_pass http://192.168.186.157:8000;#反向代理ip地址
}
}
}
【proxy_pass中常用指令】
proxy_method:修改用户的method请求
proxy_http_version:修改用户的http协议版本
proxy_set_header:修改用户header头部,如客户端真实IP信息,也是配置较多的选项
proxy_set_body:修改用户包体信息
proxy_send_timeout:默认60s
proxy_connect_timeout:默认60s,Nginx与后端服务器连接超时时间
【upstream负载均衡的算法】
1、轮询
http {#upstream模块包含在http模块下
upstream myserver {#定义upstream名字,下面会引用
server 192.168.186.10; #指定服务器地址
server 192.168.186.20;
server 192.168.186.30 back; #备用服务器
}
server {
listen 80;
server name www.myserver.com;
location / {
proxy_pass http://myserver;#引用upstream
}
}
}
2、加权重
http {
upstream myserver {
server 192.168.186.10 weight=3;#指定服务器地址,权重为3
server 192.168.186.20;
server 192.168.186.30;
}
server {
listen 80;
server name www.myserver.com;
location / {
proxy_pass http://myserver;
}
}
}
3、ip-hash
upstream myserver {
ip_hash;#采用IP HASH算法
server 192.168.186.10;
server 192.168.186.20;
server 192.168.186.30;
}
4、url-hash
http {
upstream myserver {
hash $request_uri;#采用URL HASH算法
server 192.168.186.10;
server 192.168.186.20;
server 192.168.186.30;
}
server {
listen 80;
server name www.myserver.com;
location / {
proxy_pass http://myserver;
}
}
}
5、最少连接数轮询
http {
upstream myserver {
least_conn;#最少连接数轮询:哪个节点当前的连接数少就分配给哪个节点处理
server 192.168.186.10;
server 192.168.186.20;
server 192.168.186.30;
}
server {
listen 80;
server name www.myserver.com;
location / {
proxy_pass http://myserver;
}
}
}
- GoAccess实现可视化并实时监控access日志
GoAccess地址:https://goaccess.io/
实时输出html(Real-Time HTML Output):
goaccess access.log -o /var/www/html/report.html --log-format=COMBINED --real-time-html \
--time-format='%H:%M:%S' --date-format='%d/%b/%Y'
- SSL协议(略)
证书类型:域名验证证书(DV)、组织验证证书(OV)、扩展验证证书(EV)
- 使用免费SSL证书实现一个HTTPS站点
yum -y install python2-certbot-nginx
certbot --nginx --nginx-server-root=/usr/local/nginx/conf -d www.code.pub
- OpenResty
1、下载地址:http://openresty.org/cn/
2、安装:./configure
3、使用nginx+lua(略)