Nginx 优化

Nginx优化

一、优化概述

  • 需要了解

1、首先需要了解我们当前系统的结构和瓶颈,了解当前使用的是什么,运行的是什么业务,都有哪些服务,了解每个服务最大能支撑多少并发。比如nginx作为静态资源服务并发是多少,最高瓶颈在哪里,能支持多少qps(每秒查询率)的访问请求,那我们怎么得出这组系统结构瓶颈呢,比如top查看系统的CPU负载、内存使用率、总得运行进程等,也可以通过日志去分析请求的情况,当然也可以通过我们前面介绍到的stub_status模块查看当前的连接情况,也可以对线上的业务进行压力测试(低峰期),去了解当前这套系统能承担多少的请求和并发,以做好响应的评估。这个是我们做性能优化最先考虑的地方。

2、其次我们需要了解业务模式,虽然我们是做性能优化,但每一个性能的优化都是为业务所提供的服务的,我们需要了解每个业务接口的类型,比如:电商网站中的抢购模式,这种情况下,平时没什么流量,但到了抢购时间流量会突增。我们还需要了解系统层次化的结构,比如:我们使用nginx做的是代理、还是动静分离、还是后端直接服务用户,那么这个就需要我们对每一层做好相应的梳理。以便更好的服务业务。

3、最后我们需要考虑性能与安全,往往注重了性能,但是忽略了安全。往往过于注重安全,对性能又会产生影响。比如:我们在设计防火墙功能时,检测过于严密,这样就会给性能带来影响。那么如果对于性能完全追求,却不顾服务的安全,这个也会造成很大的隐患,所以需要评估好两者的关系,把握好两者的孰重孰轻。以及整体的相关性,权衡好对应的点。

1.首先需要了解我们当前系统的结构和瓶颈
2.其次我们需要了解业务模式
3.我们还需要了解系统层次化的结构
4.最后我们需要考虑性能与安全
  • 从哪些方面入手

OSI七层模型:物理层,数据链路层,网络层,传输层,会话层,表示层,应用层

1.硬件:
		1)nginx做负载均衡只需要cpu核心多一些
		2)nginx做静态资源存储,磁盘大一些
		3)nginx做动态资源代理,CPU
		4)ES,redis服务内存需要大一些
2.网络层:
		1)丢包、延迟
		2)带宽
3.系统:
		1)文件描述符
	 	2)端口复用
4.应用:tcp长连接
5.服务:服务的针对性优化
  • 影响性能的指标

1.网络
2.系统
3.服务
4.程序
5.数据库

二、ab测试工具

  • 安装ab测试工具

#查看命令所在的包
[root@web01 ~]# yum provides ab

#安装ab
[root@web01 ~]# yum install -y httpd-tools
  • 工具使用

[root@web01 ~]# ab -n 200 -c 2 http://www.baidu.com/

-n	请求的次数
-c	请求的并发数
-k	开启长连接

Usage: ab [options] [http[s]://]hostname[:port]/path
  • 配置nginx网站

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

    location / {
        try_files $uri $uri/ @java;
    }

    location @java {
        proxy_pass http://172.16.1.7:8080;
    }
}

[root@web01 conf.d]# echo "test nginx web" > /code/index.html
  • 配置hosts压测nginx访问静态资源

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

[root@web01 conf.d]# ab -n 10000 -c 200 http://linux.ab.com/
Server Software:        nginx/1.18.0
Server Hostname:        linux.ab.com
Server Port:            80

Document Path:          /
Document Length:        15 bytes

Concurrency Level:      200
Time taken for tests:   1.084 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2590000 bytes
HTML transferred:       150000 bytes
Requests per second:    9226.11 [#/sec] (mean)
Time per request:       21.678 [ms] (mean)
Time per request:       0.108 [ms] (mean, across all concurrent requests)
Transfer rate:          2333.56 [Kbytes/sec] received
  • 压测tomcat访问静态资源

[root@web01 conf.d]# mv /code/ /codebak

[root@web01 conf.d]# ab -n 10000 -c 200 http://linux.ab.com/
Server Software:        nginx/1.18.0
Server Hostname:        linux.ab.com
Server Port:            80

Document Path:          /
Document Length:        20 bytes

Concurrency Level:      200
Time taken for tests:   3.025 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2720000 bytes
HTML transferred:       200000 bytes
Requests per second:    1739.70 [#/sec] (mean)
Time per request:       114.962 [ms] (mean)
Time per request:       0.575 [ms] (mean, across all concurrent requests)
Transfer rate:          462.11 [Kbytes/sec] received
  • 压测httpd访问静态资源

[root@web01 conf.d]# yum install -y httpd
[root@web01 conf.d]# echo "test httpd" > /var/www/html/index.html
[root@web01 conf.d]# systemctl stop nginx
[root@web01 conf.d]# systemctl start httpd

[root@web01 conf.d]# ab -n 10000 -c 200 http://10.0.0.7/
Server Software:        Apache/2.4.6
Server Hostname:        10.0.0.7
Server Port:            80

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      200
Time taken for tests:   2.888 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2810000 bytes
HTML transferred:       110000 bytes
Requests per second:    3462.37 [#/sec] (mean)
Time per request:       57.764 [ms] (mean)
Time per request:       0.289 [ms] (mean, across all concurrent requests)
Transfer rate:          950.12 [Kbytes/sec] received
  • 结论

nginx处理静态资源的速度比其他web服务要快
  • tomcat安装方式

#1.下载或上传tomcat包
[root@web01 ~]# mkdir /service
[root@web01 ~]# cd /service
[root@web01 service]# rz apache-tomcat-8.5.51.tar.gz

#2.解压代码包
[root@web01 service]# tar xf apache-tomcat-8.5.51.tar.gz

#3.配置java环境
1.上传并解压至指定文件夹
[root@web01 service]# tar xf jdk-8u40-linux-x64.gz -C /service/
[root@web01 service]# mv jdk1.8.0_40 java1.8

2.修改添加环境变量
[root@web01 service]# vim /etc/profile.d/java.sh
export JAVA_HOME=/service/java1.8
export JRE_HOME=/service/java1.8/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$PATH:$JAVA_HOME/bin
[root@web01 service]# source /etc/profile

#4.配置tomcat页面
[root@web01 service]# echo "linux_tomcat" > apache-tomcat-8.5.51/webapps/ROOT/index.html

#5.启动tomcat,启动的时候最好看着日志
[root@web01 service]# /service/apache-tomcat-8.5.51/bin/startup.sh && tail -f /service/apache-tomcat-8.5.51/logs/catalina.out

三、系统优化

  • 查看文件句柄数

#1.查看文件句柄数设置
[root@lb01 ~]# ulimit -n
65535

#2.查看打开的文件句柄数
[root@lb01 ~]# lsof | wc -l
2945
[root@web01 ~]# lsof | wc -l
12490

#3.查看指定服务的打开文件句柄数
[root@web01 ~]# lsof -p 13592 | wc -l
  • 设置文件句柄数

系统全局的设置

[root@web01 ~]# vim /etc/security/limits.conf
* - nofile 65535
* soft nofile 65535
* hard nofile 65535

*		#所有用户
-		#当超过设置的文件句柄数时,什么都不干
soft	#当超过设置的文件句柄数时,仅提示
hard	#当超过设置的文件句柄数时,直接限制

用户局部设置

[root@web01 ~]# vim /etc/security/limits.conf
root - nofile 65535
root soft nofile 65535
root hard nofile 65535

针对服务设置

[root@lb01 ~]# vim /etc/nginx/nginx.conf 
user  www;
worker_processes  1;
worker_rlimit_nofile 65535;

系统通用优化

[root@lb01 ~]# vim /etc/sysctl.conf
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
net.ipv4.ip_forward = 1

四、代理服务优化

  • 配置用户访问负载均衡的长连接

[root@lb01 ~]# vim /etc/nginx/nginx.conf
... ...
http {
	... ...
	keepalive_timeout  65;
	... ...
}
  • 配置负载均衡代理到web的长连接

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.keep.com.conf
upstream tomcat {
    server 172.16.1.7:8080;
    keepalive 8;				#配置开启长连接
}

server {
    listen 80;
    server_name linux.keep.com;

    location / {
        proxy_pass http://tomcat;
        proxy_http_version 1.1;			 #代理带后端的http版本
        proxy_set_header Connection "";	 #清除请求头字段
        include proxy_params;
    }
}
  • 配置web代理到php保持长连接

[root@web01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf 
upstream php_server {
    server 127.0.0.1:9000;
}

server {
    listen 80;
    server_name linux.wp.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass php_server;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        fastcgi_param HTTPS on;
        fastcgi_keep_conn on;
        include fastcgi_params;
    }
}
  • 代理优化配置

[root@lb01 ~]# cat /etc/nginx/proxy_params 
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
proxy_next_upstream http_500 http_502 http_503 http_504;

五、静态资源优化

  • 静态资源
类型后缀
图片文件gif、png、jpg、jpeg
视频文件mp4、avi、rmvb
其他文件txt、xml、pdf
样式文件css、js、html
  • 静态资源缓存

#响应头部
cache-control: max-age=315360000
expires: Wed, 09 Nov 2050 02:40:58 GMT
last-modified: Sat, 18 Apr 2020 15:23:09 GMT
ETag: "5fd6c1d9-7cda"

#请求头部
If-None-Match: "5fd6c1d9-7cda"
If-Modified-Since: Sat, 18 Apr 2020 15:23:09 GMT

浏览器缓存原理

1.浏览器先去查看响应头部的 cache-control
2.如果没有超过缓存时间,直接返回浏览器缓存的内容
3.如果 cache-control 设置为 no-cache,浏览器会继续去读取 expires
4.如果没有到达 expires 设置的时间,那么浏览器会读取缓存
5.如果 cache-control 和 expires 都没有设置
6.浏览器会去查看服务器上面的 ETag
7.服务器上如果有 ETag,那么浏览器会拿着 If-None-Match 与其对比,如果值相同,那么走缓存
8.如果 ETag 与 If-None-Match 不同,浏览器会读取服务器上的 last-modified
9.服务器上如果有 last-modified,那么浏览器会拿着 If-Modified-Since 与其对比,如果值相同,那么走缓存
10.如果 last-modified 与 If-Modified-Since 不同,则不走缓存,需要重新到服务器上获取数据并返回

浏览器缓存参数含义

1.cache-control:    # 缓存控制,记录的是文件的保留时长
2.expires:          # 文件缓存到期时间
3.ETag:             # 服务器上保留的文件的唯一识别符
4.last-modified:    # 服务器上保留的文件的最后修改时间

5.If-None-Match:    # 浏览器上上保留的文件的唯一识别符
6.If-Modified-Since:# 浏览器上保留的文件的最后修改时间

配置缓存过期时间

#语法
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
-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/

配置不走缓存

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;
    }
}
  • 静态资源读取

文件高效读取

Syntax:	sendfile on | off;
Default:	sendfile off;
Context:	http, server, location, if in location

文件高效传输

#将多个数据打个包,一次推送,大文件适合此配置,需要开启 sendfile
Syntax:	tcp_nopush on | off;
Default:	tcp_nopush off;
Context:	http, server, location

长连接

Syntax:	keepalive_timeout timeout [header_timeout];
Default:	keepalive_timeout 75s;
Context:	http, server, location

长连接传输

#来一条数据传输一条数据,需要开启 keepalive
Syntax:	tcp_nodelay on | off;
Default:	tcp_nodelay on;
Context:	http, server, location
  • 静态资源压缩

静态资源压缩配置语法

# 开启压缩
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

压缩配置

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

上传文件

[root@web01 cache]# ll -h
-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
<html>
	<body>
		<img src="http://linux.beidaolian.com/404.jpg">
	</body>
</html>

配置hosts访问测试

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 
<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>
  • 配置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
  • 配置允许被跨域

[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亲和

  • 查看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
  • 修改nginx配置
[root@web01 ~]# vim /etc/nginx/nginx.conf 
worker_processes  4;

[root@web01 ~]# systemctl restart nginx
  • 没有配置亲和的情况
[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
  • 配置cpu亲和

方式1

worker_processes    4;
worker_cpu_affinity 0001 0010 0100 1000;

worker_processes    16;
worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 

方式2

worker_processes    2;
worker_cpu_affinity 0101 1010;

方式3

worker_processes auto;
worker_cpu_affinity auto;
  • 配置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通用优化文件

  • 通用优化配置

[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;				#包含的配置文件
}
  • 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 prox
y_cache、fastcgi_cache、uwsgi_cache 代理缓存,第三方工具(squid、varnish)

十、PHP优化

  • 配置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();
  • php.ini配置文件优化

#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#; Error handling and logging ;
#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
expose_php = Off                        # 关闭php版本信息
display_error = Off                     # 屏幕不显示错误日志(开发环境可以开启 on)
error_reporting = E_ALL                 # 记录PHP的每个错误
log_errors = On                         # 开启错误日志
error_log = /var/log/php_error.log      # 错误日志写入的位置(程序处理代码时的错误)
date.timezone = Asia/Shanghai           # 调整时区,默认PRC

#;;;;;;;;;;;;;;;;
#; File Uploads ;
#;;;;;;;;;;;;;;;;
file_uploads = On           # 允许文件上传
upload_max_filesize = 300M  # 允许上传文件的最大大小
post_max_size = 300M        # 允许客户端单个POST请求发送的最大数据
max_file_uploads = 20       # 允许同时上传的文件的最大数量
memory_limit = 128M         # 每个脚本执行最大内存
  • PHP危险函数

有时候为了安全我们需要禁掉一些PHP危险函数,整理如下需要的朋友可以参考下 
phpinfo() 
功能描述:输出 PHP 环境信息以及相关的模块、WEB 环境等信息。 
危险等级:中

passthru() 
功能描述:允许执行一个外部程序并回显输出,类似于 exec()。 
危险等级:高

exec() 
功能描述:允许执行一个外部程序(如 UNIX Shell 或 CMD 命令等)。 
危险等级:高

system() 
功能描述:允许执行一个外部程序并回显输出,类似于 passthru()。 
危险等级:高

chroot() 
功能描述:可改变当前 PHP 进程的工作根目录,仅当系统支持 CLI 模式 
PHP 时才能工作,且该函数不适用于 Windows 系统。 
危险等级:高

scandir() 
功能描述:列出指定路径中的文件和目录。 
危险等级:中

chgrp() 
功能描述:改变文件或目录所属的用户组。 
危险等级:高

chown() 
功能描述:改变文件或目录的所有者。 
危险等级:高

shell_exec() 
功能描述:通过 Shell 执行命令,并将执行结果作为字符串返回。 
危险等级:高

proc_open() 
功能描述:执行一个命令并打开文件指针用于读取以及写入。 
危险等级:高

proc_get_status() 
功能描述:获取使用 proc_open() 所打开进程的信息。 
危险等级:高

error_log() 
功能描述:将错误信息发送到指定位置(文件)。 
安全备注:在某些版本的 PHP 中,可使用 error_log() 绕过 PHP safe mode, 
执行任意命令。 
危险等级:低

ini_alter() 
功能描述:是 ini_set() 函数的一个别名函数,功能与 ini_set() 相同。 
具体参见 ini_set()。 
危险等级:高

ini_set() 
功能描述:可用于修改、设置 PHP 环境配置参数。 
危险等级:高

ini_restore() 
功能描述:可用于恢复 PHP 环境配置参数到其初始值。 
危险等级:高

dl() 
功能描述:在 PHP 进行运行过程当中(而非启动时)加载一个 PHP 外部模块。 
危险等级:高

pfsockopen() 
功能描述:建立一个 Internet 或 UNIX 域的 socket 持久连接。 
危险等级:高

syslog() 
功能描述:可调用 UNIX 系统的系统层 syslog() 函数。 
危险等级:中

readlink() 
功能描述:返回符号连接指向的目标文件内容。 
危险等级:中

symlink() 
功能描述:在 UNIX 系统中建立一个符号链接。 
危险等级:高

popen() 
功能描述:可通过 popen() 的参数传递一条命令,并对 popen() 所打开的文件进行执行。 
危险等级:高

stream_socket_server() 
功能描述:建立一个 Internet 或 UNIX 服务器连接。 
危险等级:中

putenv() 
功能描述:用于在 PHP 运行时改变系统字符集环境。在低于 5.2.6 版本的 PHP 中,可利用该函数 
修改系统字符集环境后,利用 sendmail 指令发送特殊参数执行系统 SHELL 命令。 
危险等级:高

#禁用方法如下: 
打开/etc/php.ini文件, 
查找到 disable_functions ,添加需禁用的函数名,如下: 
phpinfo,eval,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,p
  • php-fpm服务配置

php-fpm.conf配置优化

[root@web02 ~]# vim /etc/php-fpm.conf
[global]
;pid = /var/log/php-fpm/php-fpm.pid     	#pid文件存放的位置
;error_log = /var/log/php-fpm/php-fpm.log   #错误日志存放的位置(启动时的日志)
;log_level = error                  	    #日志级别, alert, error, warning, notice, debug
rlimit_files = 65535                	    #php-fpm进程能打开的文件句柄数
;events.mechanism = epoll           	    #使用epoll事件模型处理请求
include=/etc/php-fpm.d/*.conf

包含配置文件优化 /etc/php-fpm.d/*.conf

[root@web02 ~]# vim /etc/php-fpm.d/www.conf
[www]       				    #池名称
user = www  				    #进程运行的用户
group = www 				    #进程运行的组
;listen = /dev/shm/php-fpm.sock #监听在本地socket文件
listen = 127.0.0.1:9000         #监听在本地tcp的9000端口
;listen.allowed_clients = 127.0.0.1 #允许访问FastCGI进程的IP,any不限制 
pm = dynamic                    #管理方式(dynamic为动态,static为静态)
pm.max_children = 512           #最大启动的php-fpm进程数(静态管理,配置dynamic时失效)
pm.start_servers = 32           #动态方式下的起始php-fpm进程数量。
pm.min_spare_servers = 32       #动态方式下的最小php-fpm进程数量。
pm.max_spare_servers = 64       #动态方式下的最大php-fpm进程数量。
pm.max_requests = 1500          #达到这个请求数,子进程会重启,如果是0那就一直接受请求
pm.process_idle_timeout = 15s;  #没有请求时多久释放一个进程
pm.status_path = /php_status    #开启php的状态页面

php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/phpfpm_error.log
php_admin_flag[log_errors] = on
request_slowlog_timeout = 5s    #php脚本执行超过5s的文件
slowlog = /var/log/php_slow.log #记录至该文件中
  • php状态页

配置php

[root@web02 ~]# vim /etc/php-fpm.d/www.conf
pm.status_path = /php_status 		#开启php的状态页面

[root@web02 ~]# systemctl restart php-fpm

配置nginx

[root@web02 ~]# cat /etc/nginx/conf.d/php.conf
server {
	listen 80;
	server_name linux.php.com;
	root /code/php;
	index index.php;
	location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
	}
	location /status {
		stub_status;
	}
	location /php_status {
		fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
	}	
}

访问页面

#访问  http://linux.php.com/php_status
pool:                 www							#池名称
process manager:      dynamic						#动态管理
start time:           14/Sep/2020:18:52:12 +0800	#启动时间
start since:          14							#启动了多久
accepted conn:        1								#连接数
listen queue:         0								#等待队列
max listen queue:     0								#最大等待队列
listen queue len:     511							#等待队列长度
idle processes:       4								#空闲的进程数
active processes:     1								#活跃的进程数
total processes:      5								#总的进程数
max active processes: 1								#最大的活跃进程数
max children reached: 0								#进程最大的限制连接数
slow requests:        0								#慢查询
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值