0

I need to convert an IIS rewrite rule to NGinX rewrite. I made a small progress because I don´t know how to convert parts of IIS rules. Here is my IIS config:

  • Matches the Pattern: ^(.*)$
  • Rerite URL: index.html and append Query String
  • Conditions: Match All
    • REQUEST_URI does not matches the Pattern "/api(.*)$ ignoring case"
    • REQUEST_FILENAME is not a file

So far what I did on NGinX was

location / {
    root   html;
    index  index.html index.htm;
    if ($request_uri !~ "/api(.*)$")
    {
      rewrite ^(.*)$ index.html
    }
}

I think I must use the variable $request_filename however I don´t know how to use it to filter files like on IIS and put it inside the if condition in an AND logical operation.

1

This is a typical case of front-controller pattern, and that is implemented in nginx like this:

location / {
    try_files $uri $uri/ /index.html$is_args$args;
}

This will make nginx first look for files, and if a file is not found, then it sends the request to /index.html and appends possible query arguments.

Then you specify different rules for your /api URI:

location /uri {
    ... API handling rules ...
}

Without further information this is the most I can write as an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.