Nginx – How to Redirect Dynamic Page URLs

nginxredirect

I need to change nginx config in a way , that every dynamically generated request is being redirected to main domain.
I'm not sure how to achieve this. Help please!
Not sure if this matters, but I want to add, that traffic goes thru nginx reveser proxy (ssl) to simple web server (nginx)

https://mypage.com/something ->https://mypage.com
https://mypage.com/anything123 ->https://mypage.com
https://mypage.com/randomtext ->https://mypage.com

Best Answer

You could have two location blocks, one for exact match of root directory and the other one for everything else.

location = / {
   try_files /index.html = 404;
}

location / {
   return 301 /;
}

rewrite also can be used instead of return (redirect), but will be slower.

    location / {
       rewrite ^ / permanent;
    }

Another option is just to have a rewrite for everything (inside server block), without any location blocks. In this method, url will remain as the original url you typed on the browser.

server {
    root /xxx/xxx/xx;
    server_name mypage.com;
    rewrite ^.*$ /index.html;
}