群集架构----Nginx优化与防盗链

###############1.隐藏版本号
curl -I http://192.1 68.235.144/
HTTP/1.1 200 OK
Server: nginx/1.12.0
--------方法1--------
vim /usr/local/nginx/conf/nginx.conf
http {                                 //添加
    include       mime.types;   
    default_type  application/octet-stream;
    server_tokens off;
	.....
}
-------方法2---------
vim /opt/nginx-1.12.0/src/core/nginx.h
#define nginx_version 1012000
#define NGINX VERSION "1.1.1"  //修改
#define NGINX VER "lIS/"  NGINX VERSION
重新编译,make && make install

#####################2.编辑用户和组
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;	'//在全局添加,不在'
service nginx restart
ps aux |grep nginx 


####################3.优化网页缓存时间
vim /usr/local/nginx/conf/nginx.conf
       location ~\.(gif|jpg|jpeg|png|ico)$ {        
            root  html;
            expires 1d;
        }
header中会显示一 下内容
Expires: Fri, 15 Jun 2018 13:59:55 GMT

##################4.优化连接超时
http
keepalive_timeout 100;
client_header_timeout 80;
client_body_timeout 80;

#####################5.优化进程数
cat /proc/cpuinfo | grep -c "physical"
vim /usr/local/nginx/conf/nginx.conf
worker processes 2

####################6.NGINX日志分割
 vim /opt/fenge.sh
#!/bin/bash
# Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d")
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
kill -USR1 $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf

date -s 08/11/20
生成前一天的日志
 crontab -e
0 1 * * * /opt/fenge.sh 
####################补充7.mtime ctime atime的区别
在linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime,atime,mtime
modification time(mtime):
当修改文件的内容数据的时候,就会更新这个时间,而更改权限或者属性,mtime不会改变,这就是和ctime的区别。
status time(ctime)
当修改文件的权限或者属性的时候,就会更新这个时间,ctime并不是create time, 更像是change time,
只有当更新文件的属性或者权限的时候才会更新这个时间,但是更改内容的话是不会更新这个时间。
accesstime(atime)
当使用这个文件的时候就会更新这个时间。

###################################8.网页压缩
_网页压缩-
vim /usr/local/nginx/conf/nginx.conf
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;
	
nginx -t

##################################9.防盗链
 vim /usr/local/nginx/conf/nginx.conf       
		location ~*\.(jpg|gif|jepg)$ {
             valid_referers none blocked *.kgc.com kgc.com;
             if ( $invalid_referer ) {
                rewrite ^/ http://www.kgc.com/error.png;
             }

###################################10.PHP-FPM优化  优化前提LNMP架构
vim /usr/local/php/etc/php-fpm.conf   
pid = run/php-fpm.pid    "找到该行下面添加"
pm = dynamic
pm.max_children =20
pm.start_servers = 5
pm.min_spare_servers = 2