综合架构之服务优化(后篇)

2019-08-28  本文已影响0人  一只打不死的小强

主要内容

1.nginx优化
2.php优化
3.安全优化


nginx服务优化

1.nginx配置文件移动,该如何启动nginx

mv /app/nginx/conf/nginx.conf  /opt #配置文件移动
nginx -c /opt/nginx.conf #将服务进行启动,但会提示缺少资源类型文件。
mv /app/nginx/conf/mime.types /opt
nginx -c /opt/nginx.conf #重新启动

2.优化nginx打开文件数

原因:当用户访问nginx时,会打开许多临时文件。如果超过这个数量服务会出现报错。
实现:将打开数变为65535

注:系统的系统文件打开数为1024 ulimit -n

3.优化nginx超时信息

4.优化 nginx 服务上传文件限制

client_max_body_size 设置客户端请求报文主体最大尺寸

为什么是body? 首先回顾下http协议

由http协议可知,当用户上传内容时,发送的是http报文中的请求主体,所以为body.

[root@zabbix ~]# curl -v www.baidu.com 
* About to connect() to www.baidu.com port 80 (#0)
*   Trying 182.61.200.6...
* Connected to www.baidu.com (182.61.200.6) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Wed, 28 Aug 2019 11:21:35 GMT
< Etag: "588604c8-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
[root@zabbix ~]# wget --debug www.baidu.com 
DEBUG output created by Wget 1.14 on linux-gnu.

URI encoding = ‘UTF-8’
Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8)
Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8)
--2019-08-28 19:23:05--  http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 182.61.200.7, 182.61.200.6
Caching www.baidu.com => 182.61.200.7 182.61.200.6 #DNS解析
Connecting to www.baidu.com (www.baidu.com)|182.61.200.7|:80... connected. #TCP的三次握手
Created socket 3.
Releasing 0x00000000009109a0 (new refcount 1).

---request begin---
GET / HTTP/1.1
User-Agent: Wget/1.14 (linux-gnu)
Accept: */*
Host: www.baidu.com
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response... 
---response begin---
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Content-Length: 2381
Content-Type: text/html
Date: Wed, 28 Aug 2019 11:23:12 GMT
Etag: "588604c8-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

---response end---
200 OK
cdm: 1 2 3 4 5 6 7 8
Stored cookie baidu.com -1 (ANY) / <permanent> <insecure> [expiry 2019-08-29 19:23:05] BDORZ 27315
Registered socket 3 for persistent reuse.
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’

100%[==============================================================================================>] 2,381       --.-K/s   in 0s      

2019-08-28 19:23:05 (185 MB/s) - ‘index.html’ saved [2381/2381]

5.优化nginx服务与FastCGI连接缓存和缓冲的信息

查看实际的内存的大小是看available剩余的大小。或者free+buffer cache
linux系统中将使用过的文件,存放在buffer和cache中,加速下次使用。

[root@zabbix ~]# free -h 
              total        used        free      shared  buff/cache   available
Mem:     972M     92M        760M        7.6M        118M        735M
image

6.配置 Nginx gzip 压缩实现性能优 化

补充:gzip命令的使用
gzip + 文件名 压缩
gzip -d + 压缩文件名称 解压

gzip on;    
 gzip_min_length 1k;   #设置大于 1K 才进行压缩     
gzip_buffers 4 16k;    #设置压缩缓存     
#gzip_http_version 1.0;    
 gzip_comp_level 2;    #压缩级别 数字越大 压缩率(占用空间)越小 占用 CPU 越多      
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php  ;   #哪些类型的文件 需要进行压缩   这些类型需用 mime type 媒体类型. #mime types              #媒体类型(http)  === 文件类型(linux) 

注意:不要加上html,会出现报错。

故障案例: NSES WITH THE “TEXT/HTML” TYPE ARE ALWAYS COMPRESSED. GZIP 默认压缩 TEXT/HTML 类型,不用指定,指定会报错。
浏览器设置

谷歌浏览器设置
火狐浏览器设置

7. 配置 Nginx expires 实现让客户 端缓存数据

~ #匹配正则 区分大小写 perl 语言正则表达式
~* #匹配正则 不区分大小写 perl 语言正则表达式

location ~* \.(gif|jpg|jpeg|png|bmp|ico)$ { 
                          root /var/www/img/; 
           expires 30d;       
 }

浏览器保存图片的时间为30天
响应码为304 访问的是缓存的信息


image

8.Nginx 图片及目录防盗链解决方案

if ( $http_referer !~ "www.oldboyedu.com") {
return 403; }

image

钥匙的作用是存储用户的信息,session也是存放用户的信息,两者需要通过钥匙建立连接。

9.Nginx防爬虫优化

这样会将所有的爬虫都禁止了,也可以针对于agent详细的写。

10.Nginx 网站 CDN 加速优化


cdn的主要厂商:蓝汛 网宿 阿里云 CDN 百度云 CDN 七牛


PHP优化

tmpfs是一个基于内存文件系统 (放一些缓存,速度较快 )

mount -t tmpfs -o size=16m tmpfs /cache   #创建基于 内存 的分区
[root@web01 ~]# dd if=/dev/zero of=/cache/600  bs=1M count=600 500+0 records in 500+0 records out 524288000 bytes (524 MB) copied, 0.189981 s, 2.8 GB/s 

dd if(input file) of(output file) bs(block size ) count(次数)

安装插件
需要指定源 https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装模块 pecl install redis
编辑php.ini extension=redis.so

- 关闭危险函数       
 disable_functions = system,passthru,exec,shell_exec,popen,phpinfo       3
38 行 设置为 safe_mode = On   #开启安全模式    
435 行 设置为 expose_php = Off #关闭版本信息 
- 关闭不必要的错误提示 打开错误日志   
538 行 设置为 display_errors = Off           #错误信 息控制,测试的时候开启     #报错的级别在521行 默认为 error_reporting = E_ALL & ~E_DEPRECATED
559 行 设置为 log_errors = On #打开log 日志   
643 行 设置为 error_log = /app/logs/php_errors.log   #log日志得路径(需log_errors 为 On 才能生效)
703 行 设置为 register_globals = Off        #关闭全局 变量(默认即为关闭,万万不能开启)    
756 行 设置为 magic_quotes_gpc = On   #防止SQL注入 asdfsadf&&&&"""""select  * from  mysql.user;
902 行 设置为 allow_url_fopen = Off   #打开远程打开 (禁止)    
854 行 设置为 cgi.fix_pathinfo=0         #防止Nginx 文件类型错误解析漏洞 
php性能优化    
444 max_execution_time = 30  #一个脚本可使用多少CPU秒    454 max_input_time = 60          #一个脚本等待输入数 据的时间有多长(秒)    
465 memory_limit = 128M        #单个脚本大使用内存, 单位为K或M(128M稍大可以适当调小)    
891 upload_max_filesize = 2M  #上传文件大许可        output_buffering            #数据发送给客户机之 前,有多少数据(字节)需要缓存    
894 max_file_uploads = 20         #可以通过单个请求上 载的大文件数

系统安全优化

网站安全基础配置

f644 d755  root root 
上传         /app/blog/uploads 
f644 d755  www  www
挂载参数 nodev nosuid noexec   
如果扩展名不是.zip或.jpg 提示403
如果扩展名是.zip或.jpg 提示403
location       #匹配请求里面的uri 
location / 
location = / 
location ~ /   
location ~* /  #不区分大小写正则 
location  /uploads  {
    if ( $request_uri !~ \.(zip|jpg)$  ) {
     return 403;    
    }
}   
find /etc/  -type f -name "*.conf"  |xargs md5sum  >police.txt 
find /站点目录  -type f -name "*.conf"  |xargs md5sum  >police.txt #用户上传的文件和缓存目录文件 排除
md5sum -c police.txt  #定时任务

md5有时候可能会破解,字符串一样生成的校验码也一样
现在已经使用sha系列加密。

   clamav -ri   /data
上一篇 下一篇

猜你喜欢

热点阅读