一、Nginx优化分类
安全优化(提升网站安全性配置)
性能优化(提升用户访问网站效率)
二、Nginx安全优化
2.1 隐藏nginx版本信息优化
官方参数:
1 2 3 |
Syntax: server_tokens on | off | build | string; Default: server_tokens on; Context: http, server, location |
配置举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[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.oldboyedu.com; server_tokens off; location / { root html /www ; index index.html index.htm; } access_log logs /access_www .log main; } } |
测试结果:
1 2 3 4 5 6 7 8 9 10 |
[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 |
2.2 修改nginx版本信息
修改版本信息需要修改程序源文件信息
修改内核信息
1 2 3 4 5 6 |
[root@web01 nginx-1.10.2] # vim src/core/nginx.h # ··· 13 #define NGINX_VERSION "1.0" 14 #define NGINX_VER "oldboy/" NGINX_VERSION 22 #define NGINX_VAR "oldboy" # ··· |
修改头部信息
1 2 3 4 |
[root@web01 nginx-1.10.2] # vim src/http/ngx_http_header_filter_module.c # ··· 49 static char ngx_http_server_string[] = "Server: oldboy" CRLF; # ··· |
修改错误页显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[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>" CRLF 23 "</body>" CRLF 24 "</html>" CRLF 25 ; # ··· 28 static u_char ngx_http_error_tail[] = 29 "<hr><center>oldboy</center>" CRLF 30 "</body>" CRLF 31 "</html>" CRLF 32 ; # ··· |
修改完成后重新编译
1 |
[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 |
重启服务
1 |
[root@web01 nginx-1.10.2] # /etc/init.d/nginx restart |
访问测试是否修改成功
1 2 3 4 5 6 7 8 9 10 |
[root@web01 ~] # curl -I 127.0.0.1 HTTP /1 .1 200 OK Server: oldboy 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 |
2.3 修改worker进程的用户
第一种方法:利用编译安装配置参数,设定nginx默认worker进程用户
1 2 |
useradd -s /sbin/nologin -M www . /configure --user=www --group=www |
第二种方式:编写nginx服务配置文件,设定nginx默认worker进程用户
1 2 3 |
Syntax:user user [group]; Default: user nobody nobody; Context: main |
配置举例:
1 2 3 4 5 6 |
[root@web02 conf] # cat nginx.conf user www www; # 主区块添加user参数 worker_processes 1; events { worker_connections 1024; } |
查看是否生效
1 2 3 4 |
[root@web01 nginx-1.10.2] # ps -ef|grep nginx root 16987 1 0 15:14 ? 00:00:00 nginx: master process nginx oldboy 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 |
2.4 上传文件大小的限制(动态应用)
默认语法说明:
1 2 3 |
syntax:client_max_body_size size; #<==参数语法 default:client_max_body_size 1m; #<==默认值是1m context:http,server,location #<==可以放置的标签段 |
举例配置:
1 2 3 4 5 |
http { sendfile on; keepalive_timeout 65; client_max_body_size 8m; # 设置上传文件最大值8M } |
2.5 站点 Nginx站点目录及文件URL访问控制
01. 根据目录或扩展名,禁止用户访问指定数据信息
1 2 3 4 5 6 7 8 9 10 11 12 |
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文件。
实际配置信息如下:
1 2 3 4 5 6 7 8 9 10 11 |
location ~* \.(txt|doc)$ { if (-f $request_filename){ root /data/www/www ; #rewrite …..可以重定向到某个URL break ; } } location ~* \.(txt|doc)${ root /data/www/www ; denyall; } |
03. 根据IP地址或网络进行访问策略控制
1 2 3 4 5 6 |
location / { deny 192.168.1.1; allow 192.168.1.0 /24 ; allow 10.1.1.0 /16 ; deny all; } |
04. 采用if判断方式,进行访问控制
1 2 3 |
if ($remote_addr = 10.0.0.7 ){ return 403; } |
2.6 配置Nginx,禁止非法域名解析访问企业网站
第一种方式:配置一个server虚拟主机区块,放置在所有server区块最前面
1 2 3 4 5 |
server { listen 80; server_name - ; return 501; } |
第二种方式:将计就计,通过你的域名访问时候,自动跳转到我的域名上
1 2 3 4 5 6 7 8 9 |
server { listen 80 default_server; server_name _; rewrite ^(.*) http: //www .oldboyedu.com/$1 permanent; } if ($host !~ ^www\.oldboyedu\.com$) { rewrite ^(.*) http: //www .oldboyedu.com/$1 permanent; } |
2.7 Nginx图片及目录防盗链解决方案
什么是资源盗链 ?
简单地说,就是某些不法网站未经许可,通过在其自身网站程序里非法调用其他网站的资源,然后在自己的网站上显示这些调用的资源,达到填充自身网站的效果。
实现盗链过程:
01. 真正的合法网站(盗链的目标) web01 www.oldboyedu.com www站点目录有一个oldboy.jpg图片
1 2 3 4 5 6 7 8 9 |
# 配置静态虚拟主机 server { listen 80; server_name www.oldboyedu.com; location / { root html /www ; index index.html index.htm; } # 确认生成盗链文件 |
02. 不合法的网站(真正盗链网站) www.daolian.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 编写一个html盗链文件 <html> < head > <title>老男孩IT教育< /title > < /head > <body bgcolor=green> 老男孩IT教育的博客! <br>我的博客是 <a href= "http://www.oldboyedu.com" target= "_blank" >博客地址 < /a > <img src= "http://www.oldboyedu.com/oldboy.jpg" > < /body > < /html > |
编写盗链虚拟主机
1 2 3 4 5 6 7 8 |
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配置。
1 2 3 4 5 6 7 |
location ~* /\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ { root html /www ; valid_referers none blocked *.oldboyedu.com oldboyedu.com; if ($invalid_referer){ rewrite ^/ http: //www .oldboyedu.com /img/nolink .jpg; } } |
设置expires的方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@oldboy www] # cat /application/nginx/conf/extra/www.conf server { listen 80; server_name www.oldboyedu.com; root html /www ; index index.html index.htm; access_log logs /www_access .log main; #Preventing hot linking of images and other file types location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked server_names *.oldboyedu.com oldboyedu.com; if ($invalid_referer){ rewrite ^/ http: //www .oldboyedu.com /img/nolink .jpg; } access_log off; root html /www ; expires 1d; break ; } } |
2) 根据cookie防盗链
3) 通过加密变换访问路径实现防盗链
4) 在所有网站资源上添加网站信息,让盗链人员帮你做推广宣传
2.8 NGINX错误页面友好显示
范例1:对错误代码403实行本地页面跳转,命令如下:
1 2 3 4 5 6 7 8 9 10 |
###www server { listen 80; server_name www.oldboyedu.com; location / { root html /www ; index index.html index.htm; } error_page 403 /403 .html; #<==当出现403错误时,会跳转到403.html页面 } |
# 上面的/403.html是相对于站点根目录html/www的。
范例2:50x页面放到本地单独目录下,进行优雅显示。
1 2 3 4 5 |
# 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:改变状态码为新的状态码,并显示指定的文件内容,命令如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
error_page 404 =200 /empty .gif; server { listen 80; server_name www.oldboyedu.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重定向,命令如下:
1 2 3 4 5 6 7 8 9 10 11 |
server { listen 80; server_name www.oldboyedu.com; location / { root html /www ; index index.html index.htm; error_page 404 https: //oldboy .cnblogs.com; #<==当出现404错误时,会跳转到指定的URL https://oldboy.cnblogs.com页面显示给用户,这个URL一般是企业另外的可用地址 access_log /app/logs/bbs_access .log commonlog; } } |
2.9 Nginx站点目录文件及目录权限优化
2.10 Nginx防爬虫优化
范例1:阻止下载协议代理,命令如下:
1 2 3 4 5 |
## Block download agents ## if ($http_user_agent ~* LWP::Simple|BBBike|wget) { return 403; } |
范例2:添加内容防止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; } |
2.11 利用Nginx限制HTTP的请求方法
#Only allow these request methods
1 2 3 |
if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 501; } |
#Do not accept DELETE,SEARCH and other methods
2.12 使用普通用户启动nginx
1、切换到普通用户家目录下,创建nginx所需文件
1 2 3 4 5 |
[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、编写配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[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服务
1 2 3 |
/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 & |
注意:忽略一些不正确的输出信息