企业级web Nginx服务优化

    0.隐藏nginx header内版本号信息

一些特定的系统及服务漏洞一般都和特定的软件及版本号有关,我们应尽量隐藏服务器的敏感信息(软件名称及版本等信息),这样黑客无法猜到有漏洞的服务是否是对应服务的版本,从而确保web服务器最大的安全。

利用curl查看隐藏前header内的web版本号信息

[root@nginx ~]# curl -I 192.168.80.104

HTTP/1.1 200 OK

Server: nginx/1.6.3

Date: Tue, 24 May 2016 11:46:12 GMT

Content-Type: text/html

Content-Length: 5

Last-Modified: Fri, 29 Apr 2016 13:10:16 GMT

Connection: keep-alive

ETag: “57235d38-5”

Accept-Ranges: bytes

浏览器访问web服务报错信息

 

以上两个访问不但暴漏了nginx软件名称,而且暴漏了nginx特定的版本号,这样就会给服务的安全带来一定的风险,应禁止掉。

修改配置文件参数实现隐藏版本号

在nginx配置文件nginx.conf的http标签中加入”server_tokens off;”

官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html

Syntax: server_tokens on | off | string;

Default:

server_tokens on;

Context: http, server, location

Enables or disables emitting nginx version in error messages and in the “Server” response header field.

  1. 更改默认用户及用户组

nginx服务启动,使用的默认用户是nobody

[root@nginx www]# grep “#user” /application/nginx/conf/nginx.conf.default

#user  nobody;

为了防止黑客猜到这个用户,我们需要更改下特殊的用户名,提供nginx服务用

更改默认用户的方法有两种,

第一种为:

user  nginx nginx                         #配置文件中修改

设置Nginx worker进程运行的用户以及用户组,如果注释或不设置,默认即是nobody用户和组,不推荐使用nobody用户名称,最好采用一个普通用户,如nginx。注意Nginx的主进程还是以root身份运行的,后文也会有不用root进程起服务的配置。

建立nginx用户的操作过程如下:

useradd -s /sbin/nologin -M nginx

第二种为:

useradd -s /sbin/nologin -M nginx            #先添加一个用户

./configure –prefix=/application/nginx-1.6.3 –user=nginx –group=nginx –with-http_ssl_module –with-http_stub_status_module              #编译nginx时指定用户

ps -ef|grep nginx|grep -v grep                  #检查nginx进程的对应用户

  1. 配置nginx worker进程个数

在高并发场景,我们需要事先启动更多的nginx进程以保证快速响应并处理用户的请求。具体的配置参数如下:

worker_processes  1;

指定了nginx要开启的进程数。建议指定和CPU的数量相等或乘2的进程数。

worker_processes参数开始的设置可以等于CPU的个数或核数(worker_cpu_affinity参数中的配置可以指定第一个到最后一个进程分别使用的哪个cpu),进程数多一些,起始提供服务时就不会临时启动新进程提供服务,减少了系统开销,提升了服务速度。特殊场合也可以考虑提高至CPU*2的进程数,具体情况要根据实际的业务来选择,因为这个参数,除了CPU核数的影响外,和硬盘存储的数据以及负载也有关。

查看linux服务器的核数的方法

例如:CPU个(核)数为4,就配置worker_processes  4;

[root@nginx conf]# grep “physical id” /proc/cpuinfo

physical id     : 0

这里我们修改参数值为4,然后重新加载nginx服务,操作过程及结果

[root@nginx conf]# grep worker_processes nginx.conf

worker_processes  4;

[root@nginx conf]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

[root@nginx conf]# /application/nginx/sbin/nginx -s reload

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

root      2144     1  0 19:53 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx

nginx     2494  2144  0 21:55 ?        00:00:00 nginx: worker process

nginx     2495  2144  0 21:55 ?        00:00:00 nginx: worker process

nginx     2496  2144  0 21:55 ?        00:00:00 nginx: worker process

nginx     2497  2144  0 21:55 ?        00:00:00 nginx: worker process

官网文档:http://nginx.org/en/docs/ngx_core_module.html

Syntax: worker_processes number | auto;

Default: worker_processes 1;

Context: main

Defines the number of worker processes.

The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard disk drives that store data, and load pattern. When one is in doubt, setting it to the number of available CPU cores would be a good start (the value “auto” will try to autodetect it).

  1. 根据cpu核数进行nginx进程优化

默认情况nginx的多个进程可能更多的跑在一颗CPU上,本节是分配不同的进程给不同的CPU处理,达到充分利用硬件多核多CPU的目的.

不同的CPU对应配置如下

四核cpu服务器:

worker_cpu_affinity 0001 0010 0100 1000;

#nginx进程CPU亲和力,即把不同的进程分给不同的CPU处理。这里0001 0010 0100 1000是掩码,分别代表第1、2、3、4颗cpu核心。

八核cpu服务器:

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;

官方文档http://nginx.org/en/docs/ngx_core_module.html

Syntax: worker_cpu_affinity cpumask …;

worker_cpu_affinity auto [cpumask];

Default: —

Context: main

Binds worker processes to the sets of CPUs. Each CPU set is represented by a bitmask of allowed CPUs. There should be a separate set defined for each of the worker processes. By default, worker processes are not bound to any specific CPUs.

For example,

worker_processes    4;

worker_cpu_affinity 0001 0010 0100 1000;

binds each worker process to a separate CPU, while

 

worker_processes    2;

worker_cpu_affinity 0101 1010;

binds the first worker process to CPU0/CPU2, and the second worker process to CPU1/CPU3. The second example is suitable for hyper-threading.

测试:

webbench -c 20000 -t 180 http://192.168.80.104     #测试过程这里不再叙述,也可以用ab

通过观察,我们发现配置后不同CPU使用率相对平均,和测试前变化不大。因此就认为比较平均,一方面是软件自身再逐渐的优化使用多核CPU,另一方面测试的数有待调整。

另外(taskset – retrieve or set a process’s CPU affinity)命令本身也有分配CPU的功能

taskset -c 1,2,3 /etc/init.d/mysql start

  1. 事件处理模型优化

nginx的连接处理机制在于不同的操作系统采用不同的10模型,在linux使用epoll的IO多路复用模型,在freebsd使用kqueue的IO多路复用模型,在solaris使用/dev/Poll方式的IO多路复用模型,在windows使用的是icop等等。

根据系统类型不同选择不同 use [kqueue|rtsig|epoll|/dev/poll|select|poll];该参数结合系统使用,不同系统使用参数不同,我们使用的是Centos6.5,因此我们调整为epoll模型。

events {

worker_connections  1024;

use epoll;

}

官方文档http://nginx.org/en/docs/events.html

Syntax: use method;

Default: —

Context: events

Specifies the connection processing method to use. There is normally no need to specify it explicitly, because nginx will by default use the most efficient method.

 

The following connection processing methods are supported:

select — standard method. The supporting module is built automatically on platforms that lack more efficient methods. The –with-select_module and –without-select_module configuration parameters can be used to forcibly enable or disable the build of this module.

 

poll — standard method. The supporting module is built automatically on platforms that lack more efficient methods. The –with-poll_module and –without-poll_module configuration parameters can be used to forcibly enable or disable the build of this module.

 

kqueue — efficient method used on FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0, and Mac OS X.

 

epoll — efficient method used on Linux 2.6+.

 

Some older distributions like SuSE 8.2 provide patches that add epoll support to 2.4 kernels.

/dev/poll — efficient method used on Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+, and Tru64 UNIX 5.1A+.

 

eventport — event ports, efficient method used on Solaris 10.

  1. 调整单个进程允许的客户端最大连接数

这个值根据具体服务器性能和程序的内存使用量来指定(一个进程启动使用的内存根据程序确定)

events {

worker_connections  1024;

use epoll;

}

worker_connections也是个事件模块指令,用于定义Nginx每个进程的最大连接数,默认是1024.最大客户端连接数由worker_processes和worker_connections决定,即Max_client=worker_connections*worker_processes。进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -HSn 65535”或配置相应文件后worker_connections的设置才能生效。

官方文档:http://nginx.org/en/docs/ngx_core_module.html#worker_connections

Syntax: worker_connections number;

Default:

worker_connections 512;

Context: events

Sets the maximum number of simultaneous connections that can be opened by a worker process.

 

It should be kept in mind that this number includes all connections (e.g. connections with proxied servers, among others), not only connections with clients. Another consideration is that the actual number of simultaneous connections cannot exceed the current limit on the maximum number of open files, which can be changed by worker_rlimit_nofile.

  1. 配置每个进程最大文件打开数

worker_rlimit_nofile 32768;

每个进程打开的最大文件数,可设里为系统优化后的ulimit -HSn的结果,在第一章系统安装时,调整文件描述符和这个处理的一个问题。

官方文档:http://nginx.org/en/docs/ngx_core_module.html#worker_rlimit_nofil

Syntax: worker_rlimit_nofile number;

Default: —

Context: main

Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes. Used to increase the limit without restarting the main process.

  1. 优化服务器名字的hash表大小

确切名字和通配符名字存储在哈希表中。哈希表和监听端口关联,每个端口都最多关联到三张表:确切名字的哈希表,以星号起始的通配符名字的哈希表和以星号结束的通酝符名字的哈希表。哈希表的尺寸在配置阶段进行了优化,可以以最小的CPU缓存命中失败来找到名字。nginx首先搜素确切名字的哈希表,如果没有找到,搜索以星号起始的通配符名字的哈希表,如果还是没有找到,继续搜索以星号结束的通配符名字的哈希表。因为名字是按照域名的节来搜索的,所以搜索通配符名字的哈希表比搜索确切名字的哈希表慢。注意.nginx.org存储在通配符名字的哈希表中,而不在确切名字的哈希表中。正则表达式是一个一个串行的测试,所以是最慢的,而且不可扩展。

鉴于以上原因,请尽可能使用确切的名字。举个例子,如果使用nginx.org和www.nginx.org来访问服务器是最频繁的,那么将它们明确的定义出来就更为有效:

server {

listen       80;

server_name  www.etiantian.org etiantian.org *.etiantian.org ;

….

}

下面这种方法相比更简单,但是效率也更低

server {

listen       80;

server_name  .etiantian.org ;

….

}

如果定义了大量名字,或者定义了非常长的名字,那就需要在http配置块中调整server_names_hash_max_size and server_names_hash_bucket_size的值。server_names_hash_bucket_size的默认值可能是32、64或其他值,取决于缓存行的长度。如果这个值是32,那么定义“too.long.server.name.example.org”作为虚拟主机名就会失败,显示下面错误信息

could not build the server_names_hash,

you should increase either server_names_hash_max_size: 512

or server_names_hash_bucket_size: 32

可以在http标签中添加如下一行

server_names_hash_bucket_size 64;

官方文档http://nginx.org/en/docs/http/ngx_http_core_module.html#server_names_hash_bucket_size

Syntax: server_names_hash_bucket_size size;

Default:

server_names_hash_bucket_size 32|64|128;

Context: http

Sets the bucket size for the server names hash tables. The default value depends on the size of the processor’s cache line. The details of setting up hash tables are provided in a separate document.

 

Syntax: server_names_hash_max_size size;

Default:

server_names_hash_max_size 512;

Context: http

Sets the maximum size of the server names hash tables. The details of setting up hash tables are provided in a separate document.

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

sendfile       on;

tcp_nopush     on;

sendfile参数用于开启文件高效传输模式。同时将tcp_nopush和tcp_nodelay两个指令设为on用于防止网络阻塞

官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#sendfile

Syntax: sendfile on | off;

Default:

sendfile off;

Context: http, server, location, if in location

Enables or disables the use of sendfile().

 

Starting from nginx 0.8.12 and FreeBSD 5.2.1, aio can be used to pre-load data for sendfile():

  1. 设置连接超时时间

keepalive_timeout  65;

#设置客户端连接保持会话的超时时间。超过这个时间,服务器会关闭该连接

tcp_nodelay    on;

打开tcp_nodelay,在包含了keepalive参数才有效

client_header_timeout 15;

#设置客户端请求头读取超时时间.如超过这个时间,客户端还没有发送任何数据,Nginx将返回“Request timeout(408)"错误,默认值是60。

client_body_timeout 15;

#设置客户端请求主体读取超时时间。如超过这个时间,客户端还没有发送任何数据,Nginx将返回“Request timeout(408)错误,默认值是60。

send_timeout 15;

#指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。

官网文档:http://nginx.org/en/docs/http/ngx_http_core_module.html

Syntax: client_header_timeout time;

Default:

client_header_timeout 60s;

Context: http, server

  1. 上传文件大小限制(动态应用)

主配置文件里加入如下参数,具体大小根据你自己的业务做调整.

client_max_body_size  10m;

官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

Syntax: client_max_body_size size;

Default: client_max_body_size 1m;

Context: http, server, location

Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size.

  1. fastcgi调优(配合PHP引擎动态服务)

官网文档:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html

fastcgi_connect_timeout 300;

#指定连接到后端fastCGI的超时时间

Syntax: fastcgi_connect_timeout time;

Default: fastcgi_connect_timeout 60s;

Context: http, server, location

fastcgi_send_timeout 300;

#向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间

Syntax: fastcgi_send_timeout time;

Default: fastcgi_send_timeout 60s;

Context: http, server, location

fastcgi_read_timeout 300;

#指定接收FastcGI应答的超时时间,这个值是指己经完成两次握手后接收FastCGI应答的超时时间。

Syntax: fastcgi_read_timeout time;

Default: fastcgi_read_timeout 60s;

Context: http, server, location

fastcgi_buffer_size 64k;

#指定读取FastCGI应答第一部分需要用多大的缓冲区,这个值表示将使用1个64KB的缓冲区读取应答的第一部分(应答头),可以置为fastcgi_buffers选项指定的缓冲区大小。

Syntax: fastcgi_buffer_size size;

Default: fastcgi_buffer_size 4k|8k;

Context: http, server, location

fastcgi_buffers 4 64k;

#指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256KB,为其分配4个64KB的缓冲区来缓存;如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于硬盘。一般这个值应该为站点中PHP脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为”16 16k”、”16 16k”

Syntax: fastcgi_buffers number size;

Default: fastcgi_buffers 8 4k|8k;

Context: http, server, location

fastcgi_busy_buffers_size 128k;

#建议为fastcgi_buffers的两倍

fastcgi_temp_file_write_size 128k;

#在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍,设置上述数值设置太小时若负载上来时可能报502 Bad Gateway

fastcgi_cache oldboy_nginx;

#表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502错误的发生,但是开启缓存也可能会引起其它问题,要根据具体情况选择。

fastcgi_cache_valid 200 302 1h;

#用来指定应答代码的缓存时间,实例中的值表示将200和302应答缓存一个小时.

fastcgi_cache_valid 301      1d;

fastcgi_cache_valid any      1m;

fastcgi_cache_min_uses 1;

#缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数

以上参数集合

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

fastcgi_cache oldboy_nginx;

fastcgi_cache_valid 200 302 1h;

fastcgi_cache_valid 301      1d;

fastcgi_cache_valid any      1m;

fastcgi_cache_min_uses 1;

  1. conf配置参数

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

root   /var/html/bbs;

fastcgi_pass  127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

include proxy.conf;

}

proxy.conf配置文件参数

vi /application/nginx/conf/proxy.conf

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 50m;

client_body_buffer_size 256k;

proxy_connect_timeout 30;

proxy_send_timeout 30;

proxy_read_timeout 60;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

proxy_max_temp_file_size 128m;

proxy_store on;

proxy_store_access user:rw group:rw all:r;

#nginx cache

#client_body_temp_path /data/nginx_cache/client_body 1 2;

#proxy_temp_path /usr/local/nginx/proxy_temp 1 2;

  1. 更改源码隐藏软件名称及版本号

修改header信息

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

[root@nginx nginx-1.6.3]# vi src/http/ngx_http_header_filter_module.c +48

static char ngx_http_server_string[] = “Server: nginx” CRLF;

static char ngx_http_server_full_string[] = “Server: ” NGINX_VER CRLF;

修改上面两行的黑体为下面红色字体,然后编译安装

static char ngx_http_server_string[] = “BWS” CRLF;

static char ngx_http_server_full_string[] = “BWS” CRLF;

然后修改nginx.conf配置文件

server_tokens off;

修改403错误页信息

cd /server/tools/nginx-1.6.3

[root@nginx nginx-1.6.3]# vi src/http/ngx_http_special_response.c +29

“<hr><center>nginx</center>” CRLF

修改为下面一行

“<hr><center>BWS(www.etiantian.org)</center>” CRLF

然后重新编译安装

  1. 配置nginx gzip压缩功能

nginx gzip压缩模块提供了对文件内容压缩的功能,允许nginx服务器将输出内容在发送到客户端之前根据具体的策略进行压缩,以节约网站带宽,同时提升用户访问体验。此功能同apache的mod_deflate压缩功能,依赖ngx_http_gzip_module模块,默认己安装,我们已经详细讲解过了压缩的功能。

要压缩的内容(js,css,html),不要压缩的内容(图片,视频,FLASH)

官方文档:http://nginx.org/en/docs/http/ngx_http_gzip_module.html

gzip on;                     #开启压缩功能

Syntax: gzip on | off;

Default: gzip off;

Context: http, server, location, if in location

gzip_min_length 1k;

#设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,不管页面多大都进行压缩。建议设旦成大于1K。如果小于lK可能会越压越大。

Syntax: gzip_min_length length;

Default: gzip_min_length 20;

Context: http, server, location

gzip_buffers 4 16k;

#压缩缓冲区大小。表示申请4个单位为16K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果

Syntax: gzip_buffers number size;

Default: gzip_buffers 32 4k|16 8k;

Context: http, server, location

gzip_http_version 1.0;

#压缩版本(默认1. l,前端为squid2.5时使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器己经支持GZIP解压,使用默认即可。

Syntax: gzip_http_version 1.0 | 1.1;

Default: gzip_http_version 1.1;

Context: http, server, location

gzip_comp_level 2;

#压缩比率;用来指定GZIP压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理最慢,也比较消耗CPU资源。

Syntax: gzip_comp_level level;

Default: gzip_comp_level 1;

Context: http, server, location

gzip_types text/plain application/javascript test/css text/xml;

#用来指定压缩的类型,”text/html”类型.总是会被压缩。

提示:gzip_types类型不同的版本可能不同,可以查看:cat /application//nginx/conf/mime.types

Syntax: gzip_types mime-type …;

Default: gzip_types text/html;

Context: http, server, location

gzip_vary on;

#vary header支持。该选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用Squid缓存经过Nginx压缩的数据。(让前端的缓存不解压缩发送给客户端)

Syntax: gzip_vary on | off;

Default: gzip_vary off;

Context: http, server, location

完整的配置如下

gzip on;

gzip_min_length 1k;

gzip_buffers 4 32k;

gzip_http_version 1.1;

gzip_comp_level 9;

gzip_types text/plain application/javascript test/css text/xml;

gzip_vary on;

  • 需要压缩的对象

大于1K的纯文本文件html、js、css、xml、html。

图片视频等不要压缩。因为不但不会减小,在压缩时消耗CPU,MEM资源。

可以通过火狐的yslow插件查看是否压缩

  1. 配置nginx expires功能

在网站开发和运营中,对于图片,css,js等元素更改机会较少,特别是图片,这时可以将图片设置在浏览器本地缓存365天或更长,CSS,JS,html等代码缓存10天,这样用户第一次打开页面后,会在本地的浏览器缓存相应的上述内容,这样的缓存可以提高下次用户打开类该页面的加载速度,并节省服务器端大量的带宽。此功能同apache的expires,我们己经详细讲解过了。这里通过location的功能,将需要缓存的扩展名列出来,然后指定缓存时间。

expires功能优点

Expires可以降低网站购买的带宽,节约成本,同时提升了用户访问体验,减轻服务器的压力,是web服务非常重要的功能

expire功能缺点

被缓存的页面或数据更新了,用户看到的可能还是旧的内容,反而影响用户体验

解决办法:

第一个 缩短缓存时间,例如:1天,不彻底,除非更新频率大于1天

第二个 对缓存的对象改名。图片,附件一般不会被用户修改,如果用户修改了,实际上也都是更改文件名里新传了而己。网站升级对于js,css元素,一般可以改名。把js,css推送到CDN

一般不希望被缓存的内容

1)广告图片

2)网站流量统计文件

3)更新频繁的文件

语法: expires [time|epoch|max|off]

默认值: expires off

作用域: http, server, location

例如:控制图片等过期时间为30天,当然这个时间可以设置的更长。具体视情况而定

location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {

expires 30d;

}

比如控制匹配/resource/或者/mediatorModule/里所有的文件缓存设置到最长时间

location ~ /(resource|mediatorModule)/ {

root    /opt/demo;

expires max;

}

  1. Nginx防蜘蛛爬虫处理

假定一个场景:某个网站它可能不希望被网络爬虫抓取,例如测试环境不希望被抓取,以免对用户造成误导,那么需要在该网站中申明,本站不希望被抓取。有如下方法:

方法一:修改nginx.conf,禁止网络爬虫的ua,返回403。

server {

listen 80;

server_name 127.0.0.1;

#添加如下内容即可防止爬虫

if ($http_user_agent ~* “qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot”)

{

return 403;

}

方法2:网站更目录下增加Robots.txt,放在站点根目录下。

在http://tool.chinaz.com/robots/站点可以针对现在的搜索引擎按照想要的规则生成robots.txt文件。

robots.txt是搜索引擎中访问网站的时候要查看的第一个文件。robots.txt文件告诉蜘蛛程序在服务器上什么文件是可以被查看的。

当一个搜索蜘蛛访问一个站点时,它会首先检查该站点根目录下是否存在robots.txt,如果存在,搜索机器人就会按照该文件中的内容来确定访问的范围;如果该文件不存在,所有的搜索蜘蛛将能够访问网站上所有没有被口令保护的页面。百度官方建议,仅当您的网站包含不希望被搜索引擎收录的内容时,才需要使用robots.txt文件。如果您希望搜索引擎收录网站上所有内容,请勿建立robots.txt文件。

Robots协议是国际互联网界通行的道德规范,基于以下原则建立

1、搜索技术应服务于人类,同时尊重信息提供者的意愿,并维护其隐私权;

2、网站有义务保护其使用者的个人信息和隐私不被侵犯。

当然,如果搜索引擎不遵守约定的Robots协议,那么通过在网站下增加robots.txt也是不起作用的。(在正式环境中,可以适当允许搜索引擎抓取收录)

  1. nginx日志相关优化与安全

编写脚本实现Nginx access日志轮询

  • 自动切割访问日志脚本

#!/bin/bash

Dataformat=`date +%F -d -1day`

Basedir=”/application/nginx”

Nginxlogdir=”$Basedir/logs”

Logname=”access”

[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1

[ -f ${Logname}.log ] || exit 1

/bin/mv ${Logname}.log ${Dataformat}_${Logname}.log

$Basedir/sbin/nginx -s reload

添加定时任务实现每天定时切割

00 00 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null

日志相关工具:syslog,rsyslog,Awstats,flume,logstash,scribe,kafka,storm

  • 不记录不需要的访问日志

对于健康检查或某些(图片、css、js)的日志,一般不需要记录,因为在统计PV时是按照页面计算。而且日志写入频繁会消耗磁盘IO,降低服务性能。

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

access_log off;

}

  • 访问日志的权限设置

假如日志目录为/app/logs,则授权方法为

chown -R root.root /app/logs

chmod -R 700 /app/logs

不需要在日志目录上给nginx用户读或者写许可,这个问题很多网友都没在意,直接给nginx或apache用户。因为apache或nginx的主进程都是以root用户运行的。

  1. 最小化nginx目录及文件权限设置

为了保证apache的网站不遭受木马入侵上传及修改文件

安全的权限:

  • 所有站点目录的用户和组都应该为root
  • 所有目录权限是默认的755
  • 所有文件权限是默认的644

以上的权限设置可以做到防止黑客上传木马,以及修改站点文件,但是,合理的用户上传的内容也被拒之门外了,那么如何解决可以让合法的用户传文件又不至于被黑客利用攻击呢?

这就是对业务进行分离,在比较好的网站业务架构中,应把资源文件,包括用户上传的图片,附件等的服务和程序服务分离,最好把上传程序服务也分离,这样就可以从容按照前面安全的标准授权了。

  1. nqinx站点目录及文件URL访问控制

根据扩展名限制程序和文件访问

location ~  ^/images/.*\.(php|php5)${

deny all;

}

location ~  ^/(static|js)/ {

deny all;

}

location ~  ^/(static|js)/ {

return 403;

}

location ~  ^/(static|js)/ {

allow 202.110.21.144;

allow 192.168.80.0/24;

deny all;

}

#http://nginx.org/en/docs/http/ngx_http_access_module.html

  1. nginx错误页面及优雅显示

server

{

listen       80;

server_name  www.XXX.com ;

index index.html index.htm index.php;

root  /opt/www/;

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

{

#fastcgi_pass  unix:/tmp/php-cgi.sock;

fastcgi_pass  127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

error_page  404 = /404.html;

创建自己的404.html页面,放在站点目录下面

error_page   500 502 503 504 = /50x.html;

location = /50x.html {

root   /var/html;

}

门户网站nginx优雅显示配置案例

error_page 400 http://err.tmall.com/error1.html

error_page 403 http://err.tmall.com/error2.html

  1. 使用tmpfs文件系统替代频繁访问的目录

mkdir /opt/tmp

mv /tmp/* /opt/tmp

mount -t tmpfs -o size=16m tmpfs /tmp     #工作中一般给2-4G

echo “mount -t tmpfs -o size=16m tmpfs /tmp” >>/etc/rc.local

vi /etc/fstab              #或者在fstab文件中挂载

tmpfs         /tmp       tmpfs      size=2048m    0 0

  1. 使用普通用户启动nginx

本优化属架构优化(同样适合其他软件),通过nginx的-c参数指定不同的配置文件,以起多个实例使用。如果使用普通用户,则不能使用80端口,

/application/nginx/sbin/nginx -h        #查看帮助

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇