• Nginx静态资源部署之响应内容部署


    Nginx静态资源概述

    Nginx处理静态资源的内容,我们需要考虑下面这几个问题:

    (1)静态资源的配置指令
    (2)静态资源的配置优化
    (3)静态资源的压缩配置指令
    (4)静态资源的缓存处理
    (5)静态资源的访问控制,包括跨域问题和防盗链问题
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Nginx静态资源的配置指令

    listen指令

    listen:用来配置监听端口。

    语法listen address[:port] [default_server]…;
    listen port [default_server]…;
    默认值listen *:80 | *:8000
    位置server

    default_server属性是标识符,用来将此虚拟主机设置成默认主机。所谓的默认主机指的是如果没有匹配到对应的address:port,则会默认执行的。如果不指定默认使用的是第一个server。

    server{
    	listen 8080;
    	server_name 127.0.0.1;
    	location /{
    		root html;
    		index index.html;
    	}
    }
    server{
    	listen 8080 default_server;
    	server_name localhost;
    	default_type text/plain;
    	return 444 'This is a error request';
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    server_name指令

    server_name:用来设置虚拟主机服务名称。

    127.0.0.1 、 localhost 、域名[www.baidu.com | www.jd.com]

    语法server_name name …;
    name可以提供多个中间用空格分隔
    默认值server_name “”;
    位置server

    关于server_name的配置方式有三种,分别是:

    精确匹配
    通配符匹配
    正则表达式匹配
    
    • 1
    • 2
    • 3

    配置方式一:精确匹配

    如:

    server {
    	listen 80;
    	server_name www.onenewcode.cn;
    	...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    补充小知识点:

    hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统会再将网址提交DNS域名解析服务器进行IP地址的解析。
    
    • 1

    因为域名是要收取一定的费用,所以我们可以使用修改hosts文件来制作一些虚拟域名来使用。需要修改 /etc/hosts文件来添加

    vim /etc/hosts
    127.0.0.1 www.onenewcode.cn
    
    • 1
    • 2

    配置方式二:使用通配符配置

    server_name中支持通配符"*",但需要注意的是通配符不能出现在域名的中间,只能出现在首段或尾段,如:

    server {
    	listen 80;
    	server_name  *.onenewcode.cn;
    	# www.onenewcode.cn abc.onenewcode.cn
    	...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    **配置三:**使用正则表达式配置

    server{
            listen 80;
            server_name ~^www\.(\w+)\.com$;
            default_type text/plain;
            return 200 $1  $2 ..;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    匹配执行顺序
    No1:准确匹配server_name
    
    No2:通配符在开始时匹配server_name成功
    
    No3:通配符在结束时匹配server_name成功
    
    No4:正则表达式匹配server_name成功
    
    No5:被默认的default_server处理,如果没有指定默认找第一个server
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    location指令

    server{
    	listen 80;
    	server_name localhost;
    	location / {
    	
    	}
    	location /abc{
    	
    	}
    	...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    location:用来设置请求的URI

    语法location [ = | ~ | ~* | ^~ |@ ] uri{…}
    默认值
    位置server,location

    uri变量是待匹配的请求字符串,可以不包含正则表达式,也可以包含正则表达式,那么nginx服务器在搜索匹配location的时候,是先使用不包含正则表达式进行匹配,找到一个匹配度最高的一个,然后在通过包含正则表达式的进行匹配,如果能匹配到直接访问,匹配不到,就使用刚才匹配度最高的那个location来处理请求。

    属性介绍:
    不带符号,要求必须以指定模式开始

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    = : 用于不包含正则表达式的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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ~ : 用于表示当前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";
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

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

    server {
    	listen 80;
    	server_name 127.0.0.1;
    	location ^~/abc{
    		default_type text/plain;
    		return 200 "access success";
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    设置请求资源的目录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;
    }
    
    • 1
    • 2
    • 3

    访问图片的路径为:

    http://192.168.200.133/images/mv.png
    
    • 1

    (2)如果把root改为alias

    location /images {
    	alias /usr/local/nginx/html;
    }
    
    • 1
    • 2
    • 3

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

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

    需要在alias后面路径改为

    location /images {
    	alias /usr/local/nginx/html/images;
    }
    
    • 1
    • 2
    • 3

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

    将上述配置修改为

    location /images/ {
    	alias /usr/local/nginx/html/images;
    }
    
    • 1
    • 2
    • 3

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

    小结:

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

    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,找到第一个来进行返回
    
    • 1
    • 2
    • 3
    • 4
    • 5

    error_page指令

    error_page:设置网站的错误页面

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

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

    举例说明:

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

    server {
    	error_page 404 http://www.itcast.cn;
    }
    
    • 1
    • 2
    • 3

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

    server{
    	error_page 404 /50x.html;
    	error_page 500 502 503 504 /50x.html;
    	location =/50x.html{
    		root html;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

    server{
    	error_page 404 @jump_to_error;
    	location @jump_to_error {
    		default_type text/plain;
    		return 404 'Not Found Page...';
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

    server{
    	error_page 404 =200 /50x.html;
    	location =/50x.html{
    		root html;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

  • 相关阅读:
    基于springboot+vue社区疫情防控系统
    12个Python自动化办公的官方文档,中文版只有2个?
    1100*B. Maximum Rounding(贪心)
    数字IC手撕代码-XX公司笔试真题(数据流pipeline加和)
    ios ipa包上传需要什么工具
    Minecraft 1.12.2 彩色渐变字体 模组发布
    React——react 的基本使用
    数据结构与算法--分治策略
    46道史上最全Redis面试题,面试官能问的都被我找到了(含答案)
    uoj#751-[UNR #6]神隐【交互】
  • 原文地址:https://blog.csdn.net/studycodeday/article/details/134374199