您现在的位置是:主页 > news > 网站建设 石景山/网络站点推广的方法

网站建设 石景山/网络站点推广的方法

admin2025/6/13 19:33:14news

简介网站建设 石景山,网络站点推广的方法,网站备案号填写,wordpress评论邮件通知nginx的location配置 使用的版本是nginx1.18.0。 nginx各个版本差别不大,可以通用。 简介 nginx的location配置是为了让不同的url访问指向不同的资源文件位置,例如下面的配置: server {listen 8080;server_name localhost;#chars…

网站建设 石景山,网络站点推广的方法,网站备案号填写,wordpress评论邮件通知nginx的location配置 使用的版本是nginx1.18.0。 nginx各个版本差别不大,可以通用。 简介 nginx的location配置是为了让不同的url访问指向不同的资源文件位置,例如下面的配置: server {listen 8080;server_name localhost;#chars…

nginx的location配置

使用的版本是nginx1.18.0。

nginx各个版本差别不大,可以通用。

简介

nginx的location配置是为了让不同的url访问指向不同的资源文件位置,例如下面的配置:

server {listen       8080;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root html;index  index.html index.htm;}location /testpages {alias html/test;index aliastest.html;}location /ourimg/{alias html/love/img/;}location /games/{root html;index index.html;}location /games/killthegoblin {root html;index  index.html index.htm;}error_page  404     /errorpages/404.html;location = /errorpages/404.html {root   html;}

location :后面的参数(有各种参数)会匹配url后面的路径,然后实现目录的指向。
root :参数指定的是根目录,即这个url访问的根目录,location中的目录必须存在这个根目录里面。
alias :参数指定资源的绝对目录。
index :参数指定访问该url时的默认页面。


root和alias指定资源文件路径

nginx指定文件资源路径有两种方式:rootalias

root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器的目录文件上。

  • root会直接拼接,root的目录拼接location的目录。
  • alias丢弃匹配后再拼接,alias的目录会拼接 不含匹配的location目录之后的路径。

详细解释如下:

root

location后的路径相当于是root后面路径的具体路径,root后为根路径,location后为具体路径,用户看见的是具体路径,看不到根路径。

当访问站点localhost:8080,后面没有跟着其他路径

实际访问的就是html文件夹下的文件,因为我这设置了index主页,所以请求的路径为html/index.html

这个和直接访问localhost:8080/index.html是一样的,请求路径也是html/index.html

location / {root html;index  index.html index.htm;
}

在这里插入图片描述


也可以访问html下的其他文件

访问localhost:8080/test.html

这个文件在html目录下,所以能直接访问。

请求的路径为html/test.html
在这里插入图片描述


访问localhost:8080/games/killthegoblin/
实际路径是html/games/killthegoblin/index.html
在这里插入图片描述

alias

location后的路径相当于是alias后面路径的一个别名,是用户能够看见的一个虚拟的路径。

location /testpages {alias html/test;index aliastest.html;
}
location /ourimg/{alias html/love/img/;
}

上面的代码我有两种写法,一种带斜杠 / ,另外一种不带斜杠 / ,因为很多博客都说一定要带 / ,所以我就试了下,发现其实是都可以的

前提是要么location和alias都带,要么就都不带斜杠。如果最后你们测试发现无法访问,最好重启一下nginx,我测试了一个早上,无数次重启得出的结论…


我尝试给alias 指定路径后设置了主页发现也是可以的。

location /testpages {alias html/test;index aliastest.html;
}

访问localhost:8080/testpages,直接显示设置的默认主页,请求的路径是html/test/aliastest.html
在这里插入图片描述


访问localhost:8080/testpages/aliastest.html,和上面是一样的,请求的路径是html/test/aliastest.html
在这里插入图片描述


也可以访问test下的其他文件。

访问localhost:8080/testpages/roottest.html,请求的路径是html/test/roottest.html

在这里插入图片描述


location /ourimg/{alias html/love/img/;
}

访问localhost:8080/ourimg/testimg.png,请求的路径是html/love/img/testimg.png
在这里插入图片描述

index主页设置

  • 该指令后面可以跟多个文件,用空格隔开;
  • 如果包括多个文件,Nginx会根据文件的枚举顺序来检查,直到查找的文件存在;
  • 文件可以是相对路径也可以是绝对路径,绝对路径需要放在最后;
  • 文件可以使用变量$来命名;

该指令拥有默认值,index index.html ,意思就是即使你没有显式的给出index,也会有隐式默认的index,且值为index.html

在访问localhost:8080/games,虽然没有指定html文件,但是默认返回设定的index.html文件。
在这里插入图片描述

location匹配优先级顺序

1.精确匹配(=)

  `=` 前缀指令匹配,如果匹配成功,则停止其他匹配

2.一般匹配(^~)

普通字符串指令匹配,顺序是从长到短,
匹配成功的location如果使用^~,则停止其他匹配(正则匹配)`一般匹配就是加了符号的通用匹配,使得其优先级在正则匹配之前。`

3.正则匹配(~ , *~)

正则表达式指令匹配,按照配置文件里的顺序,成功就停止其他匹配

4.通用匹配

默认匹配是除了一般匹配之外的情况
如果第三步中有匹配成功,则使用该结果,否则使用第二步结果

精确匹配和一般匹配

location = /helloworld/ { #1return 601;}location  ^~ /helloworld/ { #2return 602;}  

返回601,精确匹配。
**加粗样式**


当路径后还有其他参数,则会进行一般匹配,返回602.
在这里插入图片描述

精确匹配,路径必须一模一样,否则会进行其他匹配。


一般匹配和正则匹配

        location ^~ /helloworld { #1 return 601;} #location /helloworld { #2 # 	return 602;#}location ~ /helloworld { return 603;} 

返回601,为一般匹配。在这里插入图片描述

正则匹配和通用匹配

        # location ^~ /helloworld { #1 #     return 601;# } location /helloworld { #2 return 602;}location ~ /helloworld { return 603;} 

返回603,为正则匹配。
在这里插入图片描述

一般匹配和通用匹配

        location ^~ /helloworld { #1 return 601;} location /helloworld { #2 return 602;}# location ~ /helloworld { #     return 603;# } 

这样会报错nginx: [emerg] duplicate location "/helloworld"
原因上面也有讲述,两个匹配其实是一样的,只是优先级不一样,所以重复定义了。

通用匹配的长度优先

        location /helloworld/ {                #2return 602;}location /helloworld/test/ {        #1return 601;}

返回601,匹配的是较长的/helloworld/test/
在这里插入图片描述
当访问http://localhost:8080/helloworld/a.html,匹配的是 /helloworld/ ,返回602。

通用匹配的优先级是和其长度有关,优先匹配较长的结果,和顺序无关。

一般匹配和通用匹配应该类似。


正则匹配的顺序优先

        location /helloworld/test/ { #1return 601;}location ~ /helloworld { #2return 602;}location ~ /helloworld/test { #3return 603;}

返回602,匹配的是#2的代码。
在这里插入图片描述


交换了正则匹配#2 #3的顺序

        location /helloworld/test/ { #1return 601;}location ~ /helloworld/test { #3return 603;}location ~ /helloworld { #2return 602;}     

返回的是603,匹配#3。
在这里插入图片描述

说明正则匹配的优先是和顺序有关的,和长度无关。