Dmitri
Dmitri

Reputation: 36280

How to redirect all request to same file in nginx?

My root directory

/web/app/src

In this directory I have 2 directories /js/ and /assets/ and one file index.html

This is what I need to achieve:

Any request to /js/ or /assets/ or /index.html just serve files from root directory For example myapp.com/js/app.js servers app.js from /web/app/src/js/ directory Same with requests to /assets/

But all other requests, any other uri should result in serving index.html

For example

myapp.com/bla/bla/q?param=x Should serve index.html from web root directory

All rewrites should be internal, no http 301 redirects.

Upvotes: 4

Views: 9628

Answers (1)

Satys
Satys

Reputation: 2425

Sir, please try below code. :)

server {
   ...
   root /web/app/src;
   ...
   location / {
      try_files $uri $uri/ /index.html;
   }
}

Upvotes: 15

Related Questions