web网站集群之企业级Nginx Web服务优化详解

1. 隐藏nginx版本信息优化(安全优化)

官方参考链接:http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens

Syntax:  server_tokens on | off | build | string;

Default: server_tokens on;(默认显示nginx服务版本)

Context: http, server, location

实践配置:

server {

listen       80;

server_name  www.etiantian.org;

server_tokens off;

root   html/www;

index  index.html index.htm;

}

[[email protected] conf]# curl -I www.etiantian.org

HTTP/1.1 200 OK

Server: nginx      <-- 版本号信息已经隐藏

Date: Thu, 08 Mar 2018 06:15:18 GMT

Content-Type: text/html

Content-Length: 19

Last-Modified: Mon, 05 Feb 2018 00:58:31 GMT

Connection: keep-alive

ETag: "5a77ac37-13"

Accept-Ranges: bytes


2 修改nginx服务名称信息优化(安全优化)

可以通过修改程序源代码,实现修改nginx服务名称

第一个文件: /server/tools/nginx-1.12.2/src/core/nginx.h

13行#define NGINX_VERSION      "6.6.6"            #<==已修改为想要显示的版本号

14行 #define NGINX_VER      “oldboy/" NGINX_VERSION    #<==已修改为想要显示的名字

22行 #define NGINX_VAR          ”oldboy"             #<==已修改为想要显示的名字

将以上源码文件中三行内容进行修改

第二个文件:/server/tools/nginx-1.12.2/src/http/ngx_http_header_filter_module.c

49行 static u_char ngx_http_server_string[] = "Server: oldboy" CRLF;   #<=== 将nginx名称改为想要显示的名字

第三个文件:nginx1.xxx/src/http/ngx_http_special_response.c

改动之前配置信息

21 static u_char ngx_http_error_full_tail[] =

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

23 "</body>" CRLF

24 "</html>" CRLF

25 ;

改动之后配置信息

21 static u_char ngx_http_error_full_tail[] =

22 "<hr><center>" NGINX_VER "(http://oldboy.blog.51cto.com)</center>" CRLF

23 "</body>" CRLF

24 "</html>" CRLF

25 ;

改动之前配置信息:

35 static u_char ngx_http_error_tail[] =

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

37 "</body>" CRLF

38 "</html>" CRLF

39 ;

改动之后配置信息:

35 static u_char ngx_http_error_tail[] =

36 "<hr><center>oldboy</center>" CRLF

37 "</body>" CRLF

38 "</html>" CRLF


3. 修改nginx软件worker_processes进程用户信息(安全优化)

第一种方法:

编译安装软件时,指定配置参数

--user=www --group=www

第二种方法:

修改服务配置文件,实现修改worker进程用户

官方链接说明:http://nginx.org/en/docs/ngx_core_module.html#user

Syntax:user user [group];

Default:user nobody nobody;

Context:main

实践配置:

user oldboy oldboy;

worker_processes  1;

error_log  /tmp/error.log error;

[[email protected] nginx-1.12.2]# ps -ef|grep nginx

root      47630      1  0 14:48 ?        00:00:00 nginx: master process nginx

oldboy    47713  47630  0 15:01 ?        00:00:00 nginx: worker process


4 修改nginx软件worker_processes进程数量(性能优化)

官方参考链接:http://nginx.org/en/docs/ngx_core_module.html#worker_processes

Syntax:  worker_processes number | auto;

Default: worker_processes 1;

Context: main

worker_processes进程数据建议:(一般和CPU的核数设置一致;高并发可以和CPU核数2倍)

1)建议数量和你的服务器CPU核数一致

a. grep processor /proc/cpuinfo|wc -l

b. 如何通过top命令获取cpu核数:按键盘数字1获取到

2) 建议数量和你的服务器cpu核数两倍一致


5 优化nginx服务进程均匀分配到不同CPU进行处理(性能优化)

官方参考链接:http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity

Syntax: worker_cpu_affinity cpumask ...;

worker_cpu_affinity auto [cpumask];

Default: —

Context: main

4核4路CPU配置方法

worker_processes    4;

worker_cpu_affinity 0001 0010 0100 1000;

8核4路CPU配置方法:

worker_processes    8;

worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;

worker_processes    8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

4核心2路CPU配置方法:

worker_processes    4;

worker_cpu_affinity 0001 0010 0100 1000;

worker_processes    4;

worker_cpu_affinity 0101 1010;


6 优化nginx事件处理模型(性能优化)

官方参考链接:http://nginx.org/en/docs/ngx_core_module.html#use

Syntax: use method;

Default: —

Context: events

实践优化配置:

events {

worker_connections  1024;

use epoll;

}


7 调整Nginx单个进程允许的客户端最大连接数(性能优化)

官方参考链接:http://nginx.org/en/docs/ngx_core_module.html#worker_connections

Syntax:worker_connections number;

Default:worker_connections 512;

Context:events

注意事项:

worker_connections*worker_processes <= 系统的最大打开文件数

#加大文件描述

echo '*               -       nofile          65535 ' >>/etc/security/limits.conf


8 配置Nginx worker进程最大打开文件数(性能优化)

官方参考链接:http://nginx.org/en/docs/ngx_core_module.html#worker_rlimit_nofile

Syntax:worker_rlimit_core size;

Default:—

Context:main

实践配置:

worker_rlimit_nofile 65535;

#<==最大打开文件数,可设置为系统优化后的ulimit -HSn的结果,在第3章中,调整系统文件描述符和这个问题有相同之处


9 优化nginx服务数据高效传输模式(性能优化)

官方链接参考: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

sendfile on      开启底层高效传输数据模式

官方链接参考:http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nopush

Syntax:tcp_nopush on | off;

Default:tcp_nopush off;

Context:http, server, location

tcp_nopush on    对快递员有利(自身服务器有利)

让数据不着急传输

网络中传输数据的车==数据包 1500  3100k  1500 1500  100 1400

官方链接参考:http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nodelay

Syntax:tcp_nodelay on | off;

Default:tcp_nodelay on;

Context:http, server, location

tcp_nodelay on 对用户感知更好

尽快将数据传输出去

实践配置:

http {

include       mime.types;

default_type  application/octet-stream;

sendfile        on;

tcp_nopush      on;

tcp_nodelay     off;


10 优化Nginx连接参数,调整连接超时时间 (安全优化)

keepalive_timeout       <-- 表示传输双方在指定时间内,没有数据传输就超时断开连接

官方链接参考:https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout

Syntax: keepalive_timeout timeout [header_timeout];

Default: keepalive_timeout 75s;

Context: http, server, location

client_header_timeout <-- 表示客户端发送请求报文的请求头部信息间隔超时时间

官方链接参考:https://nginx.org/en/docs/http/ngx_http_core_module.html#client_header_timeout

Syntax:client_header_timeout time;

Default:

client_header_timeout 60s;

Context:http, server

client_body_timeout     <-- 表示客户端发送请求报文的请求主体信息间隔超时时间

官方链接参考:https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_timeout

Syntax:client_body_timeout time;

Default:

client_body_timeout 60s;

Context:http, server, location

send_timeout            <-- 表示服务端发送响应报文的间隔超时时间

官方链接参考:https://nginx.org/en/docs/http/ngx_http_core_module.html#send_timeout

Syntax:send_timeout time;

Default:

send_timeout 60s;

Context:http, server, location


11 优化nginx服务上传文件限制 (安全优化)

client_max_body_size  控制上传数据大小限制参数

官方链接参考:https://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


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

100k      ---- 1s   90k

100k      ---- 5s   10k

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;

Syntax: gzip_buffers number size;

Default:

gzip_buffers 32 4k|16 8k;

Context: http, server, location

Syntax: gzip_comp_level level;

Default:

gzip_comp_level 1;

Context: http, server, location

Syntax: gzip_types mime-type ...;

Default:

gzip_types text/html;

Context: http, server, location

Syntax: gzip_vary on | off;

Default:

gzip_vary off;

Context: http, server, location

Syntax: gzip_min_length length;

Default:

gzip_min_length 20;

Context: http, server, location

Syntax: gzip_http_version 1.0 | 1.1;

Default:

gzip_http_version 1.1;

Context: http, server, location


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

范例1:将网站的所有图片进行缓存

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$   oldboy.jpg

{

expires      30d;

}

范例2:将网站程序代码文件在本地进行缓存

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

{

expires      30d;

}

注意事项:

01. 进行缓存的时间要设置合理

02. 不是所有请求的数据信息都可以进行缓存


14 Nginx日志相关优化与安全

1)要将web服务日志信息进行切割处理

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

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

access_log off;

}

3)对重要日志信息进行授权

chown -R root.root /app/logs

chmod -R 700 /app/logs

4)进行日志清理

编写脚本自动化清除日志


15 Nginx站点目录及文件URL访问控制

公司会搭建内部网站平台,只想让公司内部人员进行查看

范例1:配置Nginx,禁止解析指定目录下的指定程序。

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

{

allow 10.0.0.0/24;

deny 10.0.0.0/24;

}

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

{

deny all;

}

location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$

{

deny all;

}

范例1:配置禁止访问指定的单个或多个目录。

禁止访问单个目录的命令如下:

location ~ ^/(static)/ {

deny all;

}

location ~ ^/static {

deny all;

}



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

方法1:让使用IP访问网站的用户,或者恶意解析域名的用户,收到501错误,命令如下:

server {

listen 80;

server_name _;

return 501;

}

方法2:通过301跳转到主页,命令如下:

server {

listen 80;

server_name _;

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

}

方法3:发现某域名恶意解析到公司的服务器IP,在server标签里添加以下代码即可,若有多个server则要多处添加。

if ($host !~ ^www\.oldboyedu\.com$)

{

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

}



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

模拟实现盗链过程:

01. 配置盗链网站信息

第一个里程:编写盗链网站配置文件

server {

listen       80;

server_name  www.daolian.org;

root   html/daolian;

index  index.html index.htm;

}

第二里程:编写盗链网站程序代码

<html>

<head>

<title>老男孩教育

</title>

</head>

<body bgcolor=green>

whw的博客!

<br>我的博客是linux

<a href="http://oldboy.blog.51cto.com" target="_blank">博客地址

</a>

<img src="http://www.etiantian.org/ mp4/ sdfasdfdsaadsf/10-10/test.mp4">

</body>

</html>

02. 配置被盗链网站信息

在站点目录下,生成要被盗链的图片信息

[[email protected] www]# ll oldboy.jpg

-rw-r--r-- 1 root root 71806 3月   9 16:27 oldboy.jpg

1) 根据HTTP referer实现防盗链

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/nolink.png;

}

}

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {     <==说明:将盗链提示图片缓存到用户本地

valid_referers none blocked server_names *.etiantian.org etiantian.org;

if ($invalid_referer){

rewrite ^/ http://bbs.etiantian.com/img/nolink.jpg;

}

access_log off;

root html/www;

expires 1d;

break;

}

}

2) 根据cookie防盗链

3) 通过加密变换访问路径实现防盗链(扩展研究)

研究nginx模块  ngx_http_accesskey_module

4)在产品设计上解决盗链方案

在数据信息上加上水印logo信息



18 Nginx错误页面的优雅显示

##www

server {

listen       80;

server_name  www.etiantian.org;

location / {

root   html/www;

index  index.html index.htm;

}

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

}

范例2:50x页面放到本地单独目录下,进行优雅显示。

# redirect server error pages to the static page /50x.html

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   /data0/www/html;

}

范例3:改变状态码为新的状态码,并显示指定的文件内容,命令如下:

error_page 404 =200 /empty.gif;

server {

listen       80;

server_name www.linuxpeixun.com;

location / {

root   /data0/www/bbs;

index  index.html index.htm;

fastcgi_intercept_errors on;

error_page  404 =200    /ta.jpg;

access_log  /app/logs/bbs_access.log  commonlog;

}

}

范例4:错误状态码URL重定向,命令如下:

server {

listen       80;

server_name www.oldboyedu.com;

location / {

root   html/www;

index  index.html index.htm;

error_page   404  http://oldboy.blog.51cto.com;

#<==当出现404错误时,会跳转到指定的URL http://oldboy.blog.51cto.com页面显示给用户,

这个URL一般是企业另外的可用地址

access_log  /app/logs/bbs_access.log  commonlog;

}

}



19 Nginx站点目录文件及目录权限优化

1. robots.txt机器人协议介绍(君子协议)

2. 利用user_agent参数信息进行防爬虫

范例1:阻止下载协议代理,命令如下:

## Block download agents ##

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

{

return 403;

}

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;

}



20 利用Nginx限制HTTP的请求方法

#Only allow these request methods

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

return 501;

}

#Do not accept DELETE,SEARCH and other methods

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

return 501;

}



21 使用普通用户启动Nginx(监牢模式)

第一个里程:把nginx服务重要文件或目录信息放置到普通用户家目录中

[[email protected] ~]$ mkdir {conf,html,logs}

[[email protected] ~]$ cp /application/nginx/conf/nginx.conf.default ./conf/

[[email protected] ~]$ cp /application/nginx/conf/mime.types ./conf/

第二个里程:编写nginx配置文件

[[email protected] ~]$ cat ./conf/nginx.conf

worker_processes  1;

error_log  /home/oldboy/logs/error.log;

pid       /home/oldboy/logs/nginx.pid;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;

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  /home/oldboy/logs/web_blog_access.log  main;

server {

listen       80;               <-- 对于普通用户是没有资格应用特殊端口 大于1024的端口可以让普通用户管理

server_name  www.etiantian.org;

location / {

root   /home/oldboy/html/;

index  index.html index.htm;

}

}

}

第三个里程:利用普通用启动nginx程序

[[email protected] ~]$ /application/nginx/sbin/nginx  -c /home/oldboy/conf/nginx.conf

nginx: [alert] could not open error log file: open() "/application/nginx-1.12.2/logs/error.log" failed (13: Permission denied)

[[email protected] ~]$ ps -ef|grep nginx

oldboy    24337      1  0 19:00 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx -c /home/oldboy/conf/nginx.conf

oldboy    24338  24337  0 19:00 ?        00:00:00 nginx: worker process

oldboy    24340  24297  0 19:00 pts/0    00:00:00 grep --color=auto nginx

原文地址:http://blog.51cto.com/13520774/2087319

时间: 2024-08-03 09:20:01

web网站集群之企业级Nginx Web服务优化详解的相关文章

web网站集群之企业级Nginx Web服务优化详解(二)

12 配置Nginx gzip压缩实现性能优化 100k ---- 1s 90k 100k ---- 5s 10k 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; Syntax: gzip_buffers number size;

web网站集群之企业级Nginx Web服务优化详解(一)

1. 隐藏nginx版本信息优化(安全优化) 官方参考链接:http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens Syntax:  server_tokens on | off | build | string; Default: server_tokens on;(默认显示nginx服务版本) Context: http, server, location 实践配置: server { listen     

大中型网站集群架构企业级高标准全自动实战项目征集

大中型网站集群架构企业级高标准全自动实战 发布本博文目标: 老男孩教育全新期中集群架构项目实战(老男孩老师亲自带队) 1)征集网友或老男孩教育学生资源3-5人. 2)树立老男孩教育运维班期中集群架构项目标杆(期末的的架构会更精彩) 3)共同完成老男孩老师第一本书的结尾项目实战项目. 4)将结合23期运维班毕业前的期末架构项目出书(全自动化大型网站集群搭建优化) 5)让支持老男孩教育网友或老男孩教育学生得到锻炼和成长.享受出书作者待遇. 具体项目将以10台左右VM资源进行部署和演示,规模可以扩展到

集群之LVS(负载均衡)详解

提高服务器响应能力的方法 scale on  在原有服务器的基础上进行升级或者直接换一台新的性能更高的服务器. scale out  横向扩展,将多台服务器并发向外响应客户端的请求.优点:成本低,扩展架构比较简单. 集群(Cluster),通俗地讲就是按照某种组织方式将几台电脑组织起来完成某种特定任务的这样一种架构. 三种集群类型: LB,Load Balancing 负载均衡:在一定程度上能够实现高可用的目的. HA,High Availability 高可用:实时在线,能够及时响应客户端请求

Nginx配置文件、优化详解

上篇<编译安装nginx>已将nginx安装好,这篇写nginx配置文件和部分优化参数. 查看nginx的配置文件路径,可以使用nginx配置文件检查命令nginx -t: 1 [[email protected] ~]# nginx -t 2 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok #编译安装后的nginx配置文件路径3 nginx: configuration file /etc/nginx/ngin

第126讲:Hadoop集群管理之Datanode目录元数据结构详解学习笔记

namenode是管理hdfs文件系统的元数据 datanode是负责当前节点上的数据的管理,具体目录内容是在初始阶段自动创建的.在用hdfs dfs namenode format时并没有对datanode进行format. 在datanode中目录是按文件信息存储的. datanode存在于具体节点上的hadoop-2.6.0/dfs/data/current中. datanode的VERSION内容与namenode的VERSION内容相似. storageID:在namenode与dat

nginx.conf 配置优化详解

user www www;--配置nginx运行用户和用户组,使用之前创建用户useradd www -s /sbin/nologin -M worker_processes 4;--配置nginx worker进程数,根据cpu内核数设置,也可以设置成auto worker_cpu_affinity 0001 0010 0100 1000;--配置cpu亲和力,此配置为4核(如果cpu是8核,前面worker_processes也设置为8,worker_cpu_affinity配置为00000

Web集群部署(Nginx+Keepalived+Varnish+LAMP+NFS)

Web集群部署(Nginx+Keepalived+Varnish+LAMP+NFS)  一.服务介绍   1.1 Nginx服务 Nginx是一个高性能的HTTP和反向代理服务器,也是一个支持IMAP/POP3/SMTP的代理服务器.Nginx即支持Web服务正向代理,也支持反向代理,尤其是反向代理功能十分强大.Nginx支持缓存功能,负载均衡,FASTCGI协议,支持第三方模块.时下Nginx的Web反向代理功能非常流行.   1.2 Keepalived     Keepalived见名知意

keepalived + nginx组建高可用负载平衡Web server集群

1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了屏蔽负载均衡服务器的宕机,需要建立一个备份机.主服务器和备份机上都运行高可用(High Availability)监控程序,通过传送诸如"I am alive"这样的信息来监控对方的运行状况.当备份机不能在一定的时间内收到这样的信息时,它就接管主服务器的服务IP并继续提供负