1.日志切割
vim /usr/local/nginx/logs/cut_log.sh
#!/bin/bash
Dateformat=`date +%Y%m%d`
Basedir="/usr/local/nginx"
NginxLogdir="$Basedir/logs"
Logname="access"
[ -d $NginxLogdir ] && cd $NginxLogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
chmod +x /usr/local/nginx/logs/cut_log.sh
crontab -e
00 00 * * * /usr/local/nginx/logs/cut_log.sh
2.rewrite地址重写
在nginx.conf中的server中进行添加跳转模块,可以让访问直接跳转到指定页面
第一个server
server {
listen 81;
server_name www.haha.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
第二个server
server {
listen 80;
server_name www.jcfw.com;
rewrite ^/(.*) http://www.hahha.com:81/$1 permanent;
location / {
proxy_pass http://proxy;
}
}
vim /etc/hosts
192.168.1.5 www.haha.com
3.根据用户的URI返回不同的状态码
在Nginx.conf的server模块中添加
root /var/www/sls;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~/images/ {
return 404;
}
firefox www.haha.com/var/www/sls/sls.html
401 Authorization Required
firefox www.haha.com
402 Payment Required
firefox www.haha.com/documents/document.html
403 Forbidden
firefox www.haha.com/images/images.html
404 Not Found
4.隐藏WEB服务器的版本号和名称
vim /usr/local/nginx/conf/nginx.conf #在http模块中添加以下内容
server_tokens off;
nginx -s reload
firefox www.haha.com #显示不出来nginx版本号
cd /usr/src/nginx-1.11.1/src/core/
vim nginx.h
#define NGINX_VERSION "11.111.111.11"
#define NGINX_VER "WebLogic/" NGINX_VERSION
#define NGINX_VAR "WebLogic"
vim /usr/src/nginx-1.11.1/src/http/ngx_http_header_filter_module.c
static char ngx_http_server_string[] = "Server: WebLogic" CRLF; #49行
vim /usr/src/nginx-1.11.1/src/http/ngx_http_special_response.c
"<hr><center>" NGINX_VER "(http://www.sls.com) </center>" CRLF #22行
"<hr><center>WebLogic</center>" CRLF #29行
cd /usr/src/nginx-1.11.1/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module && make && make install
nginx -s quit
nginx
5.优化Nginx
vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_connections 1024;
worker_rlimit_nofile 65535;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_header_timeout 30;
client_body_timeout 30;
send_timeout 30;
client_max_body_size 1M;
gzip on;
gzip_min_length 1K;
gzip_comp_level[1-9];
gzip_varry on;
gzip_types text/plain application/x-javascript text/css application/xml
vim /etc/security/limits.conf
在Nginx中可以设置自动连接超时
1.将无用连接尽快超时,保证服务器资源高效使用
2.当连接较多时,及时断掉空闲连接,保证服务器资源高效可用状态
内容解释
auth_basic string/off; #开启用户密码或者关闭密码
auth_basic_user_file; #用户密码存放路径
htpasswd -bc /usr/local/nginx/conf/htpasswd gods 123.com #在上述目录生成一个用户名字叫做gods密码为123.com的名字叫做htpasswd的文件 -b 生成 -c 创建
^:匹配以...开头的数据
$:匹配输入字符串的位置
*:匹配*前面的字符串0次或者多次
+:匹配+前面的字符一次或者多次
?:匹配?前面的字符0次或者多次
permanent:返回301永久重定向
redirect:返回302临时重定向,浏览器显示跳转后的URL地址
location:根据用户请求的URI执行不同的应用(URI本地地址)
grep processor /proc/cpuinfo | wc -l #查看CPU核心数
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1; #工作时的进程数,在正常提供服务时设置和CPU核心数量相等的进程数,如果高负荷时设置为和CPU核心数的二倍数量
worker_connections 1024; #单个进程的最大连接数,最大工作连接数=工作的进程数 * 单个进程工作连接数量
worker_rlimit_nofile 65535; #在events模块中添加,限制最大文件数量
sendfile on; #开启文件高效传输模式,默认开启
tcp_nopush on; #防止网络以及磁盘的IO阻塞(减少网络使用)
tcp_nodelay on; #防止网络以及磁盘IO阻塞(发送数据时,内核将多个包合成一个包进行发送,减少IO消耗)
keepalive_timeout 65; #设置连接的超时时间
client_header_timeout 30; #设置读取客户机请求头部数据的超时时间
client_body_timeout 30; #设置读取客户机请求主体的超时时间
send_timeout 30; #用于指定响应客户端的超时时间
client_max_body_size 1M; #设置客户端请求主题的最大体积
gzip on; #开启压缩文件功能,可以提升用户体验,发送给用户的数据小,打开网页速度快,节约网站带宽,减少流量损失,选择压缩或者不压缩的内容
gzip_min_length 1K; #允许压缩的最小字节
gzip_comp_level[1-9]; #设置压缩比,压缩比越大处理文件的速度越慢,反之越快
gzip_varry on; #可以让前端的缓存服务器经过gzip压缩页面
gzip_types text/plain application/x-javascript text/css application/xml #选择将要压缩的文件,text/htlm页面总是被压缩