Make 1 root file accessible, and serve it for all requests

Jonah nginx-forum at nginx.us
Mon Oct 21 03:58:06 UTC 2013


I have a version of this working, but I suspect my solution is not the best
one.

Please suggest any improvements I can make to my conf file.  I am attempting
to do the following:

1. If any file is requested from the root, we should always serve
"index.html".  No other file should be accessible, and requesting anything
else should be treated as if you requested "index.html".  Currently I'm
using rewrite, but a redirect would be okay too, and possibly preferable.

2. Any file under "/css" or "/js" can be requested, and requesting files
from those directories that don't exist should return a 404.

Here's my current working conf file:

--- BEGIN CODE ----
    server {
      listen 80;
      server_name www.example.com;
      client_max_body_size 50M;
    
      root /var/www/mysite;
    
      location = /index.html {
      }
    
      # map everything in base dir to one file
      location ~ ^/[^/]*$ {
        rewrite ^/[^/]*$ /index.html;
      }
    
      location ~ ^/css/ {
      }
    
      location ~ ^/js/ {
      }
    }
--- END CODE ----

Thanks!
Jonah

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,243881,243881#msg-243881



More information about the nginx mailing list