当前位置: 首页 > news >正文

Linux下nginx访问路径页面


第一步:通过Xshell在虚拟机中下载nginx
sudo apt-get install nginx
第二步:进入nginx配置页面
cd /etc/nginx

我这里创建了一个html文件夹 在进入去创建页面并且重新加载

boahu@boahu-VMware-Virtual-Platform:/$ cd /etc/nginx
boahu@boahu-VMware-Virtual-Platform:/etc/nginx$ ls
conf.d        fastcgi_params  koi-utf  mime.types         modules-enabled  proxy_params  sites-available  snippets      win-utf
fastcgi.conf  html            koi-win  modules-available  nginx.conf       scgi_params   sites-enabled    uwsgi_params
boahu@boahu-VMware-Virtual-Platform:/etc/nginx$ cd html/
boahu@boahu-VMware-Virtual-Platform:/etc/nginx/html$ ls
myindex02.html  myindex.html
boahu@boahu-VMware-Virtual-Platform:/etc/nginx/html$ sudo vi myindex03.html
boahu@boahu-VMware-Virtual-Platform:/etc/nginx/html$ ls
myindex02.html  myindex03.html  myindex.html
boahu@boahu-VMware-Virtual-Platform:/etc/nginx/html$ sudo nginx -s reload
2025/06/20 16:15:31 [notice] 14003#14003: signal process started
boahu@boahu-VMware-Virtual-Platform:/etc/nginx/html$ 

第三步配置 conf.d

boahu@boahu-VMware-Virtual-Platform:/etc/nginx/html$ cd ..
boahu@boahu-VMware-Virtual-Platform:/etc/nginx$ ls
conf.d        fastcgi_params  koi-utf  mime.types         modules-enabled  proxy_params  sites-available  snippets      win-utf
fastcgi.conf  html            koi-win  modules-available  nginx.conf       scgi_params   sites-enabled    uwsgi_params
boahu@boahu-VMware-Virtual-Platform:/etc/nginx$ cd conf.d/
boahu@boahu-VMware-Virtual-Platform:/etc/nginx/conf.d$ ls
mynginx.conf
boahu@boahu-VMware-Virtual-Platform:/etc/nginx/conf.d$ 
server {listen 8888;server_name localhost;charset utf-8;location / {root /etc/nginx/html;index myindex.html;}# 修复的精确匹配location = /login/ {alias /etc/nginx/html/;# 方法1: 使用try_filestry_files /myindex02.html =404;# 方法2: 或者使用rewrite# rewrite ^ /myindex02.html last;}# 前缀匹配location ^~ /login/ {root /etc/nginx/html;index myindex03.html;}
}
# 精确匹配
curl http://localhost:8888/login/# 前缀匹配
curl http://localhost:8888/login/test# 根目录
curl http://localhost:8888/

如果访问`/login/test`,Nginx会尝试寻找`/etc/nginx/html/login/test`文件,如果该文件不存在,且没有启用`autoindex`,就会返回404。
修复后

location ^~ /login/ {alias /etc/nginx/html/;try_files /myindex03.html =404;
}

解决方案

修改前缀匹配配置,使用 try_files 指令直接返回目标文件:

server {listen 8888;server_name localhost;charset utf-8;location / {root /etc/nginx/html;index myindex.html;}# 精确匹配/login/ → 返回myindex02.htmllocation = /login/ {alias /etc/nginx/html/;try_files /myindex02.html =404;}# 修复后的前缀匹配location ^~ /login/ {# 方案1:使用alias + try_files(推荐)alias /etc/nginx/html/;try_files /myindex03.html =404;# 方案2:或者使用root + rewrite# root /etc/nginx/html;# rewrite ^/login/.*$ /myindex03.html last;}
}
http://www.lqws.cn/news/454357.html

相关文章:

  • 【MySQL篇10】:四种分库分表详解
  • 云蝠智能大模型呼叫系统:为企业提供专业的智能客户联络
  • 第3讲、LangChain性能优化:上下文缓存与流式响应实战指南
  • 通过Radius认证服务器实现飞塔/华为防火墙二次认证:原理、实践与安全价值解析
  • django serializer __all__中 额外添加外键里的某一个属性
  • King’s LIMS 系统引领汽车检测实验室数字化转型
  • Unity3d中使用Mirror进行自定义消息通信
  • 解锁决策树:数据挖掘的智慧引擎
  • YOLO系列模型基础与实战(YOLO系列发展线)
  • YOLOv11改进 | RCS-OSA与C3k2融合架构技术详解
  • 容器技术技术入门与Docker环境部署
  • 在 CentOS 7.9 中 Node 18.20.2 安装指南
  • HTTP 请求方法与状态码
  • MySQL慢SQL优化全攻略:从诊断到调优
  • day30 导包
  • html和css实现文本打断换行、自动换行
  • postgresql DDL脚本
  • 电影感户外柔和光线人像街拍摄影后期Lr调色教程,手机滤镜PS+Lightroom预设下载!
  • 高防IP怎么选
  • 开源CMS vs 闭源CMS:二次开发究竟有何不同?
  • 工具:Autosar:DBC转ARXML
  • PLuTo 编译器示例17-20
  • 24. 开发者常用工具:抓包,弱网模拟,元素检查
  • TDengine 与开源可视化编程工具 Node-RED 集成
  • 基于微信小程序的美食点餐订餐系统
  • tkinter Text 组件学习指南
  • Python函数:全面教程
  • 【秒杀系统设计】
  • Logback 在java中的使用
  • VS2022 C#【自动化文件上传】AutoFileUpload 新需求 V13