跨境派

跨境派

跨境派,专注跨境行业新闻资讯、跨境电商知识分享!

当前位置:首页 > 工具系统 > 选品工具 > Nginx实际问题解决——如何指定地址访问指定页面

Nginx实际问题解决——如何指定地址访问指定页面

时间:2024-04-12 16:15:25 来源:网络cs 作者:璐璐 栏目:选品工具 阅读:

标签: 指定  地址  访问  实际  题解 

Nginx实际问题解决——如何指定地址访问指定页面

image.png

问题复现

/var/www/dist/biographicalNotes/下面有一个Html文件 biographicalNotes.html,我实际的nginx代理是这样的

server {    listen 8080;    server_name localhost;    root /var/www/dist;    index index.html;    location / {        try_files $uri $uri/ /index.html;    }    # Allow access to static resources    location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ {        expires max;        add_header Cache-Control "public, max-age=31536000";    }​    error_page 500 502 503 504 /50x.html;    location = /50x.html {        root /var/www/dist;    }}

这段 Nginx 配置主要用于监听端口 8080,并且定义了一个基本的静态文件服务器。下面是对每个部分的解释:

listen 8080;:指定 Nginx 监听在 8080 端口上。server_name localhost;:指定该服务器块的名称,此处是 localhost。root /var/www/dist;:指定网站的根目录,这里是 /var/www/dist。所有文件的路径都是相对于这个根目录的。index index.html;:定义默认的索引文件是 index.html。如果访问一个目录时没有指定文件名,默认会尝试加载 index.htmllocation / { ... }:处理根路径 / 的请求。try_files $uri $uri/ /index.html; 表示尝试按照给定顺序查找文件,如果找不到,则返回 /index.htmllocation ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ { ... }:处理静态资源的请求,包括图片、CSS、JavaScript 和 SVG 文件。通过设置 expires max;add_header Cache-Control "public, max-age=31536000";,启用了浏览器缓存,提高页面加载速度。error_page 500 502 503 504 /50x.html;:定义错误页面的路径,当出现 500、502、503 或 504 错误时,会返回 /50x.htmllocation = /50x.html { ... }:处理 /50x.html 的请求,返回这个错误页面。

但是我现在我要求 localhost:8080/description要去访问这个页面 /var/www/dist/biographicalNotes/biographicalNotes.html,现在又该如何去解决呢?

问题解决

server {    listen 8080;    server_name localhost;​    root /var/www/dist;    index index.html;​    location / {        try_files $uri $uri/ /index.html;    }​    location /description/ {        alias /var/www/dist/biographicalNotes/;        try_files $uri $uri/ /biographicalNotes.html;​        location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ {            expires max;            add_header Cache-Control "public, max-age=31536000";        }    }​    # Allow access to static resources    location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ {        expires max;        add_header Cache-Control "public, max-age=31536000";    }​    error_page 500 502 503 504 /50x.html;    location = /50x.html {        root /var/www/dist;    }}

于是我们里面加了一段,这段配置主要是用于处理访问路径以 /description/ 开头的请求。让我们逐步解释这个配置块:

location /description/ {    alias /var/www/dist/biographicalNotes/;    try_files $uri $uri/ /biographicalNotes.html;​    location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ {        expires max;        add_header Cache-Control "public, max-age=31536000";    }}

location /description/ { ... }:

这是一个 Nginx location 块,指定了路径匹配规则,即处理以 /description/ 开头的请求。

alias /var/www/dist/biographicalNotes/;:

使用 alias 指令指定了实际文件系统路径,将请求映射到 /var/www/dist/biographicalNotes/ 目录。这意味着访问 /description/ 将被映射到 /var/www/dist/biographicalNotes/

try_files $uri $uri/ /biographicalNotes.html;:

try_files 指令用于尝试查找文件,按照给定的顺序查找,如果找不到,则按照最后一个参数的路径返回。$uri 表示当前请求的 URI。$uri/ 表示尝试查找目录,例如,如果请求是 /description/something/,则尝试查找 /var/www/dist/biographicalNotes/something/ 目录。/biographicalNotes.html 是最后一个备用路径,如果前面的尝试都失败,则返回此文件。

location ~* .(jpg|jpeg|png|gif|ico|css|js|svg)$ { ... }:

嵌套的 location 块,用于处理特定类型的静态文件,包括图片、CSS、JavaScript 和 SVG 文件。~* 表示对后面的正则表达式进行不区分大小写的匹配。.(jpg|jpeg|png|gif|ico|css|js|svg)$ 匹配以这些扩展名结尾的文件。expires max; 设置浏览器缓存过期时间为最大值。add_header Cache-Control "public, max-age=31536000"; 设置缓存控制头,使浏览器可以缓存这些静态资源。

总体而言,这段配置的目的是处理 /description/ 路径的请求,将其映射到指定的文件系统目录,并对其中的静态文件启用浏览器缓存。

本文链接:https://www.kjpai.cn/news/2024-04-12/157659.html,文章来源:网络cs,作者:璐璐,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。

文章评论