'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 . I tried to make a rule that if there is no file on the server, then it will redirect to 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

index.php?username=username

There is this piece of code:

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


Solution 1:[1]

Use try_files to determine if the file already exists, and branch to a named location containing your rewrite rule, if it does not.

For example:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Richard Smith