Linux下nginx访问路径页面

发布于:2025-06-21 ⋅ 阅读:(21) ⋅ 点赞:(0)


第一步:通过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_files
        try_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.html
    location = /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;
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到