1.1 Nginx优化分类

安全优化(提升网站安全性配置)

性能优化(提升用户访问网站效率)

1.2 Nginx安全优化

1.2.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;
Context: http, server, location

配置举例:

[root@web01 ~]# cat /application/nginx/conf/nginx.conf
worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        off;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"';server {listen       80;server_name  www.nmtui.com;server_tokens off;location / {root   html/www;index  index.html index.htm;}access_log  logs/access_www.log  main;}
}

测试结果:

[root@web01 ~]# curl -I 10.0.0.8
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 01 Nov 2017 18:32:40 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Wed, 25 Oct 2017 01:20:56 GMT
Connection: keep-alive
ETag: "59efe6f8-a"
Accept-Ranges: bytes

1.2.2 修改nginx版本信息

修改版本信息需要修改程序源文件信息

修改内核信息

[root@web01 nginx-1.10.2]# vim  src/core/nginx.h
# ···13 #define NGINX_VERSION      "1.0"14 #define NGINX_VER          "clsn/" NGINX_VERSION22 #define NGINX_VAR          "clsn"
# ···

修改头部信息

[root@web01 nginx-1.10.2]# vim  src/http/ngx_http_header_filter_module.c
# ···49 static char ngx_http_server_string[] = "Server: clsn" CRLF;
# ···

修改错误页显示

[root@web01 nginx-1.10.2]# vim src/http/ngx_http_special_response.c
# ···
# 此处可以不修改21 static u_char ngx_http_error_full_tail[] =22 "<hr><center>" NGINX_VER "</center>" CRLF23 "</body>" CRLF24 "</html>" CRLF25 ;
# ···28 static u_char ngx_http_error_tail[] =29 "<hr><center>clsn</center>" CRLF30 "</body>" CRLF31 "</html>" CRLF32 ;
# ···

修改完成后重新编译

[root@web01 nginx-1.10.2]# ./configure  --prefix=/application/nginx-1.10.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

重启服务

[root@web01 nginx-1.10.2]# /etc/init.d/nginx restart

访问测试是否修改成功

[root@web01 ~]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: clsn
Date: Wed, 01 Nov 2017 19:05:43 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Wed, 25 Oct 2017 01:20:56 GMT
Connection: keep-alive
ETag: "59efe6f8-a"
Accept-Ranges: bytes

1.2.3 修改worker进程的用户

第一种方法:利用编译安装配置参数,设定nginx默认worker进程用户

useradd -s /sbin/nologin -M www
./configure --user=www --group=www

第二种方式:编写nginx服务配置文件,设定nginx默认worker进程用户

官方配置参数说明:http://nginx.org/en/docs/ngx_core_module.html#user

Syntax:user user [group];
Default: user nobody nobody;
Context: main

配置举例:

[root@web02 conf]# cat nginx.conf
user  www www;          # 主区块添加user参数
worker_processes  1;
events {worker_connections  1024;
}

查看是否生效

[root@web01 nginx-1.10.2]# ps -ef|grep nginx
root      16987      1  0 15:14 ?        00:00:00 nginx: master process nginx
clsn    18484  16987  0 15:22 ?        00:00:00 nginx: worker process
root      18486   9593  0 15:22 pts/0    00:00:00 grep --color=auto nginx

1.2.4 上传文件大小的限制(动态应用)

默认语法说明:

syntax:client_max_body_size size;    #<==参数语法
default:client_max_body_size 1m;    #<==默认值是1m
context:http,server,location        #<==可以放置的标签段

举例配置:

http {sendfile        on;keepalive_timeout  65;client_max_body_size 8m;    # 设置上传文件最大值8M
}

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

01. 根据目录或扩展名,禁止用户访问指定数据信息

location ~ ^/images/.*\.(php|php5|sh|pl|py|html)$ { deny all;}
location ~ ^/static/.*\.(php|php5|sh|pl|py)$ { deny all;}
location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$ { deny all;} 

02. 当访问禁止的数据信息时,进行页面跳转

Nginx下配置禁止访问*.txt和*.doc文件。

实际配置信息如下:

location ~* \.(txt|doc)$ {if (-f $request_filename){root /data/www/www;#rewrite …..可以重定向到某个URLbreak;}
}
location ~* \.(txt|doc)${root /data/www/www;denyall;
}

03. 根据IP地址或网络进行访问策略控制

location / { deny 192.168.1.1;allow 192.168.1.0/24;allow 10.1.1.0/16;deny all;
}

04. 采用if判断方式,进行访问控制

if ($remote_addr = 10.0.0.7 ){return 403;}

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

第一种方式:配置一个server虚拟主机区块,放置在所有server区块最前面

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

第二种方式:将计就计,通过你的域名访问时候,自动跳转到我的域名上

server {listen 80 default_server;server_name _;rewrite ^(.*) http://www.nmtui.com/$1 permanent;
}
if ($host !~ ^www\.nmtui\.com$)
{rewrite ^(.*) http://www.nmtui.com/$1 permanent;
}

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

什么是资源盗链 ?

简单地说,就是某些不法网站未经许可,通过在其自身网站程序里非法调用其他网站的资源,然后在自己的网站上显示这些调用的资源,达到填充自身网站的效果。

实现盗链过程:

01. 真正的合法网站(盗链的目标)  web01   www.nmtui.com www站点目录有一个oldboy.jpg图片

    # 配置静态虚拟主机server {listen       80;server_name  www.nmtui.com;location / {root   html/www;index  index.html index.htm;}# 确认生成盗链文件

02. 不合法的网站(真正盗链网站)  www.daolian.com

   # 编写一个html盗链文件<html><head><title>惨绿少年</title></head><body bgcolor=green>惨绿少年的博客!<br>我的博客是<ahref="http://www.nmtui.com" target="_blank">博客地址</a><img src="http://www.nmtui.com/clsn.jpg"></body></html>

编写盗链虚拟主机

    server {listen       80;server_name  www.daolian.org;location / {root   html;index  index.html index.htm;}   }

至此就实现了盗链。

03 常见防盗链解决方案的基本原理

1)     根据HTTP referer实现防盗链

利用referer,并且针对扩展名rewrite重定向,下面的代码为利用referer且针对扩展名rewrite重定向,即实现防盗链的Nginx配置。

    location ~* /\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {root  html/www;valid_referers none blocked *.nmtui.com nmtui.com;if ($invalid_referer){ rewrite ^/  http://www.nmtui.com/img/nolink.jpg;} } 

设置expires的方法如下:

    [root@clsn www]# cat /application/nginx/conf/extra/www.conf server {listen            80;server_name        www.nmtui.com;root        html/www;index        index.html index.htm;access_log    logs/www_access.log main;#Preventing hot linking of images and other file typeslocation ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {valid_referers none blocked server_names *.nmtui.com nmtui.com;if ($invalid_referer){rewrite ^/ http://www.nmtui.com/img/nolink.jpg;}access_log off;root html/www;expires 1d;break;}}

2) 根据cookie防盗链

3) 通过加密变换访问路径实现防盗链

4) 在所有网站资源上添加网站信息,让盗链人员帮你做推广宣传

1.2.8 NGINX错误页面友好显示

范例1:对错误代码403实行本地页面跳转,命令如下:

###wwwserver {listen       80;server_name  www.nmtui.com;location / {root   html/www;index  index.html index.htm;}error_page  403  /403.html;    #<==当出现403错误时,会跳转到403.html页面}

# 上面的/403.html是相对于站点根目录html/www的。

范例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.nmtui.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.nmtui.com;location / {root   html/www;index  index.html index.htm;error_page   404  https://clsn.cnblogs.com;
#<==当出现404错误时,会跳转到指定的URL https://clsn.cnblogs.com页面显示给用户,这个URL一般是企业另外的可用地址access_log  /app/logs/bbs_access.log  commonlog;}
}

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

服务器角色

权限处理

安全系数

动态Web集群

目录权限755

文件权限644

所用的目录,以及文件用户和组都是root

环境为Nginx+PHP   文件不能被改,目录不能被写入,安全系数10

static图片集群

目录权限755

文件权限644

所用的目录,以及文件用户和组都是root

环境为Nginx    文件不能被改,目录不能被写入,安全系数10

上传upload集群

目录权限755

文件权限644

所用的目录,以及文件用户和组都是root

特别:用户上传的目录设置为755,用户和组使用Nginx服务配置的用户

文件不能被改,目录不能被写入,但是用户上传的目录允许写入文件且需要通过Nginx的其他功能来禁止读文件,安全系数8

1.2.10 Nginx防爬虫优化

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

## Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget){return 403;
}

范例2:添加内容防止N多爬虫代理访问网站,命令如下:

这些爬虫代理使用“|”分隔,具体要处理的爬虫可以根据需求增加或减少,添加的内容如下:

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.11 利用Nginx限制HTTP的请求方法

#Only allow these request methods

if ($request_method !~ ^(GET|HEAD|POST)$ ) {return 501;
}

#Do not accept DELETE,SEARCH and other methods

1.2.12 使用普通用户启动nginx

1、切换到普通用户家目录下,创建nginx所需文件

[nginx@web01 ~]$ mkdir -p blog/{conf,logs,html}
[nginx@web01 ~]$ cd blog/
[nginx@web01 blog]$ cp /application/nginx/conf/nginx.conf.default  ./conf/
[nginx@web01 blog]$ grep -vE "^$|#" conf/nginx.conf.default  >  conf/nginx.conf
[nginx@web01 blog]$ cp /application/nginx/conf/mime.types conf/

2、编写配置文件

[nginx@web01 ~]$ cat blog/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535;
error_log  /home/nginx/blog/logs/error.log;
user inca inca;
pid       /home/nginx/blog/logs/nginx.pid;
events {use epoll;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"';server {listen       8080;server_name  www.etiantian.org;root   /home/nginx/blog/html;location / {index  index.html index.htm;}access_log  /home/nginx/blog/logs/web_blog_access.log  main;}
}

注意:普通用户不能使用知名端口,需要使用其他端口启动服务

3、检查配置文件语法,并启动nginx服务

/application/nginx/sbin/nginx -t -c /home/nginx/blog/conf/nginx.conf
或
/application/nginx/sbin/nginx -c /home/nginx/blog/conf/nginx.conf &>/dev/null &

注意:忽略一些不正确的输出信息

转载于:https://www.cnblogs.com/A121/p/10059851.html

nginx优化_安全方面的优化相关推荐

  1. louvian算法 缺点 优化_机器学习中的优化算法(1)-优化算法重要性,SGD,Momentum(附Python示例)...

    本系列文章已转至 机器学习的优化器​zhuanlan.zhihu.com 优化算法在机器学习中扮演着至关重要的角色,了解常用的优化算法对于机器学习爱好者和从业者有着重要的意义. 这系列文章先讲述优化算 ...

  2. 双目标帕累托优化_结构力学中的优化分析(3) —— 结构优化分析

    引言 上文中,我们主要介绍了优化分析的基本类型. 蒙特遇见卡罗:结构力学中的优化分析(1) -- 优化方法基本概念​zhuanlan.zhihu.com 蒙特遇见卡罗:结构力学中的优化分析(2) -- ...

  3. 九度搜索引擎点击优化_吉林免费seo优化工具

    吉林免费sxaa0c1eo优化工具,网站在建立时搜索引擎是较为完善的基本上是不太需要长期进行优化的,但是近几年搜索引擎优化的频率开始增加,这让人有些好奇,搜索引擎优化的目的是什么?为什么受企业欢迎呢? ...

  4. mysql not in优化_实践中如何优化MySQL(收藏)

    SQL语句的优化: 1.尽量避免使用子查询 3.用IN来替换OR 4.LIKE前缀%号.双百分号._下划线查询非索引列或*无法使用到索引,如果查询的是索引列则可以 5.读取适当的记录LIMIT M,N ...

  5. mysql 8核16g参数优化_问个 MySql 优化问题, 16G, 8 核服务器??

    服务器配置: 处理器:Xeon E3-1230 V2 内存:16G DDR3 1600Mhz 硬盘:1TB 企业级硬盘 我现在的配置 [mysqld] port= xxxx socket= xxx d ...

  6. 白屏优化_今日头条品质优化 图文详情页秒开实践

    背景 作为一个内容类应用,看新闻读资讯一直是头条用户的核心需求,页面的打开速度直接关系到用户使用头条的核心体验,在头条中,为了更多的承载足够丰富的样式和逻辑下保持多端体验的统一,详情页的内容我们是通过 ...

  7. 网站加载速度 优化_您肯定要优化网站的加载速度。 这是如何做。

    网站加载速度 优化 Do you remember this iconic scene from a very famous Star Wars Parody? 您还记得著名的<星球大战> ...

  8. python 链表操作 优化_链表的内存优化

    我最近制作了一个脚本来生成随机迷宫,它使用一个自定义的迷宫类,由几个迷宫节点构建而成.每个节点如下所示:class mazeNode: def __init__(self, pos, conn = N ...

  9. aspen吸收塔气液比优化_使用 Aspen Plus 优化环氧乙烷吸收塔

    使用 Aspen Plus 优化环氧乙烷吸收塔 气体吸收塔在化学加工工业 ( CPI ) 中有着广泛的应用,溶剂通过接触吸 收混合气中的可溶解组分组成, 从而来分离各个组成. 溶质在气相和液相之间转 ...

最新文章

  1. 图像分类:CVPR2020论文解读
  2. DataGridView 密码列(显示为*号)的设置
  3. Springboot之多环境打包配置
  4. ubuntu解决eclipse中logcat只显示level栏
  5. ReactiveCocoa的使用方法
  6. python有参装饰器 多个装饰器装饰一个
  7. mysql双机互备linux成功的_配置MySQL双机热备 - Linux服务器MySQL双机热备份试验_数据库技术_Linux公社-Linux系统门户网站...
  8. url的特殊字符编码 encodeURI编码
  9. 绘制三维散点图_使用Origin,轻松绘制三维散点图
  10. 面试题之SpringMVC整体工作流程
  11. CString转换成const char*
  12. 《JavaScript 高级程序设计(第四版)》—— 06 集合引用类型
  13. Java开发自学教程!java应届生面试自我介绍
  14. Flashpaper序列号
  15. 回归中的相关度和决定系数及应用
  16. 怎么用matlab画心形曲线方程,matlab画心形曲线
  17. iOS开发三方资源 - 欲先攻其事必先利其器
  18. 不稳定就是人生常态,要坚信未来一定会非常美好
  19. mysql查询数据库中所有字段的属性
  20. 2023,再转行去做软件测试还有前途吗?

热门文章

  1. 作业帮电脑版在线使用_应届生应聘作业帮的在线辅导老师
  2. 为什么在加油站上班,一个月休3天,工资2000元,却有人干?
  3. 钱花了才是自己的你们觉得这句话对吗?
  4. 为什么你说的话别人不愿意听?
  5. 30岁的你,目标工资是多少?
  6. 最简单的零成本创业模式
  7. 4.2第一个窗口程序
  8. Qt4_快速设计对话框
  9. 关于web项目跨域问题详解
  10. dmv 统计数据库io_使用内置功能和动态管理视图(DMV)发现特定于数据库的信息