Nginx静态资源部署之响应内容部署(二)

简介: Nginx静态资源部署之响应内容部署

Nginx静态资源部署之响应内容部署(一)https://developer.aliyun.com/article/1392035


= : 用于不包含正则表达式的uri前,必须与指定的模式精确匹配

server {
  listen 80;
  server_name 127.0.0.1;
  location =/abc{
    default_type text/plain;
    return 200 "access success";
  }
}
可以匹配到
http://192.168.200.133/abc
http://192.168.200.133/abc?p1=TOM
匹配不到
http://192.168.200.133/abc/
http://192.168.200.133/abcdef

~ : 用于表示当前uri中包含了正则表达式,并且区分大小写

~*: 用于表示当前uri中包含了正则表达式,并且不区分大小写

换句话说,如果uri包含了正则表达式,需要用上述两个符合来标识

server {
  listen 80;
  server_name 127.0.0.1;
  location ~^/abc\w${
    default_type text/plain;
    return 200 "access success";
  }
}
server {
  listen 80;
  server_name 127.0.0.1;
  location ~*^/abc\w${
    default_type text/plain;
    return 200 "access success";
  }
}

^~: 用于不包含正则表达式的uri前,功能和不加符号的一致,唯一不同的是,如果模式匹配,那么就停止搜索其他模式了。

server {
  listen 80;
  server_name 127.0.0.1;
  location ^~/abc{
    default_type text/plain;
    return 200 "access success";
  }
}

设置请求资源的目录root / alias

root:设置请求的根目录

语法 root path;
默认值 root html;
位置 http、server、location

path为Nginx服务器接收到请求以后查找资源的根目录路径。

alias:用来更改location的URI

语法 alias path;
默认值
位置 location

path为修改后的根路径。

以上两个指令都可以来指定访问资源的路径,那么这两者之间的区别是什么?

举例说明:

(1)在/usr/local/nginx/html目录下创建一个 images目录,并在目录下放入一张图片mv.png图片

location /images {
  root /usr/local/nginx/html;
}

访问图片的路径为:

http://192.168.200.133/images/mv.png

(2)如果把root改为alias

location /images {
  alias /usr/local/nginx/html;
}

再次访问上述地址,页面会出现404的错误,查看错误日志会发现是因为地址不对,所以验证了:

root的处理结果是: root路径+location路径
/usr/local/nginx/html/images/mv.png
alias的处理结果是:使用alias路径替换location路径
/usr/local/nginx/html/images

需要在alias后面路径改为

location /images {
  alias /usr/local/nginx/html/images;
}

(3)如果location路径是以/结尾,则alias也必须是以/结尾,root没有要求

将上述配置修改为

location /images/ {
  alias /usr/local/nginx/html/images;
}

访问就会出问题,查看错误日志还是路径不对,所以需要把alias后面加上 /

小结:

root的处理结果是: root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的含义。
如果location路径是以/结尾,则alias也必须是以/结尾,root没有要求

index指令

index:设置网站的默认首页

语法 index file …;
默认值 index index.html;
位置 http、server、location

index后面可以跟多个设置,如果访问的时候没有指定具体访问的资源,则会依次进行查找,找到第一个为止。

举例说明:

location / {
  root /usr/local/nginx/html;
  index index.html index.htm;
}
访问该location的时候,可以通过 http://ip:port/,地址后面如果不添加任何内容,则默认依次访问index.html和index.htm,找到第一个来进行返回

error_page指令

error_page:设置网站的错误页面

语法 error_page code … [=[response]] uri;
默认值
位置 http、server、location…

当出现对应的响应code后,如何来处理。

举例说明:

(1)可以指定具体跳转的地址

server {
  error_page 404 http://www.itcast.cn;
}

(2)可以指定重定向地址

server{
  error_page 404 /50x.html;
  error_page 500 502 503 504 /50x.html;
  location =/50x.html{
    root html;
  }
}

(3)使用location的@符合完成错误信息展示

server{
  error_page 404 @jump_to_error;
  location @jump_to_error {
    default_type text/plain;
    return 404 'Not Found Page...';
  }
}

可选项=[response]的作用是用来将相应代码更改为另外一个

server{
  error_page 404 =200 /50x.html;
  location =/50x.html{
    root html;
  }
}

这样的话,当返回404找不到对应的资源的时候,在浏览器上可以看到,最终返回的状态码是200,这块需要注意下,编写error_page后面的内容,404后面需要加空格,200前面不能加空格

相关文章
|
3月前
|
Kubernetes 应用服务中间件 nginx
百度搜索:蓝易云【使用Kubernetes部署Nginx应用教程】
现在,你已经成功在Kubernetes集群上部署了Nginx应用。通过访问Service的外部IP地址,你可以访问Nginx服务。
42 4
|
28天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
110 0
|
5天前
|
JavaScript 前端开发 应用服务中间件
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
13 0
|
4月前
|
应用服务中间件 Linux 网络安全
windows+linux环境下nginx部署环境
windows+linux环境下nginx部署环境
|
2天前
|
运维 Serverless 应用服务中间件
Serverless 应用引擎产品使用之在阿里云Serverless中函数计算FC nginx 部署上去之后放置静态页面如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
25 0
|
13天前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
29天前
|
应用服务中间件 nginx Windows
windows下Nginx+RTMP部署
windows下Nginx+RTMP部署
21 0
|
2月前
|
应用服务中间件 Linux nginx
web后端-linux-nginx-1.18操作命令和部署
web后端-linux-nginx-1.18操作命令和部署
|
3月前
|
应用服务中间件 Shell nginx
【Docker】Docker安装Nginx配置静态资源
【Docker】Docker安装Nginx配置静态资源
76 0
|
4月前
|
应用服务中间件 Linux 网络安全
【Nginx】Nginx Linux 部署
【1月更文挑战第25天】【Nginx】Nginx Linux 部署