Nginx服务基础到架构优化详解--25条

一、隐藏nginx header版本号javascript

二、更改源码隐藏软件名称php

三、更改nginx默认用户及用户组css

四、配置nginx worker进程个数html

五、根据CPU核数进行nginx进程优化前端

六、nginx事件处理模型优化java

七、调整Nginx worker单个进程容许的客户端最大链接数node

八、配置Nginx worker进程最大打开文件数nginx

九、开启高效的文件传输模式web

十、设置链接超时时间数据库

十一、上传文件大小设置(动态应用)

十二、fastcgi调优(配合PHP引擎动态服务)

1三、配置nginx gzip压缩功能

1四、配置Nginx expires缓存功能

1五、Nginx日志相关优化与安全

1六、Nginx站点目录及文件URL访问控制(防止恶意解析)

1七、防止恶意解析访问企业网站

1八、Nginx图片及目录防盗链

1九、Nginx错误页面的优雅显示

20、Nginx防爬虫优化

21 、限制HTTP请求方法

2二、防DOS攻击

2三、使用CDN为网站内容加速

2四、Nginx程序架构优化

2五、使用普通用户启动Nginx(监牢模式)

一、隐藏nginx header版本号

查看版本号

1

2

3

4

5

6

7

8

[root@db02 ~]# curl -I http://www.lichengbing.cn 

HTTP/1.1 200 OK

Server: nginx/1.6.3

Date: Tue, 16 Aug 2016 14:39:48 GMT

Content-Type: text/html; charset=UTF-8

Connection: keep-alive

X-Powered-By: PHP/5.5.32

Link: <http://www.lichengbing.cn/wp-json/>; rel="https://api.w.org/"

编译nginx.conf配置文件,添加server_tokens off参数

1

2

3

4

5

6

7

8

http {

   ...

    server_tokens off; #控制http response header内的服务版本信息的显示,以及错误信息中web服务版本信息

   ...

}

[root@db02 ~]# curl -I http://www.lichengbing.cn

HTTP/1.1 200 OK

Server: nginx #版本隐藏

 

二、更改源码隐藏软件名称

修改3个nginx源码文件

第一个nginx-1.6.3/src/core/nginx.h文件

1

2

3

4

5

6

[root@lichengbing nginx-1.6.3]# cd ~/tools/nginx-1.6.3

[root@lichengbing nginx-1.6.3]# sed -n '13,17p' src/core/nginx.h 

#define NGINX_VERSION      "1.6.3" #改为你想要的版本号,如2.2.5

#define NGINX_VER          "Nginx/" NGINX_VERSION #你想改为的软件名称,如Apache

#define NGINX_VAR          "NGINX" #能够改为OWS等

#define NGX_OLDPID_EXT     ".oldbin"

第二个

1

[root@lichengbing nginx-1.6.3]# sed -i 's#Server: nginx#Server: OWS#g' src/http/ngx_http_header_filter_module.c

第三个ngx_http_special_response.c是否对外展现敏感信息

1

2

3

4

5

6

7

8

9

[root@lichengbing nginx-1.6.3]# sed -n '21,30p' src/http/ngx_http_special_response.c 

static u_char ngx_http_error_full_tail[] =

"<hr><center>" NGINX_VER "</center>" CRLF

"</body>" CRLF

"</html>" CRLF

;

static u_char ngx_http_error_tail[] =

"<hr><center>nginx</center>" CRLF

"</body>" CRLF

修改成以下

1

2

3

4

5

6

7

8

9

[root@lichengbing nginx-1.6.3]# sed -n '21,30p' src/http/ngx_http_special_response.c 

static u_char ngx_http_error_full_tail[] =

"<hr><center>" NGINX_VER " (http://www.lichengbing.cn)</center>" CRLF

"</body>" CRLF

"</html>" CRLF

;

static u_char ngx_http_error_tail[] =

"<hr><center>OWS</center>" CRLF

"</body>" CRLF

从新编译安装nginx,重启服务

 

三、更改nginx默认用户及用户组

1

2

3

4

5

6

7

8

9

10

[root@lichengbing ~]# grep '#user' /application/nginx/conf/nginx.conf.default 

#user  nobody; #系统默认的用户为nobody

[root@lichengbing ~]# useradd www -s /sbin/nologin -M #创建用户www

user  www www; #nginx.conf 中配置用户

也能够在编译时加入用户和用户组,如

--user=www --group=www --with-http_ssl_module --with-http_stub_status_module --prefix=/application/nginx-1.6.3/

[root@lichengbing ~]# ps -ef|grep nginx|grep -v grep

root      1386     1  0 Jul21 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx

www      24732  1386  0 23:19 ?        00:00:00 nginx: worker process

#此时worker process进程就是使用的www用户权限,固然也可使master降权运行

 

四、配置nginx worker进程个数

1

2

3

4

5

6

7

worker_processes  8; #最好为服务器CPU的逻辑核心数

[root@netmonitor ~]# grep "physical id" /proc/cpuinfo|sort|uniq|wc -l #物理核数

2

[root@netmonitor ~]# grep "cpu cores" /proc/cpuinfo|uniq #单核心数

cpu cores         : 4

[root@netmonitor ~]# grep "processor" /proc/cpuinfo|wc -l #逻辑核心数

8

 

五、根据cpu核数进行nginx进程优化

亲和力参数(Nginx服务可能会发生只在同一颗CPU上起做用的状况)

1

2

3

worker_processes  8;

worker_cpu_affinity 001 0010 0100 1000; #数字表明一、二、三、4的掩码,平均分摊进程压力

worker_cpu_affinity 00000001 00000010...#8核心写法

防止进程只在一个核心上运行也可使用 taskset 命令

1

taskset -c 1,2,3 /application/nginx/sbin/nginx start

 

六、nginx事件处理模型优化

nginx的链接处理机制在不一样的操做系统中会采用不一样的I/O模型:Linux上使用epoll、BSD上面用kqueue、Solaris中使用/dev/poll、windows中使用icop.

1

2

3

events {

    use epoll; #nginx官方建议,能够不指定事件处理模型,Nginx会自动选择最佳的事件处理模型

}

 

七、调整Nginx worker单个进程容许的客户端最大链接数

1

2

3

4

5

6

7

worker_processes  8;

events {

    worker_connections  1024;

}

#最大链接数Max_client = worker_processes * worker_connections

#进程的最大链接数收Linux系统进程的最大打开文件数限制,在执行操做系统命令“ulimit -HSn 65535”后,才能生效

#链接数并非越大越好,要在系统性能能接受的范围内

 

八、配置Nginx worker进程最大打开文件数

1

2

3

events {

    worker_rlimit_nofile 65535; #该值可设置为系统优化后的ulimit -HSn的结果

}

 

九、开启高效的文件传输模式

1

2

3

4

http {

    sendfile        on; #开启文件的高效传输模式

}

#同时将tcp_nopush和tcp_nodelay两个指令设置为on,减小网络传输包数量、防止网络及磁盘的I/O阻塞,提高Nginx效率

 

十、设置链接超时时间

1

2

3

4

5

6

7

8

http {

    keepalive_timeout  65; #设置客户端链接保持会话的超时时间

    tcp_nodelay on; #激活tcp_nodelay,提升I/O性能,在包含keepalive参数时才有效

    client_header_timeout 15; #读取客户端请求头数据超时时间,超时则返回“Request time out(408)”

    clietn_body_timeout 15; #读取客户端请求主体超时时间

    send_timeout 15; #指定客户端响应时间

}

#将无用的链接尽快设置为超时,能够保护服务器的系统资源(CPU、内存、磁盘)

 

十一、上传文件大小设置(动态应用)

1

2

3

http {

 client_max_body_size 10m; #具体数字要根据业务需求决定

}

 

十二、fastcgi调优(配合PHP引擎动态服务)

fastcgi做为静态服务和动态服务之间的传接口,也有cache和buffer缓存和缓冲区

1

2

3

4

5

6

7

8

9

10

http {

    fastcgi_connect_timeout 240; #链接到后端fastcgi的超时时间

    fastcgi_send_timeout 240; #已经和fastcgi创建链接后多久不传送数据,就会被断开

    fastcgi_read_timeout 240; #接收fastcgi应答的超时时间

    fastcgi_buffer_size 64k; #指定读取fastcgi应答第一部分须要多大的缓冲区,能够设置为gastcgi_buffers选项指定的缓冲区大小

    fastcgi_busy_buffers_size 128k; #繁忙时的buffer,能够是fastcgi_buffer的两倍

    fastcgi_temp_path /data/ngx_fcgi_tmp; 在写入fastcgi_temp_path时将用多大的数据库,默认是fastcgi_buffer的两倍,若是过小,可能会报502 Bad GateWay

    fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_s

ize=40g; #指定相关参数配比

}

在server标签中配合设置相关参数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

server {

    location ~ .*\.(php|php5)?$ {

            root html/www;

            fastcgi_pass        127.0.0.1:9000;

            fastcgi_index       index.php;

            include fastcgi.conf;

            fastcgi_cache ngx_fcgi_cache;#表示开启缓存并为其指定一个名称,开启缓存很是有用,能够有效下降cpu的负载,而且防止502的发生,可是也可能会带来其余问题

            fastcgi_cache_valid 200 302 1h; #将对应的应答码缓存时间

            fastcgi_cache_valid 301 1d; #将301缓存1天

            fastcgi_cache_valid any 1m; #将其余应答缓存1分钟

            fastcgi_cache_min_uses 1; #请求数量

            fastcgi_cache_use_stale error timeout invalid_header http_500; #错误判断

        }

    }

 

1三、配置nginx gzip压缩功能

###(重要!!!)几k字节的压缩比在大数据量的冲击下也能够节省很多的流量,网站内容传输速度更快,为用户带来更好的浏览体验

Nginx gzip压缩模块提供了压缩文件内容的功能,用户请求的内容在发送到用户客户端以前,Nginx服务器会根据一些具体的策略实施压缩,到客户端后由浏览器解压

Nginx 依赖ngx_http_gzip_module模块

Apache 使用mod_deflate压缩功能

1

2

3

4

5

6

7

8

9

http {

    gzip on; #开启压缩功能

    gzip_min_length 1k; #容许压缩的最小页面字节数,从header头的Content-Length中获取,无论页面多大都进行压缩,建议设置成大于1K,若是小于1K可能会越压缩越大

    gzip_http_version 1.1; #压缩版本,默认为1.1,目前大部分浏览器都支持压缩

     gzip_buffers  4 32k; #压缩缓冲大小,容许申请4个单位为32K的内存做为流缓存

    gzip_comp_level 9; #压缩比例,1最小,9最大,传输速度最快,可是比较消耗资源

    gzip_types text/css text/xml application/javascript; #指定压缩的内容类型

    gzip_vary on; #vary header支持,让前端的缓存服务器继续缓存传输该压缩页面,而不提早解压

}

 

1四、配置Nginx expires缓存功能

(重要!!!)

在网站的开发和运营过程当中,视频、图片、CSS、JS等网站元素的更改机会较少,咱们能够将这些更改频率较少的内容缓存在用户本地,用户在第二次访问网站时就不用继续去服务器下载了,节省流量,加快访问速度

1

2

3

4

5

6

7

8

9

10

server {

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #指定缓存文件的类型

        {

        expires      3650d; #指定缓存时间

        }

        location ~ .*\.(js|css)?$

        {

        expires      3d;

        }

}

wKioL1kZgKuzCM6VAADIsRIqnA0851.png-wh_50 

expires缓存的缺点就是在网站更新相关数据后,用户若是不清理缓存看到的就会一直是过时的数据,为了解决这个问题,能够一、缩短缓存时间,好比百度的首页图片缓存时间为一天;二、服务后台更改图片名称,这样就至关因而一个新的页面内容,用户会从新下载 三、相关的CSS、JS推送到CDN

 

1五、Nginx日志相关优化与安全

1)天天进行日志切割,备份

1

2

3

4

5

#!/bin/sh

cd /application/nginx/logs/

mv www_access.log www.access_$(date +%F -d -1day).log

mv blog_access.log blog.access_$(date +%F -d -1day).log

/application/nginx/sbin/nginx -s reload

 

1

2

3

cat >>/var/spool/cron/root<<EOF

00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

EOF

2)不记录不须要的访问日志

对于健康检查或者某些图片、JS、CSS日志,通常不须要记录日志,由于在统计PV时是按照页面计算的,并且写入频繁会消耗IO,下降服务器性能

1

2

3

location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {

        access_log off;

        }

3)访问日志的权限设置

1

2

chown -R root.root /application/nginx/logs

chmod -R 700 /application/logs

 

1六、Nginx站点目录及文件URL访问控制(防止恶意解析)

1)根据扩展名限制程序或者文件被访问

资源文件夹如用户上传的头像,防止恶意上传脚本病毒文件被解析执行

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

server {

location ~ ^/images/.*\.(php|php5|sh|pl|py)$ { #指定目录限制访问

            deny all;

        }

        location ~ ^/static/.*\.(php|php5|sh|pl|py)$ {

            deny all;

        }

        location ~ ^/data/.*\.(php|php5|sh|pl|py)$ {

            deny all;

        }

        location ~ .*\.(php|php5)?$ { #必须配置在解析以前

            root html/www;

            fastcgi_pass        127.0.0.1:9000;

            fastcgi_index       index.php;

            include fastcgi.conf;

        }

}

2)禁止访问目录并返回指定HTTP代码

1

2

3

server {

    location /admin/ return 404; }

}

3)限制网站来源IP

1

2

3

4

5

6

7

server {

location ~ ^/admin/ {

            allow 202.111.12.211;

           #allow 192.168.1.0/24; #也能够限制IP段

            deny all;

        }

}

企业问题案列:Nginx作方向代理的时候能够限制客户端IP吗?

方法一:用if来控制

1

2

3

4

5

6

if ( $remotea_addr = 10.0.0.110 ) {

  return 403;

}

if ( $remotea_addr = 10.0.0.111 ) {

  set $allow_access_root 'true';

}

方法二:利用deny和allow

1

2

3

4

5

6

location / {

  root html/blog;

  index index.php index.html index.html;

  deny 10.0.0.7;

  allow all;

}

 

1七、防止恶意解析访问企业网站

方法一

1

2

3

4

5

server {

  listen 80 default_server;

  server_name _;

  return 501;

}

方法二

1

2

3

4

5

server {

  listen 80 default_server;

  server_name _;

  rewrite ^(.*) http://www.lichengbing.cn$1 permanent;

}

方法三

1

2

3

4

server {

 if ($host !~ ^www/.lichengbing/.com$) {

  rewrite ^(.*) http://www.lichengbing.cn$1 permanent;

}

 

1八、Nginx图片及目录防盗链

网站图片被盗链最直接的影响就是网络带宽占用加大了,宽带费用变高了,网络流量忽高忽低,Zabbix频繁告警

因为购买了CDN加速,流量高了好几个G,瞬间损失好几万...

利用referer防盗链

1

2

3

4

5

6

7

location ~.* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {

    valid_referers none blocked *.lichengbing.cn lichengbing.cn;

 if ($invalid_referer) {

    rewrite ^/ http://www.lichengbing.cn/img/nolink.jpg

   }

}

#或者也可使用NginxHttpAccessKeyModule实现防盗链

 

1九、Nginx错误页面的优雅显示

1

2

3

server {

  error_page 403   /403.html; #当出现403错误时,会跳转到403.html页面

}

 

20、Nginx防爬虫优化

robots.txt机器人协议

网络爬虫排除标准,告诉搜索引擎哪些目录能够抓取,哪些禁止抓取

禁止下载协议代理

1

2

3

if ($http_user_agent ~* LWP::Simple|BBBike|wget) {

    return 403;

}

防止N多爬虫代理访问网站

1

2

3

4

if ($http_user_agent ~*

  “qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot”) {

   return 403;

}

禁止不一样浏览器访问

1

2

3

4

if ($http_user_agent ~* “Firefox|MSIE”)

{

rewrite ^(.*) http://blog.etiantian.org/$1 permanent

}

 

21 、限制HTTP请求方法

1

2

3

if ($request_method !~ ^(GET|HEAD|POST)$ ) {

   return 501;

}

只容许GET等,容许DELETE、SEARCH等

为防止黑客经过上传服务器执行木马,也能够在上传服务器上作限制HTTP的GET

1

2

3

if ($request_method ~* ^(GET)$ ) {

  return 501;

}

 

2二、防DOS攻击

使用limit_conn_zone进行控制,控制单个IP或域名的访问次数,限制连续访问

1

2

3

4

5

6

7

limit_conn_zone $binary_remote_addr zone=perip:10m;

limit_conn_zone $server_remote_addr zone=perserver:10m;

server {

limit_conn perip 10;

limit_conn perserver 100;

}

#还可使用limit_req_zone进行控制,控制单个IP的访问速率

 

2三、使用CDN为网站内容加速

全国或全球的内容分布式缓存集群,其实质经过智能DNS判断用户的来源地域及上网线路,为用户选择一个最接近用户地域,以及和用户上网线路相同的服务器节点,提高用户浏览网站的体验

要加速的业务数据应该存在独立的域名,而后删除A记录解析,使用CNAME解析

 

2四、Nginx程序架构优化

解耦,一堆程序代码按照业务用途分开,而后提供服务,例如:注册登陆、上传、下载、浏览列表、商品内容、订单支付等都应该是独立的程序服务,只不过在客户端看来是一个总体而已,小公司最起码要作到的解耦是

01网页页面服务

02图片附件及下载服务

03上传图片服务

 

2五、使用普通用户启动Nginx(监牢模式)

降权思想,Nginx的Master进程使用的是root用户,worker进程使用的是nginx指定的普通用户,用root跑nginx的Master进程有两大问题:1是最小化权限分配遇到问题;二、网站一旦有漏洞,很容易丢掉root权限

降权执行的好处:

一、建立普通用户inca,用inca跑Nginx服务,开发运维都使用普通账号,只要和inca同组,照样能够管理nginx,还解决了root权限太大问题

二、职责分明,相关帐号负责维护程序和日志,出问题负首要责任

相关文章
相关标签/搜索