Skip to content

Serving static files or a static HTML site

Gruxi can serve static files or a static HTML site. This is useful for hosting a simple website or serving assets for a dynamic site.

This is where Gruxi shines and outperforms many other web servers. It is designed to be fast and efficient, and it can serve static files with very low latency, even at extremely high concurrency.

Setting up Gruxi to serve static files is simple. Just specify the directory where your static files are located, and Gruxi will take care of the rest.

Let’s look at an example of how to set up Gruxi to serve static files efficiently.

Example: Static HTML site

We assume you have a directory that contains your static files and you want to serve them at the root URL (/). You can set up Gruxi to serve the static files from that directory as follows:

  1. Go to the Gruxi admin portal and open the configuration.

  2. Add a Site and then add a Static File Handler processor under Processors.

  3. Configure the common settings, such as hostnames, access logging, and TLS.

  4. Configure the Static File Handler processor to point to the directory where your static files are located. For example, if your static files are located in a directory called my-site under Gruxi, set the Root Directory field to ./my-site. This can also point to the absolute location of the files, such as /var/www/html/my-siteLinux or C:\my-siteWindows, if you do not want it to be relative to the Gruxi executable.

  5. Set the URL pattern to * to match all requests.

  6. Add an Index Files entry, such as index.html, to serve the default file when a directory is requested. This allows, for example, a request to /my-subdir/ to serve the index.html file in /my-subdir/index.html.

INFO

Some static sites may require URL rewriting to support client-side routing. If your static site uses client-side routing (for example, a single-page application), you can add a Rewrite Function such as OnlyWebRootIndexForSubdirs. This allows the client-side router to handle routing logic. It means that a request to /about (if it does not exist on the filesystem) will serve the index.html file in the web root, allowing the client-side router to render the appropriate content.

Example: Serving static files

Static files can be served in a similar way to a static HTML site.

The most common use case is to serve static files for an entire website, such as CSS, JavaScript, and images. In this case, set the URL pattern to * to match all requests, and configure the Static File Handler processor to point to the directory where your static files are located.

Alternatively, you can serve static files under a specific URL path, such as /static/*. In this case, set the URL pattern to /static/*, and configure the Static File Handler processor to point to the directory ./my-static-files where your static files are located. This way, a request to /static/css/style.css will serve the file located at ./my-static-files/css/style.css.

When you only want to serve files that actually exist and do not want to serve index files for missing paths, leave the Index Files field empty. This way, a request to /static/css/style.css will return a 404 Not Found response if the file does not exist.

Tips for optimal performance

  1. Always use the latest version of Gruxi, as it includes performance improvements and bug fixes.
  2. Enable the file cache in the global Core settings. This greatly improves performance for static file serving by allowing Gruxi to cache frequently accessed files in memory, reducing disk I/O and improving response times.
  3. Enable compression in the global Core settings to reduce the size of files sent over the network, which can improve load times for clients. When enabled together with file caching, Gruxi will automatically cache the compressed versions of files, further improving performance.
  4. Enable HTTP Caching, which helps clients cache static files and reduces the number of requests to the server. Gruxi will add headers to responses to enable client-side caching, which can significantly improve performance for repeat visitors.