CentOS 7.4 Tengine安装配置详解三

九、根据HTTP响应状态码自定义错误页:

1、未配置前访问一个不存在的页面http://192.168.1.222/abc/def.html,按F12后刷新页面

2、server{}配置段中新增如下location

server {

listen 80;

server_name localhost;

location /bbs {

root /vhosts/bbs;

error_page 404 404/404.html;

}

}

3、创建目录,并上传自定义错误页:# mkdir -pv /vhosts/bbs/bbs/404

4、重载服务:# Nginx -t # Nginx -s reload # ss -tunlp | grep :80

5、访问测试页:

http://192.168.1.222/abc.html

http://192.168.1.222/bbs/abc.html --> 自动跳转http://192.168.1.222/bbs/404/404.html

F12后刷新页面


十、基于fancy实现索引目录(适用于下载站):

1、下载ngx-fancyindex

# yum -y install git

# cd /tmp

# git clone https://github.com/aperezdc/ngx-fancyindex.git ngx-fancyindex

2、使用Tenginedso_tool工具编译第三方模块:

# dso_tool -h

# dso_tool --add-module=/tmp/ngx-fancyindex

# ls /usr/local/tengine/modules | grep fancyindex --> ngx_http_fancyindex_module.so

3、events{}配置段后新增如下代码

dso {

load ngx_http_fancyindex_module.so;

4、location /software {

root /download;

fancyindex on; # 启用fancy目录索引功能

fancyindex_exact_size off; # 不显示文件的精确大小,而是四舍五入,单位是KBMBGB

fancyindex_localtime on; # 认为off显示的是GMT时间,改为on显示的是服务器时间

5、创建目录:# mkdir -pv /download/software,并上传软件

6、重载服务:7、检查ngx_http_fancyindex_module模块是否已经装载:

# Nginx -m --> ngx_http_fancyindex_module (shared,3.1)

8、访问测试页:http://192.168.1.222/software


十一、装载echo模块:

echo-Nginx-module

# git clone https://github.com/openresty/echo-Nginx-module.git

# dso_tool --add-module=/tmp/echo-Nginx-module

# ls /usr/local/tengine/modules | grep echo --> ngx_http_echo_module.so

load ngx_http_echo_module.so;

server_name www.qiuyue.com;

access_log /usr/local/tengine/logs/www.qiuyue.com-access.log main;

location /echo {

default_type "text/plain"; # 不指定default_type,会提示下载文件,而非直接输出在浏览器中

echo "host --> $host"; # 请求主机头字段,否则为服务器名称

echo "server_name --> $server_name"; # 服务器名称

echo "server_addr --> $server_addr"; # 服务器IP地址

echo "remote_addr --> $remote_addr"; # 客户端echo "uri --> $uri"; # 不带请求参数的当前URI$uri不包含主机名

echo "document_uri --> $document_uri"; # $uri含义相同

echo "request_uri --> $request_uri"; # 带请求参数的原始URI,不包含主机名

echo "request_filename --> $request_filename"; # 当前请求的文件路径

echo "document_root --> $document_root"; # 当前请求在root指令中指定的值

5、重载服务:6、检查ngx_http_echo_module模块是否已经装载:

# Nginx -m --> ngx_http_echo_module (shared,serif;">7、修改本地Windows 10系统的hosts文件

C:\Windows\System32\drivers\etc\hosts,末尾新增代码192.168.1.222 www.qiuyue.com

http://www.qiuyue.com/echo


十二、location的匹配顺序(匹配顺序与在配置文件中定义的顺序无关):

1、方便演示效果,装载echo模块

default_type "text/plain";

location = / {

# 精确匹配,匹配优先级最高,匹配成功则不再向下继续匹配

echo 1;

}

location / {

# 通用匹配,匹配优先级最低,匹配所有请求

# 如果有更长的同类型的表达式,则优先匹配更长的表达式

# 如果有正则表达式可以匹配,则优先匹配正则表达式

echo 2;

location /documents/ {

# 匹配所有以/documents/开头的请求

echo 3;

location ^~ /static/ {

/static/开头的请求,匹配优先级第2高,匹配成功则不再向下继续匹配

echo 4;

location ~ \.(txt|css)$ {

txtcss结尾的请求,区分字符大小写的正则匹配

echo 5;

location ~* \.(txt|css)$ {

css结尾的请求,不区分字符大小写的正则匹配

echo 6;

3、重载服务:4、修改本地192.168.1.222 www.qiuyue.com

5、访问测试页:

http://www.qiuyue.com

http://www.qiuyue.com/static/1.js

http://www.qiuyue.com/static/1.txt

http://www.qiuyue.com/documents/1.txt

http://www.qiuyue.com/documents/1.html

http://www.qiuyue.com/notes/1.txt

http://www.qiuyue.com/notes/1.TXT

http://www.qiuyue.com/notes/1.chm

匹配优先级说明:=^~~~*/path//

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native