大网数据广告活动
服务器运维问答

NGINX location block for subdirectory causing index page to return 404

· 52CE 我爱测

Just learning basic NGINX. I am trying to rewrite some clean URLS so they redirect to files in a subdirectory called on the server called views. The below config accomplishes this. However, whenever I return to the index page, it…

问题原文

原帖链接 · rguttersohn · CC BY-SA 4.0

Just learning basic NGINX. I am trying to rewrite some clean URLS so they redirect to files in a subdirectory called on the server called views.

The below config accomplishes this. However, whenever I return to the index page, it returns a 404 error.

As you'll see below, I define my index as index.html. I assumed this would ensure that the index.html be treated as the index file. However, it appears NGINX is opting to use the first location block to determine the index. That makes sense because '/' is the index. However, I am attempting to only use that first location block for subsequent pages (ie nginx-practice.test/secondpage)

Here's the config file:

server {
    listen 127.0.0.1:80;
    server_name nginx-practice.test;
    root /usr/robertguttersohn/Sites/nginx-practice/public;

    index index.html;

    location ~ /. {
        root /user/Sites/nginx-practice/public/views;
        try_files $uri @htmlext =404;
    }

    location @htmlext {
        rewrite ^(.*)$ $1.html last;
    } 

    access_log /usr/local/var/log/nginx/access.log;
    error_log /usr/local/var/log/nginx/error.log;

}

How do I have NGINX use index.html for the index page and then use the rewrite for all subsequent pages?

已采纳解答

原帖链接 · Pothi Kalimuthu · CC BY-SA 4.0

You could use location = / {} to target index.html file just for the home page and then use the generic location / {} block to target the subsequent pages. Here's an example...

server {
    listen 127.0.0.1:80;
    server_name nginx-practice.test;
    root /user/robertguttersohn/Sites/nginx-practice/public;

    index index.html;

    # To isolate home page
    location = / { try_files /index.html =404; }

    # To parse subsequent pages
    location / {
        root /user/robertguttersohn/Sites/nginx-practice/public/views;
        try_files $uri @url.html =404;
    }

    access_log /usr/local/var/log/nginx/access.log;
    error_log /usr/local/var/log/nginx/error.log;

}

As you can see, the location @htmlext {} block can be eliminated too.

In my example, I have used /user/robertguttersohn/Sites/nginx-practice/public as root directory and /user/robertguttersohn/Sites/nginx-practice/public/views as sub-directory. You may want to update it to fit your environment. Please remember to restart Nginx server after making any change in configuration.

For more info on how location works, please checkout https://nginx.org/r/location .

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-03-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。