只是学习基本的NGINX。我正在尝试重写一些干净的URLS,以便它们重定向到一个名为views的子目录中的文件。
下面的配置实现了这一点。但是,每当我返回索引页时,它都会返回404错误。
如下所示,我将我的索引定义为index.html。我认为这将确保将index.html作为索引文件处理。然而,NGINX似乎选择使用第一个位置块来确定索引。这是有意义的,因为'/‘是索引。但是,我试图只对后续页面使用第一个位置块(即nginx- am . that /第二页)。
下面是配置文件:
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;
}
如何让NGINX对索引页使用index.html,然后对所有后续页面使用重写?
发布于 2023-03-04 03:11:52
您可以使用location = / {}
只针对主页的index.html文件,然后使用泛型location / {}
块来锁定后续页面。举个例子..。
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;
}
如您所见,location @htmlext {}
块也可以消除。
在我的例子中,我使用/user/robertguttersohn/Sites/nginx-practice/public
作为根目录,/user/robertguttersohn/Sites/nginx-practice/public/views
作为子目录。您可能需要更新它以适应您的环境。请记住在进行任何配置更改后重新启动Nginx服务器。
有关位置如何工作的更多信息,请查看https://nginx.org/r/location。
https://serverfault.com/questions/1124059
复制相似问题