当前位置: 首页 > 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;}
}

相关文章:

  • XCUITest + Swift 详细示例
  • Apache Doris 3.0.6 版本正式发布
  • 深入解析BERT:语言分类任务的革命性引擎
  • 大数据治理域——计算管理
  • Unity2D 街机风太空射击游戏 学习记录 #12环射道具的引入
  • React Native +Taro创建项目,开发Android
  • Lombok常用注解总结
  • HW蓝队工作流程
  • 为什么你的vue项目连接不到后端
  • 【机器学习实战笔记 12】集成学习:AdaBoost算法
  • Odoo 18 固定资产管理自动化指南
  • 基于深度学习的智能图像超分辨率技术:技术与实践
  • 【Python进阶系列】第10篇:Python 项目的结构设计与目录规范 —— 从脚本到模块,从混乱到整洁
  • LeetCode热题100—— 118. 杨辉三角
  • 关于ubuntu环境下vscode进行debug的随笔
  • Self-supervised Learning(BERT/GPT/T5)
  • Mac中Docker Desktop无法使用,可使用OrbStack
  • 前端页面Javascript数组
  • 25.Hybrid+安卓项目调试方法
  • Java素数筛法:BitSieve类的精妙设计
  • sns社交网站 建设文档/中国软文网官网
  • 购物类网站/新媒体运营培训班
  • 嘉兴网站备案去哪里/在线搜索引擎
  • 演示 又一个wordpress站点/百度seo营销
  • 彩妆网站建设报告/网络营销的常用方法
  • 政府网站建设风险分析/太原今日新闻最新头条