第四节:nginx优化二

Nginx优化

一、静态资源优化

1.静态资源

2.静态资源缓存

1.Etag:服务器上的文件唯一标示
2.Last-Modified:服务器上的文件最后修改时间
3.Expires:文件缓存过期时间
4.Cache-Control:文件多久过期

5.If-None-Match:浏览器上的文件唯一标示
6.If-Modified-Since:浏览器上的文件最后修改时间

1)配置缓存过期时间

#语法
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)配置不走缓存

1.使用无痕模式
2.开启浏览器上面的 Disable cache
3.配置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;
    }
}

3.静态资源读取

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

4.静态资源压缩

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

二、防资源盗链

1.配置被盗连的网站

[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

2.配置盗链的网站

[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
<html>
    <body>
        <img src="http://linux.beidaolian.com/404.jpg">
    </body>
</html>

3.配置hosts访问测试

10.0.0.4 linux.daolian.com
10.0.0.7 linux.beidaolian.com

#windows访问
http://linux.daolian.com/

4.配置防盗链语法

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部分为指定的域名(可以使用正则表达式)

5.配置防盗链

[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;
        }
    }
}

6.伪造请求头

#模拟请求头为 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

7.允许多个域名盗链

[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

三、跨域访问

1.盗链和跨域的区别

盗链是由盗链的网站向被盗链的网站发起get请求获取内容

跨域是由跨域的网站向被跨域的网站发起一个完整http请求,甚至是完全跳转

2.配置被跨域的网站

[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

3.配置跨域网站

[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 
<html lang="en">
<head>
        <meta charset="UTF-8" />
        <title>测试ajax和跨域访问</title>
        <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
        $.ajax({
        type: "GET",
        url: "http://linux.beikuayu.com",
        success: function(data) {
                alert("sucess 卧槽 卧槽 卧槽 成功了!!!");
        },
        error: function() {
                alert("fail!!,跨不过去啊,不让进去啊,只能蹭蹭!");
        }
        });
});
</script>
        <body>
                <h1>测试跨域访问</h1>
        </body>
</html>

4.配置hosts测试

#windows的hosts
10.0.0.4 linux.kuayu.com
10.0.0.7 linux.beikuayu.com

[root@web01 conf.d]# vim /etc/hosts
10.0.0.4 linux.kuayu.com
10.0.0.7 linux.beikuayu.com

[root@lb01 conf.d]# vim /etc/hosts
10.0.0.4 linux.kuayu.com
10.0.0.7 linux.beikuayu.com

5.测试跨域访问

6.配置允许被跨域

[root@web01 conf.d]# vim linux.beikuayu.com.conf 
server {
    listen 80;
    server_name linux.beikuayu.com;

    location / {
        root /code;
        index index.html;
        #允许跨域的网站
        add_header Access-Control-Allow-Origin *;
        #允许跨域网站发起的请求类型
        add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS';
    }
}

四、CPU亲和

1.查看cpu状态

[root@web01 ~]# lscpu
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    4
NUMA node0 CPU(s):     0-3

2.修改nginx配置

[root@web01 ~]# vim /etc/nginx/nginx.conf 
worker_processes  4;

[root@web01 ~]# systemctl restart nginx

3.没有配置亲和的情况

[root@web01 ~]# ps -eo pid,args,psr | grep [n]ginx
  7549 nginx: master process /usr/   3
  7550 nginx: worker process         2
  7551 nginx: worker process         0
  7552 nginx: worker process         1
  7553 nginx: worker process         1

4.配置cpu亲和

1)方式1

worker_processes    4;
worker_cpu_affinity 0001 0010 0100 1000;

worker_processes    16;
worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 ...;

2)方式2

worker_processes    2;
worker_cpu_affinity 0101 1010;

3)方式3

worker_processes auto;
worker_cpu_affinity auto;

5.配置CPU亲和后

[root@web01 ~]# ps -eo pid,args,psr | grep [n]ginx
  7629 nginx: master process /usr/   3
  7630 nginx: worker process         0
  7631 nginx: worker process         1
  7632 nginx: worker process         2
  7633 nginx: worker process         3

五、nginx通用优化文件

1.通用优化配置

[root@nginx ~]# cat nginx.conf
user www;                                   #nginx启动用户
worker_processes auto;                       #nginx工作进程数
worker_cpu_affinity auto;                     #开启CPU亲和
error_log /var/log/nginx/error.log warn;      #错误日志,存放路径,记录日志的级别
pid /run/nginx.pid;                         #指定pid文件位置
worker_rlimit_nofile 35535;                  #指定nginx服务的最大打开文件数

events {
    use epoll;                              #使用epoll网络模型
    worker_connections 10240;                #worker工作进程的最大连接数
}

http {
    include             mime.types;                #nginx能识别的文件类型
    default_type        application/octet-stream;   #nginx不识别的文件类型默认下载
    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     #指定访问日志路径,调用日志的格式
    server_tokens off;                            #隐藏版本号
    client_max_body_size 200m;                     #上传文件大小限制
    sendfile            on;                        #高效读取
    tcp_nopush          on;                         #高效传输
    #tcp_nodelay         on;                        #实时传输
    keepalive_timeout   65;                         #开启长连接
    gzip on;                                      #开启压缩
    gzip_disable "MSIE [1-6]\.";                  #指定不压缩的浏览器
    gzip_http_version 1.1;                          #压缩后传输的协议
    gzip_comp_level 4;                              #压缩的级别
    gzip_buffers 16 8k;                             #压缩缓存
    gzip_min_length 1024;                           #开启压缩的最小值
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg;       #压缩的文件类型
    include /etc/nginx/conf.d/*.conf;               #包含的配置文件
}

2.nginx优化总结

1、CPU亲和、worker进程数、调整nginx进程打开的文件句柄数
2、使用Epool网络模型、调整每个worker进程的最大连接数
3、文件的高效读取sendfile、nopush
4、文件的传输实时性、nodealy
5、开启tcp长连接,以及长连接超时时间keepalive_timeout
6、开启文件传输压缩gzip
7、开启静态文件expires缓存
8、隐藏nginx版本号
9、禁止通过ip地址访问,禁止恶意域名解析,只允许域名访问
10、配置防盗链、以及跨域访问
11、防DDOS、cc攻击,限制单IP并发连接,以及http请求
12、优雅显示nginx错误页面
13、nginx加密传输https优化
14、nginx proxy_cache、fastcgi_cache、uwsgi_cache 代理缓存,第三方工具(squid、varnish)

六、PHP优化

1.配置PHP页面

[root@web01 ~]# vim /etc/nginx/conf.d/php.conf
server {
    listen 80;
    server_name linux.phpserver.com;
    root /code/phpserver;
    index index.php;

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

#配置站点
[root@web01 ~]# mkdir /code/phpserver
[root@web01 ~]# vim /code/phpserver/index.php 
<?php
    phpinfo();

2.php.ini配置文件优化

联系管理员微信tutu19192010,注册账号

上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 帮助IT小伙伴学到真正的技术