16

How can I get nginx to always serve index.html no matter the URL?

For example, I want the URL https://www.example.me/723738 to always serve https://www.example.me/index.html

I don't want to use a re-direct, because the user must still have the original URL they typed in their address bar.

I changing Nginx config to re-direct 404's to index.html, but I don't think thats a great way of doing it because it will return a 404 response.

In future I may want to have the index.html display the original URL that the user accessed, is there a way to do this?

Thanks

Cherona
  • 443

3 Answers3

19

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.

3

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).

Marek Rost
  • 2,136
  • 12
  • 16
0

This is the code you're looking for

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/;
index index.html;

location = /favicon.ico { access_log off; log_not_found off; }

error_page 404 =200 /;
}

With server_name _; he listen from any ip, domain.