简体   繁体   English

如何使 nginx 重定向

[英]how to make nginx redirect

I'm trying to do the following: There is a site where you can find a user in the format http://localhost/username .我正在尝试执行以下操作:有一个站点,您可以在其中找到格式http://localhost/username的用户。 I tried to make a rule that if there is no file on the server, then it will redirect to index.php?username=username .我试图制定一个规则,如果服务器上没有文件,那么它将重定向到index.php?username=username But the problem is that it redirects everything, even js and css. I want to do it like this, if the file was on the server, then it would access it, and if not, then it would make a request但问题是它重定向了所有东西,甚至是 js 和 css。我想这样做,如果文件在服务器上,那么它会访问它,如果不在,那么它会发出请求

index.php?username=username

There is this piece of code:有这样一段代码:

rewrite ^/(.+)$ /index.php?type=userinfo&username=$1 last;

Use try_files to determine if the file already exists, and branch to a named location containing your rewrite rule, if it does not.使用try_files确定文件是否已经存在,如果不存在,则分支到包含重写规则的命名位置

For example:例如:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^/(.+)$ /index.php?type=userinfo&username=$1 last;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM