(1) 动态压缩
(2) 静态压缩
2、expires浏览器缓存
3、网络IO事件模型优化
4、隐藏版本号和源码包修改名称和版本号
(1)隐藏版本号
(2)源码包修改名称和版本号
5、防盗链优化
6、nginx不记录不需要的日志
7、限制上传到资源目录的程序被访问,防止木马入侵系统破坏文件
(1)根据扩展名限制程序和文件访问
(2)禁止访问指定目录下的所有文件和目录
8、FastCGI参数buffer和cache配置文件的优化
9、php.ini和php-fpm.conf配置文件的优化
(1)php-fpm参数调优
(2)php-ini配置优化
10、开启高效文件传输模式(零拷贝)
11、阿里云nginx加密传输优化(SSL)
12、CPU亲和
1、gzip资源压缩
(1) 动态压缩
打开nginx配置文件 nginx.conf
里面添加或者用include引用
# 打开压缩模块
gzip on;
# 最小多少就不压缩了
gzip_min_length 1k;
# buffer缓冲
gzip_buffers 4 16k;
# 使用http那个版本。有1.0和1.1,主流是1.1
gzip_http_version 1.1;
# gzip压缩率。有1到9,数字越大压缩比例越大,同时耗费性能越大
gzip_comp_level 2;
# 进行压缩的文件类型
gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# no Vary
gzip_vary off;
# IE1到6不支持压缩,这里就把它踢出去了
gzip_disable "MSIE [1-6]\.";
保存重新加载配置文件 nginx -s reload
最后用 curl
命令测试一下 curl -I -H "Accept-Encoding: gzip, deflate" "网站域名/各种文件类型"
(2) 静态压缩
在nginx配置文件 server
标签里加入下面内容(这里为了小范围测试就不写在http标签了)
# 匹配后缀是png格式的图片
location ~ .*\.png$ {
# 打开静态压缩,需要打开gzip模块
gzip_static on;
# nginx作为反向代理时候header头包含什么信息启动压缩
gzip_proxied expired no-cache no-store private auth;
}
保存重新加载配置文件 nginx -s reload
并把png格式的图片压缩一下 tar -zcvf test.png.gz test.png
最后用 curl -I -H "Accept-Encoding: gzip, deflate" 网站域名加图片url
测试一下
Content-Length
和压缩文件大小比对一下
2、expires浏览器缓存
打开nginx配置文件在 location
标签中添加expires
# 匹配后缀是jpg格式的图片
location ~ .*\.jpg$ {
# 被缓存文件的路径
root /soft/code/;
# 缓存三天
expires 3d;
}
保存重新加载配置文件 nginx -s reload
打开网站图片测试一下
3、网络IO事件模型优化
打开nginx配置文件 nginx.conf
里面添加
events {
use epoll;
# 限制每个进程处理多少个连接请求
worker_connections 1024;
}
4、隐藏版本号和源码包修改名称和版本号
(1)隐藏版本号
先用 curl -I 网站域名
检测一下 Server
里面的
打开nginx配置文件 nginx.conf
在 http
标签添加 server_tokens off;
就可以了
http {
# 隐藏nginx版本号
server_tokens off;
再用 curl -I 网站域名
检测一下 Server
里面的
(2)源码包修改名称和版本号
下载且解压完源码包(这里就介绍怎么下载和解压了)
vim nginx-1.14.2/src/core/nginx.h +13
vim nginx-1.14.2/src/http/ngx_http_header_filter_module.c +49
vim nginx-1.14.2/src/http/ngx_http_special_response.c +36
接下来就是编译安装nginx,再用 curl -I 网站域名
检测一下
5、防盗链优化
防盗链 valid_referers
参数
在nginx配置文件 server
标签里加入下面内容
# 匹配后缀是jpg格式的图片(有一样的匹配科技直接在里面写)
location ~ .*\.jpg$ {
valid_referers none blocked www.songguoyou.com;
if ($invalid_referer) {
return 403;
}
}
保存重新加载配置文件 nginx -s reload
用 curl -e "域名网站"
测试一下
# 用自己网站域名访问
[root@nginx ~]# curl -I -e "http://www.songguoyou.com" http://www.songguoyou.com/wp-content/test/test.jpg
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 27 Feb 2019 07:17:10 GMT
Content-Type: image/jpeg
Content-Length: 19452
Last-Modified: Sun, 10 Jun 2018 12:16:57 GMT
Connection: keep-alive
ETag: "5b1d16b9-4bfc"
Accept-Ranges: bytes
# 用别人网站域名访问
[root@nginx ~]# curl -I -e "http://www.baidu.com" http://www.songuoyou.com/wp-content/test/test.jpg
HTTP/1.1 403 Forbidden
Server: nginx
Date: Wed, 27 Feb 2019 07:17:14 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 146
Connection: keep-alive
6、nginx不记录不需要的日志
在nginx配置文件 server
标签里加入下面内容
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
access_log off;
}
7、限制上传到资源目录的程序被访问,防止木马入侵系统破坏文件
(1)根据扩展名限制程序和文件访问
在nginx配置文件 server
标签中禁止访问上传资源目录下的PHP,SHELL,PERL,PYTHON程序文件,这样就算是用户上传了木马文件也没办法执行
location ~ ^/images/.*\.(php|php5|.sh|.pl|.py)${
deny all;
}
location ~ ^/static/.*\.(php|php5|.sh|.pl|.py)${
deny all;
}
location ~* ^/data/(attachment|avatar)/.*\.(php|php5)${
deny all;
}
对于上述目录的限制必须写在nginx处理PHP服务配置的前面
(2)禁止访问指定目录下的所有文件和目录
配置禁止访问指定的单个或多个目录
location ~ ^/(static)/ {
deny all;
}
location ~ ^/static {
deny all;
}
禁止访问目录并且返回代码404
location /static/ {
return 404;
}
8、FastCGI参数buffer和cache配置文件的优化
打开nginx配置文件 nginx.conf
里面添加或者用include引用
fastcgi_connect_timeout 240; # Nginx和FastCGI连接超时时间
fastcgi_send_timeout 240; # Nginx允许FastCGI返回数据超时时间
fastcgi_read_timeout 240; # Nginx从FastCGI读取响应信息超时时间
fastcgi_buffer_size 64k; # 读取fastCGI应答第一部分需要多大缓冲区
fastcgi_buffers 4 64k; # 设定用来读取从FastCGI端收到响应信息的缓冲区大小和数量
fastcgi_busy_buffers_size 128k; # 用于设置系统很忙时可以使用的proxy_buffers大小
fastcgi_temp_file_write_size 128k; # FastCGI临时文件的大小
# fastcti_temp_path /data/ngx_fcgi_tmp; # 从其他服务器传送临时文件存放路径
fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; # 缓存目录(手动创建)
9、php.ini和php-fpm.conf配置文件的优化
调高linux内核打开文件数量,可以使用这些命令(必须是root帐号)
echo `ulimit -HSn 65535` >> /etc/profile
echo `ulimit -HSn 65535` >> /etc/rc.local
source /etc/profile
如果ulimit -n
数量依旧不多的话, 可以在 /etc/security/limits.conf 文件最后加上
* soft nofile 51200
* hard nofile 51200
(1)php-fpm参数调优
在 www
池里的优化
user = nginx # 进程的发起用户(和web容器同一个用户)
group = nginx #进程的发起用户组(和web容器同一个用户组)
# listen = 127.0.0.1:9000 # 监听ip端口(我这里用的socket方式访问,所以注释了)
listen = /dev/shm/php-fastcgi.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
# unix socket设置选项,如果使用tcp方式访问,上面四行注释即可
listen.allowed_clients = 127.0.0.1
pm = dynamic; # 表示使用(dynamic|static)两种进程数量管理方式
pm.max_children = 300 # 静态下开启的php-fpm进程数量(pm=static生效)
pm.start_servers = 20 # 动态下的起始php-fpm进程数量(pm=dynamic生效)
pm.min_spare_servers = 5 # 动态下的最小php-fpm进程数量(pm=dynamic生效)
pm.max_spare_servers = 35 # 动态下的最大php-fpm进程数量(pm=dynamic生效)
pm.max_requests = 10240 # 表示使用最大请求数
request_terminate_timeout = 30 # 最大执行时间, 在php.ini中也可以进行配置(max_execution_time)
request_slowlog_timeout = 2 # 开启慢日志
slowlog = log/$pool.log.slow # 慢日志路径
rlimit_files = 1024 # 增加php-fpm打开文件描述符的限制
nginx和php连接配置
location ~ \.php$ {
# fastcgi_pass 127.0.0.1:9000; # 监听ip端口(我这里用的socket方式访问,所以注释了)
fastcgi_pass unix:/dev/shm/php-fastcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /soft/code/wordpress$fastcgi_script_name;
include fastcgi_params;
(2)php-ini配置优化
safe_mode = On # 启用安全模式
safe_mode_exec_dir = Off # 关闭用户组安全(启动安全模式自动关闭,但最好设为关闭)
disable_functions = passthru,exec,system,popen,chroot,escapeshellcmd,escapeshellarg # 禁用危险函数
expose_php = Off # # 隐藏php版本号
register_globals = Off # 关闭注册全局变量
magic_quotes_pgc = On # 转换SQL命令,防止SQL注入
display_errors = Off # 关闭错误信息提示,防止信息泄露
log_errors = On # 打开错误日志
max_execution_time = 30 # 最大执行时间, 在php-fpm中也可以进行配置(request_terminate_timeout)
memory_limit = 128M # 脚本使用的最大内存
max_input_time = 60 # 每个脚本等待输入数据最长时间(秒),-1表示不限制
upload_max_filesize = 2M # 上传文件的最大许可大小
allow_url_fopen = Off # 禁止打开远程地址
cgi.fix_pathinfo=0 # 防止Nginx文件类型错误解析漏洞
session.save_handler = files # 会话默认为文件("files")
# 自定义的处理器(如基于数据库的处理器),可用"user"
# 设为"memcache"则可以使用memcache作为会话处理器(需要指定"--enable-memcache-session"编译选项)
session.save_path = "/tmp" # 会话保存路径
# 配置memcache
# session.save_handler = memcache
# session.save_path = "tcp://127.0.0.1:11211"
10、开启高效文件传输模式(零拷贝)
http {
# 零拷贝
sendfile on;
# 启用TCP_CORK,建议打开,需要打开sendfile。默认关闭
tcp_nopush on;
# 禁用Nagle算法,建议打开,需要打开keepalived。默认关闭
tcp_nodelay on;
}
11、阿里云nginx加密传输优化(SSL)
配置苹果要求的证书
12、CPU亲和
是把进程绑定到相应的CPU内核上,减少进程之间不断频繁迁移,也减少CPU性能损耗
可以用 lscpu|grep "CPU(s)"
命令查看物理状态,多少核心
在 nginx.conf
的最外面添加下面两行
worker_processes auto;
worker_cpu_affinity auto;
保存重新加载配置文件后,用 ps -eo pid,args,psr|grep [n]ginx
查看 nginx worker
绑定情况
最后来个 Nginx
通用优化配置文件
user nginx;
# CPU亲和
worker_processes auto;
worker_cpu_affinity auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
use epoll;
# 限制每个进程处理多少个连接请求
worker_connections 1024;
}
http {
# 隐藏nginx版本号
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 统一使用utf-8字符集
charset utf-8;
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 /var/log/nginx/access.log main;
# 零拷贝
sendfile on;
# 启用TCP_CORK,建议打开,需要打开sendfile。默认关闭
tcp_nopush on;
# 禁用Nagle算法,建议打开,需要打开keepalived。默认关闭
tcp_nodelay on;
keepalive_timeout 65;
# 打开压缩模块
gzip on;
# 最小多少就不压缩了
gzip_min_length 1k;
# buffer缓冲
gzip_buffers 4 16k;
# 使用http那个版本。有1.0和1.1,主流是1.1
gzip_http_version 1.1;
# gzip压缩率。有1到9,数字越大压缩比例越大,同时耗费性能越大
gzip_comp_level 2;
# 进行压缩的文件类型
gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# no Vary
gzip_vary off;
# IE1到6不支持压缩,这里就把它踢出去了
gzip_disable "MSIE [1-6]\.";
fastcgi_connect_timeout 240; # Nginx和FastCGI连接超时时间
fastcgi_send_timeout 240; # Nginx允许FastCGI返回数据超时时间
fastcgi_read_timeout 240; # Nginx从FastCGI读取响应信息超时时间
fastcgi_buffer_size 64k; # 读取fastCGI应答第一部分需要多大缓冲区
fastcgi_buffers 4 64k; # 设定用来读取从FastCGI端收到响应信息的缓冲区大小和数量
fastcgi_busy_buffers_size 128k; # 用于设置系统很忙时可以使用的proxy_buffers大小
fastcgi_temp_file_write_size 128k; # FastCGI临时文件的大小
# fastcti_temp_path /data/ngx_fcgi_tmp; # 从其他服务器传送临时文件存放路径
fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; # 缓存目录(手动创建)
include /etc/nginx/conf.d/*.conf;
}