Redirect nginx简单uri通配符
我正试图做一些我认为很简单的事情,但结果却给我带来了很多麻烦 我在nginx下有一个vhost定义如下Redirect nginx简单uri通配符,redirect,path,nginx,Redirect,Path,Nginx,我正试图做一些我认为很简单的事情,但结果却给我带来了很多麻烦 我在nginx下有一个vhost定义如下 server { listen 80; server_name test.com; root /var/apps/failover/; location / { index index.html index.htm; } } 我想做的是,所有具有请求uri集(例如:test.com/someth
server {
listen 80;
server_name test.com;
root /var/apps/failover/;
location / {
index index.html index.htm;
}
}
我想做的是,所有具有请求uri集(例如:test.com/something)的请求将只转到test.com/
我想要一种uri通配符,它将用户重定向到vhost的根目录
我尝试了许多重写语句的组合,但没有任何效果
我相信这应该很简单,但我不能让它工作。如果我理解你的话,你希望所有到你网站的请求都由某种
索引文件提供,是吗?我想这是真的,因为如果任何请求将被重写为只有域名,没有任何额外的路径,那么我不知道你还能想到什么
所以,在这样的假设下,还有一个问题:你想用301
http状态代码作为永久重定向(用户会知道)重定向用户,还是只在你的应用程序中进行软重定向而不需要任何后台信息(用户不会知道)
如果是第一个,则此重写指令应起作用,请将其粘贴到任何位置
块之前:
rewrite /.+ / permanent ;
rewrite ^/.* / last;
如果是第二个,则此重写
指令应起作用,将其粘贴到任何位置
块之前:
rewrite /.+ / permanent ;
rewrite ^/.* / last;
您还需要一个位置
块,它将为路径/
location / {
try_files index.html =404;
}
有了这个location
和try_files
指令,我们可以说,无论你的应用程序出现什么问题,都无关紧要,因为它总是要使用我们的index.html
文件,或者返回http状态代码404
(未找到错误)
它对你有帮助吗?还是你在寻找不同的东西?我终于用这样的东西实现了:
server {
listen 80;
server_name text.com;
root /var/apps/failover/;
location / {
index index.html;
error_page 404 =200 /index.html;
}
}
不确定这是否是最好的方法,但效果很好,可以满足我的需要我尝试了两种选择:第一种情况下,我得到重定向循环,第二种情况下,我得到重写或内部重定向循环,同时处理“/”(500响应,我可以在nginx日志中看到这一点)服务器{listen 80;server_name test.com;root/var/apps/failover/;rewrite/+/permanent;location/{index index.html index.htm;}}但是您是否使用了指令try_files index.html=404
?