1.隐藏版本号
[root@server1 ~]# curl -I http://192.168.106.30
HTTP/1.1 200 OK
Server: nginx/1.6.0
####方法一####
[root@server1 ~]# vi /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; ####这里添加
[root@server1 ~]# systemctl restart nginx.service ## 重启服务
[root@server1 ~]# curl -I http://192.168.106.30
HTTP/1.1 200 OK
Server: nginx ##隐藏版本后缀
####方法二####
#define nginx_version 1006000
#define NGINX_VERSION "1.1.1" ##修改版本号
#define NGINX_VER "IIS/" NGINX_VERSION ###修改成微软IIS
1,修改src/core/nginx.h(Nginx内部名称的)
#define nginx_version 1006000
#define NGINX_VERSION "1.1.1" ##修改版本号
#define NGINX_VER "IIS/" NGINX_VERSION ###修改成微软IIS
2,修改src/http/ngx_http_header_filter_module.c(HTTP ResponseHeader)
static u_char ngx_http_server_string[] = "Server: nginx" CRLF
3,修改src/http/ngx_http_special_response.c(修改错误页的底部Footer)
static u_char ngx_http_error_tail[] = 下的nginx信息
修改完这些以后编译安装,或者重新编译。nginx版本信息则彻底修改。
######开启了fastcgi_param功能 把SERVER_SOFTWARE nginx;#######
vi /usr/local/nginx/conf/fastcgi_param
fastcgi_param SERVER_SOFTWARE nginx
2. 指定用户账号与组账号
[root@server1 ~]# vi /etc/nginx.conf
user nginx nginx; ###修改成用户账号和组账号都未nginx
[root@server1 ~]# systemctl restart nginx.service ### 重启服务
[root@server1 ~]# ps aux | grep nginx ### 查看服务进程
root 9879 0.0 0.0 24812 816 ? Ss 08:40 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 9880 0.0 0.1 27740 2104 ? S 08:40 0:00 nginx: worker process
nginx 9881 0.0 0.1 27740 2104 ? S 08:40 0:00 nginx: worker process
nginx 9882 0.0 0.1 27740 2096 ? S 08:40 0:00 nginx: worker process
nginx 9883 0.0 0.0 27740 1848 ? S 08:40 0:00 nginx: worker process
root 9885 0.0 0.0 112676 980 pts/0 R+ 08:40 0:00 grep --color=auto nginx
3. 配置nginx连接超时
vi/usr/local/nginx/nginx.conf
http {
····
keepalive_timeout 65 100; #前面参数是服务器主动关闭的超时时间,后面参数是客户机浏览器主动关闭的使时间
client_header_timeout 80; #等待客户端发送请求头的超时时间 超时会发送408错误
client_body_timeout 80; # 设置客户端发送请求体超时时间
····
}
4. 设置图片的缓存时间
vi/usr/local/nginx/nginx.conf
server{
···
location ~ \.(gif|jpg|jepg|png|ico)$ { ##在合适的位置添加
root /var/www/aaa/; ###存放图片的路径
expires 1d;
}
····
}
[root@server1 ~]# nginx -t ###检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 ~]# systemctl restart nginx.service ###重启服务
###抓包可以看到图片的缓存时间,再次抓包看不到缓存图片的状态,因为时从缓存读取的###
#######5 nginx的日志分割#######
vi /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
# server_tokens off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '##去掉3行注释,设置日志格式未main
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; ##开启日志功能
cd /opt
vi 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" ##定义变量 这里时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) ##命令的作用是发送kill -USR1信号给Nginx的主进程号,让Nginx重新生成一个新的日志文件。
find $logs_path-mtime +30 | xargs rm -rf ##超过31天的文件强制删除
#mtime参数的理解应该如下:
-mtime n 按照文件的更改时间来找文件,n为整数。
n表示文件更改时间距离为n天, -n表示文件更改时间距离在n天以内,+n表示文件更改时间距离在n天以前。
例如:
-mtime 0 表示文件修改时间距离当前为0天的文件,即距离当前时间不到1天(24小时)以内的文件。
-mtime 1 表示文件修改时间距离当前为1天的文件,即距离当前时间1天(24小时-48小时)的文件。
-mtime+1 表示文件修改时间为大于1天的文件,即距离当前时间2天(48小时)之外的文件
-mtime -1 表示文件修改时间为小于1天的文件,即距离当前时间1天(24小时)之内的文件
为什么-mtime+1 表示文件修改时间为大于1天的文件,即距离当前时间48小时之外的文件,而不是24小时之外的呢?
因为n值只能是整数,即比1大的最近的整数是2,所有-mtime+1不是比当前时间大于1天(24小时),而是比当前时间大于2天(48小时)。
文件的 Access time,atime 是在读取文件或者执行文件时更改的。
文件的 Modified time,mtime 是在写入文件时随文件内容的更改而更改的。
文件的 Create time,ctime 是在写入文件、更改所有者、权限或链接设置时随 Inode 的内容更改而更改的
##kill -USR1 Nginx主进程号 :命令的作用是发送kill -USR1信号给Nginx的主进程号,让Nginx重新生成一个新的日志文件。
在linux系统中,linux是通过信号与”正在运行的进程”进行通信的。linux系统中,也很多预定义好的信号,像SIGHUP。USR1是用户自定义信号。可以理解为:进程自己定义接到这个信号该干嘛(也就是进程编写者自己确定收到这个信号干嘛还是什么都不做都行,完全交给开发人员自己决定)。而在nginx中,它自己编写了代码处理当我接到USR1信号的时候,让nginx重新打开日志文件。具体原理如下:
1、nginx 的主进程收到USR1信号,会重新打开日志文件(以nginx配置文件中的日志名称命名,就是配置文件中access_log项所设置的值,如果文件不存在,会自动创建一个新的文件xxx.log)。
2、然后把日志文件的拥有者改为“工作进程(worker进程)”,目的是让worker进程就具备了对日志文件的读写权限(master和worker通常以不同用户运行,所以需要改变拥有者)。
3、nginx主进程会关闭重名的日志文件(也就是刚才使用mv命令重命名成xxx.log_ 20130909.log的文件),并通知工作进程使用新打开的日志文件(刚才主进程打开的文件xxx.log)。具体实现上更细化点就是,主进程把USR1信号发给worker,worker接到这个信号后,会重新打开日志文件(也就是配置文件中约定的xxx.log)
所以-USR1是Nginx的一个信号,那么还有哪些信号呢,这些信号有什么作用呢?总结如下:
主进程可以处理以下的信号:
TERM, INT 快速关闭
QUIT 从容关闭
HUP 重载配置
用新的配置开始新的工作进程
从容关闭旧的工作进程
USR1 重新打开日志文件
USR2 平滑升级可执行程序。
WINCH 从容关闭工作进程
尽管你不必自己操作工作进程,但是,它们也支持一些信号:
TERM, INT 快速关闭
QUIT 从容关闭
USR1 重新打开日志文件
5.更改Nginx运行进程数
[root@server1 nginx]# cat /proc/cpuinfo | grep -c "physical" ##查看cpu的个数
4
vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 4; ##根据cpu 更改进程数
worker_cpu_affinity 0001 0010 0100 1000; ##因为nginx 可能跑在同一个cpu上 这里分配到不同的cpu上
#####7配置Nginx实现网页压缩功能gzip#####
[root@server1 nginx]# vi /etc/nginx.conf
http{
···
gzip on; ##开器gzip
gzip_min_length 1k; ##压缩阈值,超过这个值就压缩
gzip_buffers 4 16k; ##buffer 大小为4个16k缓存区大小
gzip_http_version 1.1; ##压缩版本
gzip_comp_level 6; ##压缩比率,最小为1,处理速度快,传输速度慢;最大为9,处理速度慢,传输速度快。
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条件,支持正则,表示IE6一下不启用gzip
gzip_vary on; ##选择支持very header 有的服务不支持gzip ,可以让前端的缓存服务器缓存经过gzip压缩的页面
···
}
[root@server1 nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# systemctl restart nginx.service
6.配置Nginx实现防盗链
[root@server1 nginx]# vi /etc/nginx.conf
server {
···
location ~*\.(jpg|png|swf)$ { ###访问以jpg,png,swf三种格式为结尾的
valid_referers *.aaa.com aaa.com; ###有效来源为*.aaa.com aaa.com
if ($invalid_referer) { ###如果不是有效来源则返回值为403
return 403;
}
}
···
}
[root@server1 nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# systemctl restart nginx.service
break和last
1.当rewrite规则在location{}外,break和last作用一样,遇到break或last后,其后续的rewrite/return语句不再执行。但后续有location{}的话,还会近一步执行location{}里面的语句,前提是请求能匹配该location
2.当rewrite规则在location{}里,遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行
3.当rewrite规则在location{}里,遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头匹配所有location
return的用法
该指令一般用于对请求的客户端直接返回响应状态码。在该作用域内return后面的所有nginx配置都是无效的,可以使用在server、location以及if配置中,除了支持跟状态码,还可以跟字符串或者url链接。
rewrite的语法规则
格式:rewrite regex replacement [flag]
rewrite 配置可以在server、location以及if配置段内生效
regex 是用于匹配URI的正则表达式,其不会匹配到$host(域名)
replacement 是目标跳转的URI,可以以http://或者https://开头,也可以省略掉$host,直接写$request_uri部分
flag 用来设置rewrite对URI的处理行为,其中有break、last、rediect、permanent,其中break和last在前面已经介绍过,rediect和permanent的区别在于,前者为临时重定向(302),而后者是永久重定向(301),对于用户通过浏览器访问,这两者的效果是一致的。
但是,对于搜索引擎爬虫来说就有区别了,使用301更有利于SEO。所以,建议replacemnet是以http://或者https://开头的,flag使用permanent。
7.fpm参数优化
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini ##带参数的启动fpm
[root@server1 system]# cd /usr/local/php/etc/php-fpm.d/
[root@server1 php-fpm.d]# vi www.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20 #改 static 模式下空闲进程数上线,大于下面的值
pm.start_servers = 5 #改 动态方式下默认开启的进程数,在最小和最大之间
pm.min_spare_servers = 2 #改 动态方式下最小空闲进程数
pm.max_spare_servers = 8 #改 动态方式下最大空闲进程数