隐藏版本号
在http中加入server_tokes off是全局设置,在server中添加是为当前主机设置
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
charset utf-8;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
server_tokens off;
}
隐藏版本号之前
隐藏版本号之后
修改版本号
vi /usr/local/nginx/conf/nginx.conf
server_tokens on;
vi nginx-1.12.2/src/core/nginx.h
#define nginx_version 1012002
#define NGINX_VERSION "1.1.1"
#define NGINX_VER "IIS/" NGINX_VERSION
make && make install
修改版本号之后
连接超时
参数:
Keepalive_timeout:设置连接保持超时时间
Client_header_timeout:指定等待客户端发送请求头的超时时间
Client_body_timeout:设置请求体读超时时间
vi /usr/local/nginx/conf/nginx.conf
// http中添加参数
keepalive_timeout 65 100;
client_header_timeout 65;
client_body_timeout 65;
systemctl restart nginx
设置连接超时后
网页缓存
一般针对静态网页设置,对动态网页不设置缓存时间
在http中添加为全局,在server中添加为本机,在location中添加为该网页
vi /usr/local/nginx/conf/nginx.conf
location ~ \.(gif|jpg|jepg|png)$ {
root html;
expires 1d;
}
日志分割
- 设置时间变量
- 设置保存日志路径
- 将目前的日志文件进行重命名
- 重建新日志文件
- 删除时间过长的日志文件
- 设置cron任务,定期执行脚本自动进行日志分割
vi fenge.sh
#!/bin/bash
# fenge
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/aa.com-access.log-$d
kill -USR1 $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf
chmod +x fenge.sh
// 创建计划任务
crontab -e
0 1 * * * /usr/bin/sh /root/fenge.sh
防盗链
在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济损失
Nginx防盗链功能非常强大。默认情况下,只需要进行简单的配置,即可实现防盗链处理
vi /usr/local/nginx/conf/nginx.conf
location ~* \.(swf|jpg|jepg)$ {
valid_referers 20.0.0.14;
if ($invalid_referer) {
rewrite ^/ http://20.0.0.14/error.png;
}
}
访问20.0.0.14
访问20.0.0.15
FPM参数优化
Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整
vi /usr/local/php/etc/php-fpm.d/www.conf
pm = dynamic
pm.max_children=20 #static模式下空闲进程数上限,大于下面的值
pm.start_servers = 5 #动态方式下默认开启的进程数,在最小和最大之间
pm.min_spare_servers = 2 #动态方式下最少空闲进程数
pm.max_spare_servers = 8 #动态方式下最大空闲进程数