Get Nginx to always serve index

Solution 1:

The common pattern uses try_files with a default URI. For a minimalist example:

server {
    root /path/to/root;
    location / {
        try_files $uri $uri/ /index.html;
    }
}

See this document for more.

Solution 2:

Answer to question 1:

The function you're looking for is called URL rewriting. This allows you to create masks (or "fake" URLs) which show resource that is located on different URL.

In Nginx this is achieved by rewrite <regexp-pattern> <target-url> command in configuration file. Here is Nginx configuration for domain www.example.com:

server {
    listen 80;
    server_name www.example.com;

    root /var/www/example.com;
    index index.html;

    rewrite ^.*$ /index.html;
}

The <regexp-pattern> (REGular EXPression) part is compared to url you've typed into browser - if the match is successful, resource at <target-url> is shown.

Answer to question 2:

The current URL cannot be shown with pure HTML document only. You will need to use server-side scripting language - for example PHP. This will allow you to display dynamic content to the user. There is inexhaustible supply of guides on PHP with Nginx (https://askubuntu.com/a/134676) and on the topic of how to display current URL from PHP (https://stackoverflow.com/a/6768831).