一、静态资源优化
1.静态资源
2.静态资源缓存
1)配置缓存过期时间
2)配置不走缓存
3.静态资源读取
1)文件高效读取
2)文件高效传输
3)长连接
4)长连接传输
4.静态资源压缩
1)静态资源压缩配置语法
2)压缩配置
3)上传文件
二、防资源盗链
1.配置被盗连的网站
2.配置盗链的网站
3.配置hosts访问测试
4.配置防盗链语法
5.配置防盗链
6.伪造请求头
7.允许多个域名盗链
三、跨域访问
1.盗链和跨域的区别
2.配置被跨域的网站
3.配置跨域网站
4.配置hosts测试
5.测试跨域访问
6.配置允许被跨域
四、CPU亲和
1.查看cpu状态
2.修改nginx配置
3.没有配置亲和的情况
4.配置cpu亲和
1)方式1
2)方式2
3)方式3
5.配置CPU亲和后
五、nginx通用优化文件
1.通用优化配置
2.nginx优化总结
六、PHP优化
1.配置PHP页面
2.php.ini配置文件优化
3.PHP危险函数
4.php-fpm服务配置
1)php-fpm.conf配置优化
2)包含配置文件优化 /etc/php-fpm.d/*.conf
5.php状态页
1)配置php
2)配置nginx
3)访问页面
服务器上:
#语法
Syntax: expires [modified] time;
expires epoch | max | off;
Default: expires off;
Context: http, server, location, if in location
#配置
[root@web01 conf.d]# vim linux.cache.com.conf
server {
listen 80;
server_name linux.cache.com;
location ~* \.(png|jpg|gif)$ {
root /code/cache;
expires 7d;
}
}
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# cd /code/
[root@web01 code]# mkdir cache
[root@web01 code]# cd cache/
[root@web01 cache]# rz
[root@web01 cache]# ll
total 1868
-rw-r--r-- 1 root root 156617 Dec 7 08:54 1.jpg
-rw-r--r-- 1 root root 47542 Dec 7 08:54 2.jpg
-rw-r--r-- 1 root root 1586108 Dec 7 08:54 3.jpg
-rw-r--r-- 1 root root 113900 Dec 7 09:28 4.jpg
[root@web01 cache]# chown -R www.www /code/cache/
2)配置不走缓存
方法一:使用无痕模式
方法二:开启浏览器上面的 Disable cache
方法三:配置nginx关闭缓存
[root@web01 conf.d]# vim linux.cache.com.conf
server {
listen 80;
server_name linux.cache.com;
location ~* \.(png|jpg|gif)$ {
root /code/cache;
etag off;
add_header Cache-Control no-cache;
if_modified_since off;
}
}
1)文件高效读取
Syntax: sendfile on | off;
Default: sendfile off;
Context: http, server, location, if in location
2)文件高效传输
#将多个数据打个包,一次推送,大文件适合此配置,需要开启 sendfile
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location
3)长连接
Syntax: keepalive_timeout timeout [header_timeout];
Default: keepalive_timeout 75s;
Context: http, server, location
4)长连接传输
#来一条数据传输一条数据,需要开启 keepalive
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location
1)静态资源压缩配置语法
#开启压缩
Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in location
#指定压缩文件的类型
Syntax: gzip_types mime-type ...;
Default: gzip_types text/html;
Context: http, server, location
#指定压缩的级别,压缩比例
Syntax: gzip_comp_level level;
Default: gzip_comp_level 1; #共1-9个级别,一般我们设置3-5
Context: http, server, location
#压缩后传输使用的协议
Syntax: gzip_http_version 1.0 | 1.1;
Default: gzip_http_version 1.1;
Context: http, server, location
2)压缩配置
[root@web01 cache]# vim /etc/nginx/conf.d/linux.gzip.com.conf
server {
listen 80;
server_name linux.gzip.com;
location ~* \.(png|jpg|gif)$ {
root /code/cache;
gzip on;
gzip_types image/jpeg image/gif image/png;
gzip_comp_level 9;
}
location ~* \.txt$ {
root /code/cache;
gzip on;
gzip_types text/plain;
gzip_comp_level 5;
}
}
3)上传文件
[root@web01 cache]# ll -h
total 134M
-rw-r--r-- 1 www www 153K Dec 7 08:54 1.jpg
-rw-r--r-- 1 www www 125M Dec 15 09:38 1.png
-rw-r--r-- 1 www www 7.1M Dec 15 09:33 1.txt
-rw-r--r-- 1 www www 47K Dec 7 08:54 2.jpg
-rw-r--r-- 1 www www 1.6M Dec 7 08:54 3.jpg
-rw-r--r-- 1 www www 112K Dec 7 09:28 4.jpg
[root@web01 conf.d]# vim linux.beidaolian.com.conf
server {
listen 80;
server_name linux.beidaolian.com;
location ~* \.(png|jpg|gif)$ {
root /code;
index index.html;
}
}
[root@web01 ~]# systemctl restart nginx
#准备站点和文件
[root@web01 conf.d]# echo "我是被盗连的机器" > /code/index.html
[root@web01 code]# ll /code/
-rw-r--r-- 1 www www 31962 Dec 14 09:37 404.jpg
-rw-r--r-- 1 www www 25 Dec 15 10:18 index.html
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.daolian.com.conf
server {
listen 80;
server_name linux.daolian.com;
location ~* / {
root /code;
index index.html;
}
}
[root@lb01 ~]# systemctl restart nginx
#准备站点
[root@lb01 ~]# vim /code/index.html
"http://linux.beidaolian.com/404.jpg">
</body>
</html>
10.0.0.4 linux.daolian.com
10.0.0.7 linux.beidaolian.com
#windows访问
http://linux.daolian.com/
Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location
none #nginx日志中referer部分为空
blocked #nginx日志中referer部分没有协议
server_names #nginx日志中referer部分为指定的域名
string #nginx日志中referer部分为指定的域名(可以使用正则表达式)
[root@web01 conf.d]# vim linux.beidaolian.com.conf
server {
listen 80;
server_name linux.beidaolian.com;
location / {
root /code;
index index.html;
}
location ~* \.jpg$ {
root /code;
valid_referers none blocked server_names;
if ($invalid_referer) {
return 500;
}
}
}
#模拟请求头为 http://linux.daolian.com 访问图片
[root@lb01 ~]# curl -e "http://linux.daolian.com" -I http://linux.beidaolian.com/404.jpg
HTTP/1.1 500 Internal Server Error
Server: nginx/1.18.0
Date: Tue, 15 Dec 2020 02:51:41 GMT
Content-Type: text/html; charset=utf8
Content-Length: 177
Connection: close
#模拟请求头为 http://linux.beidaolian.com 访问图片
[root@lb01 ~]# curl -e "http://linux.beidaolian.com" -I http://linux.beidaolian.com/404.jpg
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Tue, 15 Dec 2020 02:52:24 GMT
Content-Type: image/jpeg
Content-Length: 31962
Last-Modified: Mon, 14 Dec 2020 01:37:29 GMT
Connection: keep-alive
ETag: "5fd6c1d9-7cda"
Accept-Ranges: bytes
[root@web01 conf.d]# vim linux.beidaolian.com.conf
server {
listen 80;
server_name linux.beidaolian.com;
location / {
root /code;
index index.html;
}
location ~* \.jpg$ {
root /code;
valid_referers none blocked server_names *.baidu.com;
if ($invalid_referer) {
return 500;
}
}
}
[root@lb01 ~]# curl -e "http://www.baidu.com" -I http://linux.beidaolian.com/404.jpg
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Tue, 15 Dec 2020 02:58:27 GMT
Content-Type: image/jpeg
Content-Length: 31962
Last-Modified: Mon, 14 Dec 2020 01:37:29 GMT
Connection: keep-alive
ETag: "5fd6c1d9-7cda"
Accept-Ranges: bytes
盗链是由盗链的网站向被盗链的网站发起get请求获取内容
跨域是由跨域的网站向被跨域的网站发起一个完整http请求,甚至是完全跳转
[root@web01 conf.d]# vim linux.beikuayu.com.conf
server {
listen 80;
server_name linux.beikuayu.com;
location / {
root /code;
index index.html;
}
}
#配置站点
[root@web01 conf.d]# echo "被跨域" > /code/index.html
[root@lb01 conf.d]# vim linux.kuayu.com.conf
server {
listen 80;
server_name linux.kuayu.com;
location ~* / {
root /code;
index index.html;
}
}
#配置跨域的站点
[root@lb01 conf.d]# vim /code/index.html
"en">
"UTF-8" />
测试ajax和跨域访问</title>