基础网站优化之nginx优化(部分)

版权声明:from 瑾川(fakehydra.xyz) https://blog.csdn.net/fake_hydra/article/details/82980625

优化的作用

 让服务更好的运行

LNMP架构优化

Linux优化

 ssh远程优化
 字符集优化
 内核优化
nginx优化
 3-5万并发(静态访问:nginx可以处理的请求)
PHP优化
 3-5千并发
MySQL优化
 1千并发

nginx优化操作实践

安全优化

01.隐藏nginx版本号信息

 [root@web01 ~]# curl -I -H host:www.jinc.org 10.0.0.7/index.html
 Server: nginx/1.14.0
利用server_tokens
 在配置文件中添加:server_tokens off
 默认是on 我们用off是关闭显示版本号信息
 [root@web01 nginx]# vim conf/nginx.conf
 worker_processes  1;
 events {
     worker_connections  1024;
 }
 http {
     include       mime.types;
     default_type  application/octet-stream;
     sendfile        on;
     server_tokens   off;
结果显示
 [root@web01 nginx]# nginx -s reload
 [root@web01 nginx]# curl -I -H host:www.jinc.org 10.0.0.7/index.html
 HTTP/1.1 404 Not Found
 Server: nginx

02.修改nginx软件名称

 修改三个源码文件
文件一:nginx-x.x.x/src/core/nginx.h
  14 #define NGINX_VER          "JC/" NGINX_VERSION
  22 #define NGINX_VAR          "JC"
文件二:nginx-x.x.x/src/http/ngx_http_header_filter_module.c
  49 static u_char ngx_http_server_string[] = "Server: JC" CRLF;
文件三:nginx-x.x.x/src/http/ngx_http_special_response.c
  22 "<hr><center>" NGINX_VER "([email protected])</center>" CRLF
  36 "<hr><center>JC</center>" CRLF

重新编译安装

注意事项

 1)备份好配置文件&站点目录数据信息
 2)按照原有编译安装过程再来一遍(nginx -V)

03.修改nginx进程用户信息(worker进程)

第一种方法:通过编译安装指定worker进程用户信息
 --user=www --group=www
第二种方法:修改配置文件 /application//nginx/conf/nginx.conf
 首行添加 user nginx
 [root@web01 nginx]# head -1 conf/nginx.conf
 user nginx;
 [root@web01 nginx]# ps -ef |grep nginx
 root       5694      1  0 18:13 ?        00:00:00 nginx: master process nginx
 nginx      5721   5694  0 18:43 ?        00:00:00 nginx: worker process

04.配置Nginx,禁止非法域名解析访问企业网站

 server {
    listen 80;
    server_name -;
    return 501;
 }

05.实现防盗链

01. 利用reffer实现防盗链
 location ~* .*\.(jpg|gif|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
      valid_referers none blocked *.etiantian.org etiantian.org;
     if ($invalid_referer) {
      rewrite ^/  http://www.etiantian.org/daolian.png;
     }
    }
PS:可以添加缓存expire配置, 让禁止盗链的图片在用户本地缓存
02. 根据cookie防盗链
03. 通过加密变换访问路径实现防盗链

在这里插入图片描述

04. 给图片信息加上水印

nginx 防爬虫优化

 01.利用robots协议
 02.修改nginx配置,user_agent
 03开发的角度进行防止

性能优化

01.优化worker进程数量信息

 vim nginx.conf
 worker_processes 1;
 PS:worker进程数量主要参照CPU核数信息
 worker_processes 数量==CPU核数
 worker_processes 数量==CPU核数*2
CPU核数查看:
 [root@web01 nginx]# grep processor /proc/cpuinfo |wc -l
 1
 
 也可以使用top命令 按数字键1
CPU颗数信息查看
 [root@web01 nginx]# grep 'physical id' /proc/cpuinfo |sort|wc -l
 1

02.优化worker进程连接数量能力

 vim nginx.conf
 worker_connections 1024;(2的倍数为妙)
 PS:系统最大打开文件数>=worker_connections*worker_processes
 [root@web01 nginx]# ulimit -a|grep open
 open files                      (-n) 65535

03.优化nginx服务CPU亲和力

 服务器: CPU01(繁忙) CPU02 CPU03 CPU04  

 案例一: 有4个nginx进程  服务器有4颗CPU(进程数和颗数相同)
 wker_processes    4;
 worker_cpu_affinity 0001 0010 0100 1000;

 案例二: 有多个nginx进程 服务器有4颗CPU(进程数大于颗数信息)
 wker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
 worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;

 案例三: 
 wker_processes    4;
 worker_cpu_affinity 0101 1010;

04.优化nginx使用的epoll模型信息

 use epoll;

05.开启高效文件传输模式

 sendfile    on;  特殊的数据传输功能:零拷贝
 tcp_nopush  on;  将数据积攒到一定量再发送
 tcp_nodelay off; 经数据信息进行快速传输 
 
 数据包==1500字节==货车=1500件  1600字节 1500 100 == 1400  1500
    01. 先别着急发送, 确保数据包已经装满数据, 避免了网络拥塞
    	tcp_nopush on;
    02. 有时要抓紧发货, 确保数据尽快发送, 提高可数据传输效率	
     	tcp_nodelay on;	
    说明: 以上两个参数选择其一使用

06.优化nginx服务超时信息

 1)keepalive_timeout  60;
 确保通讯双方在一定时间内没有传输数据了,断开连接
 2)client_header_timeout  15;
 发送方发出请求信息,但接受方迟迟不作出响应
 3) client_body_timeout  15;
 发送方放出主体信息,但接受方没有正常处理
 4) send_timeout  ;
 服务端等待客户端两次请求的间隔时间

07.设置nginx服务允许用户最大上传数据大小

    client_max_body_size 2m;
    PS: 默认大小为1m

08.nginx与php之间fsstcgi优化信息

时间超时设定

 fastcgi_connect_timeout 240;
 fastcgi_send_timeout 240;
 fastcgi_read_timeout 240;

缓冲设置

http区块中设置
 fastcgi_buffer_size 64k;
 fastcgi_buffers 4 64k;
 fastcgi_busy_buffers_size 128k;
 fastcgi_temp_file_write_size 128k;
 #fastcgi_temp_path /data/ngx_fcgi_tmp;
 fastcgi_cache_path /data/ngx_fcgi_cache levels=1:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g;
server区块中设置
  server {
      listen       80;
      server_name  blog.jinc.org;
      root   html/blog;
      index  index.php index.html index.htm;
      client_max_body_size 2m;
      location ~* .*\.(php|php5)?$ {
        root html/blog;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_cache ngx_fcgi_cache;
        fastcgi_cache_valid 200 302 1h;
        fastcgi_cache_valid 301 1d;
        fastcgi_cache_valid any 1m;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_use_stale error timeout invalid_header http_500;
        fastcgi_cache_key http://$host$request_uri;
        include fastcgi.conf;
    }
  }

09.压缩传输的数据信息

 gzip on;
 gzip_min_length             1k;
 gzip_buffers                4 16k;
 gzip_http_version           1.1;
 gzip_comp_level             7;
 gzip_types                  text/css text/xml application/javascripts;
 gzip_vary                   on;

在这里插入图片描述

10.让用户尽可能多的缓存网站数据信息

运维思路: 当网站架构负载压力过大的时候, 尽量将压力向前推
 location ~ .*\.(|jpg|jpeg|png|bmp|swf)$
    {
        expires      3650d;
    }

猜你喜欢

转载自blog.csdn.net/fake_hydra/article/details/82980625
今日推荐