15

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

3 Answers 3

18

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.

5
  • This is clearly more simple (on the other hand it does not mask other existing files in the directory).
    – Marek Rost
    Apr 14, 2017 at 9:18
  • 2
    Very true, but there are usually resource files that need to be pulled in by index.html which is why I presented the common solution first. Apr 14, 2017 at 9:27
  • What if I want the existing files to be also rewritten/redirected to /index.html?
    – Qian Chen
    Jan 12, 2019 at 10:31
  • 2
    @ElgsQianChen use try_files /index.html =404; or rewrite ^ /index.html break;. But without other changes, it will break access to your resource files (JS. CSS, IMG). Jan 12, 2019 at 10:47
  • This is actually a better solution to the OP's question, just for the sake of the assets being properly loaded with the correct MIME type.
    – gumol
    May 30, 2019 at 13:04
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).

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.

1
  • This is the one which worked for me. Jul 12, 2021 at 21:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .