关于favicon.ico

favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错。

具体配置

# 一:服务器不记录访问日志:# location = /favicon.ico {#   log_not_found off;#   access_log off;# }
# 二:将图标保存到指定目录访问:# location ~ ^/favicon\.ico$ {location = /favicon.ico {root /data/nginx/images123;}

显示效果

修改Nginx Server版本信息

# 修改Nginx源码文件,此配置文件需要在nginx.conf的http中添加server_tokens  off;开启nginx版本隐藏才能实现预期效果
[root@CentOS7 nginx-1.14.2]#vim src/http/ngx_http_header_filter_module.c49 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.h13 #define NGINX_VERSION      "10.0"14 #define NGINX_VER          "Darius/" NGINX_VERSION

Nginx Rewrite相关功能

Nginx服务器利用ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perl compatibler egularexpression),因此编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能,比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的链接,就可以设置为访问,另外还可以在一定程度上提高网站的安全性。

if指令

用于条件匹配判断,并根据条件判断结果选择不同的Nginx配置,可以配置在server或location块中进行配置,Nginx的if语法仅能使用if做单次判断,不支持使用if else或者if elif这样的多重判断

  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。
!=: #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false。
~: #表示在匹配过程中区分大小写字符,(可以通过正则表达式匹配),满足匹配条件为真,不满足为假。
~*: #表示在匹配过程中不区分大小写字符,(可以通过正则表达式匹配),满足匹配条件为真,不满足问假。
!~:#区分大小写不匹配,不满足为真,满足为假,不满足为真。
!~*:#为不区分大小写不匹配,满足为假,不满足为真。-f 和 ! -f:判断请求的文件是否存在和是否不存在
-d 和 ! -d: #判断请求的目录是否存在和是否不存在。
-x 和 ! -x: #判断文件是否可执行和是否不可执行。
-e 和 ! -e: #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)。注: 如果$变量的值为空字符串或是以0开头的任意字符串,则if指令认为该条件为false,其他条件为true。

set指令

指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key,另外set定义格式为set $key $value,及无论是key还是value都要加$符号。

[root@CentOS7 conf.d]#vim pc.conflocation /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

break指令

用于中断当前相同作用域(location)中的其他Nginx配置,与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效,位于后面的指令配置就不再生效了,Nginx服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配置,该指令可以在server块和location块以及if块中使用,使用语法如下:

[root@CentOS7 conf.d]#vim pc.conflocation /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

return指令

从nginx版本0.8.2开始支持,return用于完成对请求的处理,并直接向客户端返回响应状态码,比如其可以指定重定向URL(对于特殊重定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的所有配置都将不被执行,return可以在server、if和location块进行配置

  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

rewrite_log指令

设置是否开启记录ngx_http_rewrite_module模块日志记录到error_log日志文件当中,可以配置在http、server、location或if当中,需要日志级别为notice

[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"

rewrite指令

通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI做具体处理

  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

rewrite 四种flag使用介绍

  1. redirect;
      # 临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
  2. permanent;
      # 永久重定向,重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,状态码:301
  3. last;
      # 重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URL启动新一轮重写检查,不建议在location中使用
  4. break;
      # 重写完成后停止对当前URL在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location中使用
注:其中前两种是跳转型的flag,后两种是代理型,跳转型是指有客户端浏览器重新对新地址进行请求,代理型是在WEB服务器内部实现跳转的。

rewrite域名永久重定向

[root@CentOS7 conf.d]#vim ../conf/nginx.conflocation / {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

rewrite 临时重定向

[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

rewrite之URI重定向

  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-alivetest

rewrite实现页面自动跳转https

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.comHTTP/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: bytespc 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.htmlHTTP/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: bytespc web

Nginx防盗链

防盗链基于客户端携带的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

页面访问测试

转载于:https://blog.51cto.com/12980155/2403597

Nginx高阶用法(二)相关推荐

  1. Nginx高阶用法(一)

    Nginx 状态页   基于nginx模块ngx_http_auth_basic_module实现,在编译安装nginx的时候需要添加编译参数--with-http_stub_status_modul ...

  2. Nginx高阶用法(三)

    Nginx反向代理   反向代理:反向代理也叫reverse proxy,指的是代理外网用户的请求到内部的指定web服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式.   Nginx除了 ...

  3. Nginx --高阶

    Nginx -- 高阶 Nginx高级 第一部分:扩容 1.单机垂直扩容:硬件资源增加 2.水平扩展:集群化 会话管理 Nginx高级负载均衡 使用sticky模块完成对Nginx的负载均衡 1.下载 ...

  4. 【性能测试】如何用一条命令完全掌握linux系统性能监控(top高阶用法)

    目  录 一 引 言 二 top命令高阶用法 场景1:采样3次,采样间隔为10s: 场景2:采样2h,采样间隔为10s,性能数据保存到test.csv文件中: 一 引 言 熟悉CentOS linux ...

  5. php redis微信发红包,高阶篇二 使用Redis队列发送微信模版消息

    # 高阶篇二 使用Redis队列发送微信模版消息 > 此命令行执行任务的方法类比较复杂 他需要命令行运行才会有效 > 命令行源码以及创建方法 参见上节 https://www.kanclo ...

  6. ifdef的用法_chisel 高阶用法简介--rocket-chip generator

    本文将介绍chisel的三个高阶用法:diplomacy,cake pattern和参数化. diplomacy 什么是diplomacy?互联参数的自动协商. 痛点在哪里: 传统的SoC集成中,互联 ...

  7. Peewee 高阶用法

    Peewee 高阶用法 前言 本文介绍的Peewee方法语法基于PostgreSQL 高阶用法 元组多条件查询 from peewee import Tuple e.g.: 1. model.sele ...

  8. React之ref的高阶用法

    forwardRef转发Ref forwardRef的初衷就是解决ref不能跨层级捕获和传递的问题,forwardRef接受了父级元素标记的ref信息,并把它转发下去,使得子组件可以通过props来接 ...

  9. python mockito arg_that_编程高阶用法–开发者高频词汇

    开发者总会在开发时遇到变量命名困难或者命名冗长庸俗的时候. 阅读代码过程中遇到一些很好的命名,也遇到一些不好的. 当初并没有记录下来,之后才开始记录,有的也找不到出处了.以下高频词汇供有追求的开发者参 ...

最新文章

  1. MailKit帮助类
  2. 文档模式引起的浏览器兼容问题
  3. cmw500综合测试仪使用_高端示波器是德MSOX4154A示波器-产品使用操作说明书【二手示波器吧】...
  4. linux man命令_CentOS Linux中的man命令
  5. 简单实现仿某宝地址选择三级联动样式
  6. A股开盘:深证区块链50指数涨0.18%,概念股涨多跌少
  7. pytorch显存管理
  8. 新出版书籍《Python预测之美:数据分析与算法实战》,送书活动!参与即可机会,获得一本实体书,中奖后可填写地址寄送。
  9. PWN--collision
  10. 242.有效的字母异位词
  11. 子进程 已安装 pre-removal 脚本 返回了错误号 1或2 与 子进程 已安装 post-installation 脚本 返回了错误号 1或2
  12. Java常用开发工具有哪些?
  13. msyql数据库[云图智联]
  14. c 开发android原生程序,Android原生开发极简教程
  15. android wear 兼容问题,多款Android Wear手表不兼容iPhone 7
  16. 用 mkcert 搭建本地开发受信 HTTPS 证书环境
  17. 第三讲:如何构建双活数据中心方案
  18. 无法启动此程序因为计算机丢失msvcr110.dll,Win7运行程序提示计算机中丢失msvcr110.dll如何解决...
  19. 常见的测试用例设计方法7---因果图法
  20. 主键约束(PRIMARY KEY, PK)

热门文章

  1. JZ45,46,47,48
  2. python参考手册 第4版 修订版_Python参考手册 第4版 修订版
  3. mysql地名模糊匹配合并_调用百度地图API批量搜索地名并返回地址及坐标
  4. 安卓获取电池广播/电池状态/电池电压/电池电流的方法
  5. SPARROW架构介绍
  6. java http 上传大文件上传_java实现大文件的上传
  7. Datatables实现表格行内编辑功能
  8. 《深入浅出 node.js 笔记》 - part3
  9. 如何修改网站标题和logo
  10. 《资源成本双优化!看 Serverless 颠覆编程教育的创新实践》