favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会本身主动发起请求获取页面的favicon.ico文件,可是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,并且浏览器也会显示404报错。html
# 一:服务器不记录访问日志: # location = /favicon.ico { # log_not_found off; # access_log off; # } # 二:将图标保存到指定目录访问: # location ~ ^/favicon\.ico$ { location = /favicon.ico { root /data/nginx/images123; }
# 修改Nginx源码文件,此配置文件须要在nginx.conf的http中添加server_tokens off;开启nginx版本隐藏才能实现预期效果 [root@CentOS7 nginx-1.14.2]#vim src/http/ngx_http_header_filter_module.c 49 static u_char ngx_http_server_string[] = "Server: Darius/10.0" CRLF; # 中止Nginx服务,从新编译Nginx [root@CentOS7 nginx-1.14.2]#/apps/nginx/sbin/nginx -s stop [root@CentOS7 nginx-1.14.2]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/echo-nginx-module [root@CentOS7 nginx-1.14.2]#make && make ×××tall 启动服务 [root@CentOS7 nginx-1.14.2]#/apps/nginx/sbin/nginx 检测 [root@CentOS7-Test ~]#curl -I www.darius.com HTTP/1.1 200 OK Server: Darius/10.0 # 修改src/core/nginx.h文件无需开启隐藏功能,起到修改版本信息的效果 [root@CentOS7 nginx-1.14.2]# vim src/core/nginx.h 13 #define NGINX_VERSION "10.0" 14 #define NGINX_VER "Darius/" NGINX_VERSION
Nginx服务器利用ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perl compatibler egularexpression),所以编译以前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是很是有用的功能,好比它能够在咱们改变网站结构以后,不须要客户端修改原来的书签,也无需其余网站修改咱们的连接,就能够设置为访问,另外还能够在必定程度上提升网站的安全性。nginx
用于条件匹配判断,并根据条件判断结果选择不一样的Nginx配置,能够配置在server或location块中进行配置,Nginx的if语法仅能使用if作单次判断,不支持使用if else或者if elif这样的多重判断web
location /main { index index.html; default_type text/html; if ( $scheme = http ) { echo "if --> $scheme"; } } [root@CentOS7 conf.d]#nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntaxis ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 conf.d]#nginx -s reload 检测 [root@CentOS7-Test ~]#curl www.darius.com/main if --> http
=: #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false。 !=: #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false。 ~: #表示在匹配过程当中区分大小写字符,(能够经过正则表达式匹配),知足匹配条件为真,不知足为假。 ~*: #表示在匹配过程当中不区分大小写字符,(能够经过正则表达式匹配),知足匹配条件为真,不知足问假。 !~:#区分大小写不匹配,不知足为真,知足为假,不知足为真。 !~*:#为不区分大小写不匹配,知足为假,不知足为真。 -f 和 ! -f:判断请求的文件是否存在和是否不存在 -d 和 ! -d: #判断请求的目录是否存在和是否不存在。 -x 和 ! -x: #判断文件是否可执行和是否不可执行。 -e 和 ! -e: #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软连接)。 注: 若是$变量的值为空字符串或是以0开头的任意字符串,则if指令认为该条件为false,其余条件为true。
指定key并给其定义一个变量,变量能够调用Nginx内置变量赋值给key,另外set定义格式为set $key $value,及不管是key仍是value都要加$符号。正则表达式
[root@CentOS7 conf.d]#vim pc.conf location /set { root index.html; default_type text/html; set $name Darius; echo $name; set $my_port $server_port; echo $my_port; } [root@CentOS7 conf.d]#nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntaxis ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 conf.d]#nginx -s reload 检测 [root@CentOS7-Test ~]#curl www.darius.com/set Darius 80
用于中断当前相同做用域(location)中的其余Nginx配置,与该指令处于同一做用域的Nginx配置中,位于它前面的配置生效,位于后面的指令配置就再也不生效了,Nginx服务器在根据配置处理请求的过程当中遇到该指令的时候,回到上一层做用域继续向下读取配置,该指令能够在server块和location块以及if块中使用,使用语法以下:express
[root@CentOS7 conf.d]#vim pc.conf location /set { root index.html; default_type text/html; set $name Darius; echo $name; break; set $my_port $server_port; echo $my_port; } [root@CentOS7 conf.d]#nginx -s reload 检测 [root@CentOS7-Test ~]#curl www.darius.com/set Darius
从nginx版本0.8.2开始支持,return用于完成对请求的处理,并直接向客户端返回响应状态码,好比其能够指定重定向URL(对于特殊重定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的全部配置都将不被执行,return能够在server、if和location块进行配置json
location /main { index index.html; default_type text/html; if ( $scheme = http ) { return 666 "not allow http"; # 能够是返回给客户端指定的HTTP状态码、也能够是返回给客户端的状态码及响应体内容(能够调用变量)、或者返回给客户端URL地址 # echo "if-----> $scheme"; # return后面的将再也不执行 } [root@CentOS7-Test ~]#curl www.darius.com/main not allow http [root@CentOS7-Test ~]#curl -I www.darius.com/main HTTP/1.1 666 Server: Darius/10.0 Date: Sat, 01 Jun 2019 03:52:37 GMT Content-Type: text/html Content-Length: 14 Connection: keep-alive
设置是否开启记录ngx_http_rewrite_module模块日志记录到error_log日志文件当中,能够配置在http、server、location或if当中,须要日志级别为noticevim
[root@CentOS7 conf.d]#vim ../conf/nginx.conf error_log logs/error.log notice; # 开启错误日志notice级别 [root@CentOS7 conf.d]#vim pc.conf # 启用rewrite_log指令 location /set { root index.html; default_type text/html; set $name Darius; echo $name; rewrite_log on; break; set $my_port $server_port; echo $my_port; } [root@CentOS7 conf.d]#nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 conf.d]#nginx -s reload 访问并验证 [root@CentOS7 conf.d]#tail -f /apps/nginx/logs/*.log ==> /apps/nginx/logs/error.log <== 2019/06/01 12:01:46 [warn] 11234#0: *40 using uninitialized "my_port" variable, client: 192.168.36.110, server: www.darius.com, request: "GET /set/aaa HTTP/1.1", host: "www.darius.com"
经过正则表达式的匹配来改变URI,能够同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI作具体处理 api
URI(universal resource identifier):通用资源标识符,标识一个资源的路径,能够不带协议。
URL(uniform resource location):统一资源定位符,是用于在Internet中描述资源的字符串,是URI的子集,主要包括传输协议(scheme)、主机(IP、端口号或者域名)和资源具体地址(目录和文件名)等三部分,通常格式为 scheme://主机名[:端口号][/资源路径],如:http://www.a.com:8080/path/file/index.html就是一个URL路径,URL必须带访问协议。
每一个URL都是一个URI,可是URI不都是URL。
例如:
http://example.org/path/to/resource.txt #URI/URL
ftp://example.org/resource.txt #URI/URL
/absolute/path/to/resource.txt #URI 浏览器
[root@CentOS7 conf.d]#vim ../conf/nginx.conf location / { root html; index index.html index.htm; rewrite / http://www.darius.com permanent; # 永久重定向301 #rewrite / http://www.darius.com redirect; # 临时重定向302 } [root@CentOS7 conf.d]#nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 conf.d]#nginx -s reload 重定向检测 [root@CentOS7-Test ~]#curl 192.168.36.104 <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html> [root@CentOS7-Test ~]#curl -L 192.168.36.104 www.darius.com [root@CentOS7-Test ~]#curl -I 192.168.36.104 HTTP/1.1 301 Moved Permanently Server: Darius/10.0 Date: Sat, 01 Jun 2019 04:27:42 GMT Content-Type: text/html Content-Length: 178 Connection: keep-alive Location: http://www.darius.com
[root@CentOS7-Test ~]#curl -I 192.168.36.104 HTTP/1.1 302 Moved Temporarily Server: Darius/10.0 Date: Sat, 01 Jun 2019 04:28:32 GMT Content-Type: text/html Content-Length: 154 Connection: keep-alive Location: http://www.darius.com
location /last { rewrite ^/last/(.*) /test$1 last; return 888 "last"; } location /break { rewrite ^/break/(.*) /test$1 break; return 666 "break"; } location /test { return 999 "test"; } [root@CentOS7 conf.d]#nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@CentOS7 conf.d]#nginx -s reload # break不会跳转到其余location中 [root@CentOS7-Test ~]#curl -L -i http://www.darius.com/break/index.html HTTP/1.1 404 Not Found Server: Darius/10.0 Date: Sat, 01 Jun 2019 06:12:04 GMT Content-Type: text/html Content-Length: 162 Connection: keep-alive Vary: Accept-Encoding <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx</center> </body> </html> # last会跳转到其余location中继续执行匹配操做 [root@CentOS7-Test ~]#curl -L -i http://www.darius.com/last/index.html HTTP/1.1 999 Server: Darius/10.0 Date: Sat, 01 Jun 2019 06:12:11 GMT Content-Type: text/html Content-Length: 4 Connection: keep-alive test
server { listen 80; listen 443 ssl; server_name www.darius.com; error_log /apps/nginx/logs/www_darius_com_error.log; access_log /apps/nginx/logs/www_darius_com_access.log access_json; ssl_certificate /apps/nginx/certs/www.darius.com.crt; ssl_certificate_key /apps/nginx/certs/www.darius.com.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; location / { root /data/nginx/html/pc; index index.html; if ( $scheme = http ){ rewrite (.*) https://www.darius.com; } } } [root@CentOS7 conf.d]#nginx -s reload 访问测试 [root@CentOS7-Test ~]#curl -L -i -k http://www.darius.com HTTP/1.1 302 Moved Temporarily Server: Darius/10.0 Date: Sat, 01 Jun 2019 06:29:34 GMT Content-Type: text/html Content-Length: 154 Connection: keep-alive Location: https://www.darius.com HTTP/1.1 200 OK Server: Darius/10.0 Date: Sat, 01 Jun 2019 06:29:37 GMT Content-Type: text/html Content-Length: 7 Last-Modified: Thu, 30 May 2019 03:06:03 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "5cef489b-7" Accept-Ranges: bytes pc web
# 当用户访问到公司网站时,输入一个错误的URL,能够将用户访问的浏览页面重定向到公司官网首页上 location / { root /data/nginx/html/pc; index index.html; if ( !-f $request_filename ){ rewrite (.*) http://www.darius.com/index.html; } } 浏览测试 [root@CentOS7-Test ~]#curl -L -i http://www.darius.com/asdfg HTTP/1.1 302 Moved Temporarily Server: Darius/10.0 Date: Sat, 01 Jun 2019 06:56:26 GMT Content-Type: text/html Content-Length: 154 Connection: keep-alive Location: http://www.darius.com/index.html HTTP/1.1 200 OK Server: Darius/10.0 Date: Sat, 01 Jun 2019 06:56:26 GMT Content-Type: text/html Content-Length: 7 Last-Modified: Thu, 30 May 2019 03:06:03 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "5cef489b-7" Accept-Ranges: bytes pc web
防盗链基于客户端携带的referer实现,referer是记录打开一个页面以前记录是从哪一个页面跳转过来的标记信息,若是别人只连接了本身网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链,referer就是以前的那个网站域名,正常的referer信息有如下几种:安全
none:请求报文首部没有referer首部,好比用户直接在浏览器输入域名访问web网站,就没有referer信息。 blocked:请求报文有referer首部,但无有效值,好比为空。 server_names:referer首部中包含本主机名及即nginx 监听的server_name。 arbitrary_string:自定义指定字符串,但可以使用*做通配符。 regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头,例如: ~.*\.magedu\.com。
[root@CentOS7 conf.d]#cat a.conf server { listen 80; charset utf-8; server_name www.a.com; location / { root /data; index index.html; } } [root@CentOS7 conf.d]#cat /data/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盗链页面</title> </head> <body> <a href="http://www.darius.com">测试盗链</a> <img src="http://www.darius.com/logo.png"> </body> </html>
[root@CentOS7 conf.d]#tail -f /apps/nginx/logs/*.log ==> /apps/nginx/logs/www_darius_com_access.log <== {"@timestamp":"2019-06-01T15:21:30+08:00","host":"192.168.36.104","clientip":"192.168.36.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.darius.com","uri":"/logo.png","domain":"www.darius.com","xff":"-","referer":"http://www.a.com/","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:67.0) Gecko/20100101 Firefox/67.0","status":"304"}
基于访问安全考虑,nginx支持经过ungx_http_referer_module模块检查访问请求的referer信息是否有效实现防盗链功能
location / { root /data/nginx/html/pc; index index.html; valid_referers none blocked server_names *.magedu.com www.magedu.* api.online.test/v1/hostlist ~\.google\. ~\.baidu\.; if ($invalid_referer) { return 403; } } [root@CentOS7 conf.d]#nginx -s reload
页面访问测试