Nginx location 配置 - Part 2

接上文
链接: Nginx 简介和入门 - part1

上文 我们简单地在 nginx 创建了3个虚拟主机, 虽然这个3个主机都是用占用80端口

但是我们可以用不同的域名来实现区分访问3台虚拟主机。

但是, 实际项目上, 我们更加多地会使用location 配置而不是用 域名区分。 本文的重点就是location 配置。




先准备1个网站模板

这次我们直接从github 下载
github 地址:
https://github.com/LinkedInLearning/learning-nginx-2492317.git

gateman@tf-vpc0-subnet0-main-server:~/learning-nginx$ pwd
/home/gateman/learning-nginx
gateman@tf-vpc0-subnet0-main-server:~/learning-nginx$ git clone https://github.com/LinkedInLearning/learning-nginx-2492317.git



解压
gateman@tf-vpc0-subnet0-main-server:~/learning-nginx/learning-nginx-2492317$ pwd
/home/gateman/learning-nginx/learning-nginx-2492317
gateman@tf-vpc0-subnet0-main-server:~/learning-nginx/learning-nginx-2492317$ sudo mkdir -p /var/www/binaryville/
gateman@tf-vpc0-subnet0-main-server:~/learning-nginx/learning-nginx-2492317$ sudo tar -xf Binaryville_robot_website_LIL_107684.tgz --directory /var/www/binaryville/

上面的命令直接把文件解压在了/var/www/binaryVille/ 这个文件夹

gateman@tf-vpc0-subnet0-main-server:~/learning-nginx/learning-nginx-2492317$ cd /var/www/binaryville
gateman@tf-vpc0-subnet0-main-server:/var/www/binaryville$ ls -l
total 100
-rw-r--r--  1 root root  1140 Aug  5  2022 403.html
-rw-r--r--  1 root root  1193 Jul 29  2022 404.html
-rw-r--r--  1 root root  1208 Jul 29  2022 50x.html
-rw-r--r--  1 root root   648 Aug 19  2020 README.rtf
drwxrwxr-x 38 root root  4096 Aug  3  2020 _style-guide
drwxrwxr-x 16 root root  4096 Aug  3  2020 about
drwxrwxr-x  2 root root  4096 Aug  3  2020 account
drwxrwxr-x  2 root root  4096 Aug  3  2020 assets
drwxrwxr-x  8 root root  4096 Aug  3  2020 blog
drwxrwxr-x  2 root root  4096 Aug  3  2020 cart
drwxrwxr-x  4 root root  4096 Aug  3  2020 checkout
drwxrwxr-x  2 root root  4096 Aug  3  2020 contact
-rw-rw-r--  1 root root    45 Aug  3  2020 humans.txt
drwxrwxr-x  7 root root  4096 Aug  3  2020 images
-rw-rw-r--  1 root root 29987 Jul 29  2022 index.html
drwxr-xr-x  2 root root  4096 Aug  5  2022 private
-rw-rw-r--  1 root root    25 Aug  3  2020 robots.txt
drwxrwxr-x  3 root root  4096 Aug  3  2020 shop



然后在/etc/nginx/conf.d 准备1个for这个网站的配置文件
gateman@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d$ cat binaryville.conf 
server {listen 8081;server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;index index.html index.htm index.php;root  /var/www/binaryville;
}

注意这次用感到是8081 端口, 如果防火墙没打开这个端口的记得打开


重启 nginx 服务
nginx -t
sudo systemctl reload nginx



访问配置好的网站

http://jp-gcp-vms.xyz:8081/
在这里插入图片描述

好了接下来我们会尝试更新配置文件, 使用location 配置去更新这个网站的

  1. Site root
  2. Images
  3. Error Pages



配置Site root (网站根目录)

我们先检查下 网站目录 /var/www/binaryVille 的内容

gateman@tf-vpc0-subnet0-main-server:/var/www/binaryville$ ls -l
total 100
-rw-r--r--  1 root root  1140 Aug  5  2022 403.html
-rw-r--r--  1 root root  1193 Jul 29  2022 404.html
-rw-r--r--  1 root root  1208 Jul 29  2022 50x.html
-rw-r--r--  1 root root   648 Aug 19  2020 README.rtf
drwxrwxr-x 38 root root  4096 Aug  3  2020 _style-guide
drwxrwxr-x 16 root root  4096 Aug  3  2020 about
drwxrwxr-x  2 root root  4096 Aug  3  2020 account
drwxrwxr-x  2 root root  4096 Aug  3  2020 assets
drwxrwxr-x  8 root root  4096 Aug  3  2020 blog
drwxrwxr-x  2 root root  4096 Aug  3  2020 cart
drwxrwxr-x  4 root root  4096 Aug  3  2020 checkout
drwxrwxr-x  2 root root  4096 Aug  3  2020 contact
-rw-rw-r--  1 root root    45 Aug  3  2020 humans.txt
drwxrwxr-x  7 root root  4096 Aug  3  2020 images
-rw-rw-r--  1 root root 29987 Jul 29  2022 index.html
drwxr-xr-x  2 root root  4096 Aug  5  2022 private
-rw-rw-r--  1 root root    25 Aug  3  2020 robots.txt
drwxrwxr-x  3 root root  4096 Aug  3  2020 shop
gateman@tf-vpc0-subnet0-main-server:/var/www/binaryville$ ls images
accepted-cards.svg  characters  empty-cart.svg  home-hero-characters.png  logo.svg                  placeholder-371x217.jpg  placeholder-708x590.jpg  placeholder-800x800.jpg      product-montage.png
blog                contact     favicon         icons.svg                 placeholder-1184x360.jpg  placeholder-535x535.jpg  placeholder-720x480.jpg  product-montage-cropped.png  products
gateman@tf-vpc0-subnet0-main-server:/var/www/binaryville$ 

我们在 /etc/nginx/conf.d/binaryVille.conf 添加下面代码

	    location /site {# First attempt to serve a request as a file, # then as directory, then fall back to display a 404try_files $uri $uri/ =404;}

这时的配置文件内容

root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# cat binaryville.conf
server {listen 8081;server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;index index.html index.htm index.php;root  /var/www/binaryville;location / {# First attempt to serve a request as a file, then as directory, then fall back to display a 404try_files $uri $uri/ =404 ;}
}
root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# 

这段配置代码的意思是,

当访问/ url时, 例如 /abc
nginx 首先会把abc 作为 / 里的1个abc文件处理(通常是浏览器下载)
如果/var/www/binaryVille/ 没有abc这个文件 则尝试处理 /abc/ 当作文件夹
还没有, 返回404

注意 这段代码也是 nginx的默认处理逻辑,不加上也是这样的

这时我们尝试访问 /images
在这里插入图片描述

为什么是403 而不是404

首先, /var/www/binaryVille 里面没有images 的文件, 但是有images 文件夹

所以nginx 就当成/images/来处理了

但是images/下虽然有很多 图片文件, 但是默认下是不会显示文件列表的, 而且里面没有index.html 文件, 所以就显示403了

但是我们直接访问images 下某个图片文件, (前提是知道文件名)
是可以访问的
在这里插入图片描述

如果这时我们在images 添加1个index.html

root@tf-vpc0-subnet0-main-server:/var/www/binaryville/images# cat index.html 
images folder!
root@tf-vpc0-subnet0-main-server:/var/www/binaryville/images# 

再刷新下网页!
就能显示网页内容了
在这里插入图片描述




配置images 目录

这时我们再增加一段代码

		location /images {# Allow the contents of /images folder to be listedautoindex on;}

这时 配置文件内容

root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# cat binaryville.conf
server {listen 8081;server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;index index.html index.htm index.php;root  /var/www/binaryville;location / {# First attempt to serve a request as a file, then as directory, then fall back to display a 404try_files $uri $uri/ =404 ;}location /images {# Allow the contents of /images folder to be listedautoindex on;}
}

当我们为 /images folder 打开 autoindex on时 , 我们刷新下/images/网页, 见到不再是

403 了
而是文件可以被列出
在这里插入图片描述





配置覆盖

如果 location / 的配置 和 location /images 的配置有冲突会怎么样
答案是 访问 非 /images 的资源时会依照 / 的配置
而访问 /images 资源时 按照 /images 的配置

例如下面的配置文件

root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# cat binaryville.conf
server {listen 8081;server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;index index.html index.htm index.php;root  /var/www/binaryville;location / {# First attempt to serve a request as a file, then as directory, then fall back to display a 404try_files $uri $uri/ =404;}location /images {# Allow the contents of /images folder to be listedautoindex on;try_files $uri $uri/ =504;}
}

location / 找不到资源时返回404
location /images 找不到资源时返回504

测试
当访问 /abcxx 时 页面是404
在这里插入图片描述
访问/images/abcxx 页面是504了
在这里插入图片描述




配置404 错误页面

上面的404 页面是 nginx 默认的
而网站目录下有自定义的404.html, 如何让404 出错的页面指向它呢, 还是location 配置

我们增加下面1段配置代码

	    # specify the page to server for 404 errorserror_page 404 /404.html;

很容易理解
我们再访问 http://jp-gcp-vms.xyz:8081/abcxx
的确能显示自定义的404.html 页面了
在这里插入图片描述
这时我们直接访问
http://jp-gcp-vms.xyz:8081/404.html

也会一样显示上面的页面的
如何禁止用户直接在浏览器访问 http://jp-gcp-vms.xyz:8081/404.html?

加上下面这段代码

        # an exact map location for the 404 pagelocation = /404.html {# only for intenal requestsinternal;} 

也不难理解。。
但是for 这个case, 再访问 http://jp-gcp-vms.xyz:8081/404.html 还是会显示自定义的404 页面,
因为
访问 404.html -> 简直访问 -> return 404 error -> 还是调回404.html…





配置500 错误页面

我们再加上下面3段配置

        # specify the page to server for 50x errorserror_page 500 502 503 504 /50x.html;# an exact map location for the 404 pagelocation = /50x.html {# only for intenal requestsinternal;} # a location to demostrated 500 errorslocation /500 {fastcgi_pass unix:/this/will/fail;}

其实与404的配置差不多, 小小区别:

  1. 用error_page 覆盖了多个error 到同1个页面50x.html
  2. 最后构造了1个 /500 的location 用于测试, fastcgi_pass unix:/this/will/fail 这个命令肯定会出错的

我们尝试访问 http://jp-gcp-vms.xyz:8081/500
在这里插入图片描述
就是我们想要的!

出错日志: /var/log/nginx/error.log

gateman@tf-vpc0-subnet0-main-server:/var/log/nginx$ tail -n 10 error.log
2024/01/07 16:26:16 [error] 195693#195693: *1771 open() "/var/www/binaryville1/.env" failed (2: No such file or directory), client: 140.82.15.131, server: 34.39.2.90, request: "GET /.env HTTP/1.1", host: "34.39.2.90"
2024/01/07 16:29:27 [warn] 213106#213106: conflicting server name "jp-gcp-vms.xyz" on 0.0.0.0:80, ignored
2024/01/07 16:29:27 [warn] 213106#213106: conflicting server name "www.jp-gcp-vms.xyz" on 0.0.0.0:80, ignored
2024/01/07 16:29:46 [warn] 213449#213449: conflicting server name "jp-gcp-vms.xyz" on 0.0.0.0:80, ignored
2024/01/07 16:29:46 [warn] 213449#213449: conflicting server name "www.jp-gcp-vms.xyz" on 0.0.0.0:80, ignored
2024/01/07 16:29:46 [notice] 213449#213449: signal process started
2024/01/07 16:32:53 [crit] 213450#213450: *1772 connect() to unix:/this/will/fail failed (2: No such file or directory) while connecting to upstream, client: 103.151.172.31, server: jp-gcp-vms.xyz, request: "GET /500 HTTP/1.1", upstream: "fastcgi://unix:/this/will/fail:", host: "jp-gcp-vms.xyz:8081"
2024/01/07 16:33:21 [error] 213450#213450: *1774 open() "/var/www/binaryville1/.env" failed (2: No such file or directory), client: 154.47.20.17, server: 34.39.2.90, request: "GET /.env HTTP/1.1", host: "34.39.2.90"
2024/01/07 16:34:15 [crit] 213450#213450: *1775 connect() to unix:/this/will/fail failed (2: No such file or directory) while connecting to upstream, client: 103.151.172.31, server: jp-gcp-vms.xyz, request: "GET /500 HTTP/1.1", upstream: "fastcgi://unix:/this/will/fail:", host: "jp-gcp-vms.xyz:8081"
2024/01/07 16:34:20 [crit] 213450#213450: *1775 connect() to unix:/this/will/fail failed (2: No such file or directory) while connecting to upstream, client: 103.151.172.31, server: jp-gcp-vms.xyz, request: "GET /500 HTTP/1.1", upstream: "fastcgi://unix:/this/will/fail:", host: "jp-gcp-vms.xyz:8081"
gateman@tf-vpc0-subnet0-main-server:/var/log/nginx$ 

这时我们直接访问
http://jp-gcp-vms.xyz:8081/50x.html
在这里插入图片描述
这时第2段的internal 生效了, 显示的是404页面而不是50x的页面
访问 50x.html -> 不允许外部访问 -> return 404 -> return 404.html





location 配置中一些通配符规则

这时, 配置文件的内容如下:

root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# cat binaryville.conf
server {listen 8081;server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;index index.html index.htm index.php;root  /var/www/binaryville;location / {# First attempt to serve a request as a file, then as directory, then fall back to display a 404try_files $uri $uri/ =404;}location /images {# Allow the contents of /images folder to be listedautoindex on;try_files $uri $uri/ =504;}# specify the page to server for 404 errorserror_page 404 /404.html;# an exact map location for the 404 pagelocation = /404.html {# only for intenal requestsinternal;} # specify the page to server for 50x errorserror_page 500 502 503 504 /50x.html;# an exact map location for the 404 pagelocation = /50x.html {# only for intenal requestsinternal;} # a location to demostrated 500 errorslocation /500 {fastcgi_pass unix:/this/will/fail;}
}

可以简单 location 的配置中有些有 = 号 有些没有
区别是什么呢

其实

location /500 这种写法代表perfix 匹配
下面的location 都是符合规则的
/500 /5001 /500/abc … 总之 500开头的都可以

location = /500 代表精确匹配
只有 /500 才能匹配

还是其他的匹配规则, 参考如下

modifierAplication to Location Definitions
Noneprefix
=exact match
~a case-senitive regular expression
~*a case-insensitive regular expression
^~if the longest prefix maches then no regular expression is checked

1 ~ 4 规则都很简单, 值得注意是最后1个

^~是Nginx配置中用于指定前缀匹配的特殊符号,可以与location块一起使用。

当^~前缀与location块一起使用时,它表示如果请求的URL以指定的前缀匹配,那么该location块将被选择用于处理请求,而不再继续寻找其他更具体的匹配。

下面是对^~符号的使用示例:

location ^~ /images/ {# 处理以 /images/ 开头的请求# ...
}

在上述示例中,当请求的URL以 /images/ 开头时,它将被匹配到该location块,并使用该块中的配置来处理请求。这个前缀匹配具有比通用的正则表达式匹配更高的优先级。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/609041.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

机器学习-线性回归实践

目标:使用Sklearn、numpy模块实现展现数据预处理、线性拟合、得到拟合模型,展现预测值与目标值,展现梯度下降; 一、导入模块 import numpy as np np.set_printoptions(precision2) from sklearn.linear_model import LinearRegr…

JVM主要的几种垃圾回收算法

1、Java 为什么要实现自动内存管理 ? 简化开发过程:通过内存自动管理可以避免手动分配和释放内存的麻烦,减少了内存泄漏和内存错误的风险,让研发能更专注于业务逻辑,不必纠结于内存管理的细节。 提高开发效率&#xff…

vite 搭建vue3 TS项目初始框架

目录 仓库地址: 一.搭建项目 1.安装 Vite: 2.创建 Vue 3 项目: 3.进入项目目录: 4.安装依赖: 5.运行项目: 6.流程实操 二.修改项目结构,显示自定义的页面 1.整理静态样式文件 1.1.在 sr…

Dell 机架式服务器 - 高级定制服务

Dell 机架式服务器 - 高级定制服务 1. Dell Technologies2. 机架式服务器 - 高级定制服务2.1. Servers & Storage (服务器及存储) -> Servers2.2. Rack Servers (机架式服务器)2.3. Shop2.4. PowerEdge Rack Servers (PowerEdge 机架式服务器)2.5. PowerEdge R760 Rack …

编译原理期末大题步骤——例题

一、预测分析方法步骤 提取左公因子,消除左递归判断文法是否为LL(1)文法若是,构造预测分析表;否则,不能进行分析。根据预测分析表对输入串进行分析 例子: 文法G[E]: …

mysql进阶-视图

目录 1. 用途 2. 语法 2.1 创建或替换视图 2.2 修改视图 2.3 查看视图: 2.4 删除视图: 3. 其他 3.1 操作视图 3.2 迁移数据库 1. 用途 视图可以理解为一个复杂查询的简称,它可以帮助我们简化查询,主要用于报表查询:例如…

DataFrame详解

清洗相关的API 清洗相关的API: 1.去重API: dropDupilcates 2.删除缺失值API: dropna 3.替换缺失值API: fillna 去重API: dropDupilcates dropDuplicates(subset):删除重复数据 1.用来删除重复数据,如果没有指定参数subset,比对行中所有字段内容,如果全部相同,则认为是重复数据,…

Hyperledger Fabric 自动发现网络信息 discover 工具使用

客户端要往 Fabric 网络中发送请求,首先需要知道网络的相关信息,如网络中成员组织信息、背书节点的地址、链码安装信息等。 在 Fabric v1.2.0 版本之前,这些信息需要调用者手动指定,容易出错;另外,当网络中…

SPL-cmcRVFL+

吐槽 作者未提供代码,还有图1敢再糊点吗?

简单的MOV转MP4方法

1.下载腾讯的QQ影音播放器, 此播放器为绿色视频播放器, 除了播放下载好的视频外没有臃肿无用功能 官网 QQ影音 百度网盘链接:https://pan.baidu.com/s/1G0kSC-844FtRfqGnIoMALA 提取码:dh4w 2.用QQ影音打开MOV文件 3.右下角打开影音工具箱 , 选择截取…

softmax详解

在神经网络中,Softmax 是一个用于多类别分类的激活函数。给定一个包含原始分数(未经处理的模型输出)的向量,Softmax 将这些分数转化为表示概率分布的向量。具体而言,对于给定的原始分数向量 ( z ),Softmax …

Java中什么序列化?

在Java中,序列化是一种将对象转换为字节序列的机制,使得对象可以在网络上传输或存储到文件中,而后可以通过反序列化还原为对象。Java提供了java.io.Serializable接口,通过实现这个接口的类可以实现对象的序列化和反序列化。 序列…

Noisy DQN 跑 CartPole-v1

gym 0.26.1 CartPole-v1 NoisyNet DQN NoisyNet 就是把原来Linear里的w/b 换成 mu sigma * epsilon, 这是一种非常简单的方法,但是可以显著提升DQN的表现。 和之前最原始的DQN相比就是改了两个地方,一个是Linear改成了NoisyLinear,另外一个是在agent在t…

基于 SpringBoot + vue 的医院管理系统(含源码,数据库,文档)

基于 SpringBoot vue 的医院管理系统 †前后端分离思想,这个系统简直太棒了!屯 光这个系统采用了 前后端分离思想,后端使用 SpringBoot和 SpringMVC框架,让代码更高效,更易于维护。前端则使用了 vue js 和ElementU…

mybatis plus相同Id与xml配置错误时,mybatis plus解决逻辑

前言 处理做项目的问题,其中不乏奇奇怪怪的问题,其中mybatis plus的问题感觉有点隐蔽,有些是运行时出现,有些是运行到具体的逻辑触发,对于应用的状态监控提出了极大的挑战,应用的状态由健康检查接口提供&a…

【基础工具篇使用】Windows环境下瑞芯微开发工具的安装和使用

文章目录 Rockchip 烧录驱动的安装Rockchip 烧录工具使用导入配置MASKROM 模式烧录LOADER 模式烧录Update.img 包的烧录 Rockchip 烧录驱动的安装 瑞芯微提供了 RKDevTool 上位机烧录工具,此工具只能在 Windows 系统下运行,运行前要先安装驱动文件 Ro…

mitmproxy代理抓包使用mock数据

第一步 安装Python环境 下载Python环境安装包https://www.python.org/getit/https://link.jianshu.com/?thttps%3A%2F%2Fwww.python.org%2Fgetit%2F (图a) 安装Python的时候勾选“Add Python 3.5 to PATH”选项(图a) 打开CMD命…

Pytest接口自动化测试框架搭建

一. 背景 Pytest目前已经成为Python系自动化测试必学必备的一个框架,网上也有很多的文章讲述相关的知识。最近自己也抽时间梳理了一份pytest接口自动化测试框架,因此准备写文章记录一下,做到尽量简单通俗易懂,当然前提是基本的py…

书生·浦语大模型实战营第二次课堂笔记

文章目录 什么是大模型?pip,conda换源模型下载 什么是大模型? 人工智能领域中参数数量巨大、拥有庞大计算能力和参数规模的模型 特点及应用: 利用大量数据进行训练拥有数十亿甚至数千亿个参数模型在各种任务重展现出惊人的性能 …

数据结构入门到入土——链表(完)LinkedList

目录 一,双向链表 1.单向链表的缺点 2.什么是双向链表? 3.自主实现双向链表 接口实现: 二,LinkedList 1.LinkedList的使用 1.1 什么是LinkedList? 1.2 LinkedList的使用 1.LinkedList的构造 2.LinkedList的…