Knowledge
Server Side Includes (SSI) in nginx
#Nginx
Server Side Includes can be a very handy feature when dealing with caching or including (dynamic) files into static files. Here's how to use it and configure nginx to enable the power of SSI.
Published by Mark van Eijk on December 7, 2023 · 2 minute read
- What are Server Side Includes (SSI)
- When can SSI be useful?
- How to enable SSI in nginx
- Add nginx apt repository
- Install nginx using nginx-full
- Configure SSI
- Reload nginx service
What are Server Side Includes (SSI)
Server Side Includes (SSI) is a simple scripting language used on web servers to include content dynamically in web pages. SSI directives are embedded within HTML pages and are processed by the web server before the page is sent to the client's browser. The server executes the directives and includes the specified content in the final HTML document that is delivered to the user.
SSI is typically used for tasks such as:
- Including Content: You can include the content of one file into another. This is useful for creating reusable components or headers and footers that appear on multiple pages.
<!--#include virtual="/path/to/header.html" -->
- Date and Time Stamps: You can insert the current date and time into your web pages.
<!--#echo var="DATE_LOCAL" -->
- Conditional Statements: SSI supports simple conditional statements, allowing you to include or exclude content based on certain conditions.
<!--#if expr="${QUERY_STRING} = 1" -->
Content for query string 1.
<!--#else -->
Content for other cases.
<!--#endif -->
- Variable Setting and Displaying: You can set variables and display their values.
<!--#set var="pageTitle" value="My Page" -->
<title><!--#echo var="pageTitle" --></title>
To use SSI, your web server needs to be configured to recognize and process SSI directives. The file extension ".shtml" is often associated with SSI-enabled files, but the configuration can vary depending on the server software being used (e.g., Apache, nginx). Make sure that the server administrator has enabled SSI processing for the desired file extensions.
When can SSI be useful?
While including files into another file is a typical task for dynamic scripting languages, there are some situations that SSI is comes in handy. The following situations suit SSI very well:
- Hosting environment where scripting languages poses a security problem
- The hosting can't be configured for executing server side scripting
- When the file that needs to have includes is a static (HTML) file
- The server resources are limited; SSI is very performant at high traffic
- It is straightforward, lightweight and makes HTML a little bit dynamic
- You have no excuses anymore for an outdated copyright year number in the footer
How to enable SSI in nginx
Add nginx apt repository
echo "deb http://nginx.org/packages/ubuntu/ $(lsb_release -sc) nginx
deb-src http://nginx.org/packages/ubuntu/ $(lsb_release -sc) nginx" > /etc/apt/sources.list.d/nginx.list
sudo curl -L https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
sudo apt-get update
Install nginx using nginx-full
sudo apt install -y nginx-full
Configure SSI
Add in the location block that needs to use SSI, the following rule:
location / {
...
ssi on;
...
}
Reload nginx service
Reload the nginx service to apply the configuration changes without downtime:
sudo service nginx reload
Subscribe to our newsletter
Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!
Related articles
Configure Content Security Policy with nonce using nginx
Server Side Includes can be a very handy feature when dealing with caching or including (dynamic) files into static files. Here's how to use it and configure nginx to enable the power of SSI.
Detect Googlebot visits using nginx
Server Side Includes can be a very handy feature when dealing with caching or including (dynamic) files into static files. Here's how to use it and configure nginx to enable the power of SSI.