Yes, if you have multiple virtual hosts in Nginx and want to avoid repeating the same configurations (such as matching Referer headers or other common settings), you can use include files or shared configuration templates to organize and unify your configuration. This way, you avoid duplicating configuration across multiple server blocks for different domain names.
Methods to Simplify Common Configuration:
1️⃣ Reusing Configuration with include Files:
You can write reusable configurations (like the map directive for Referer checking) into a separate file and include it in all your virtual host configurations.
For example:
Shared Configuration File
Create a file, e.g., /etc/nginx/common/referer_protection.conf:
# Define the allowed referrer logic
map $http_referer $deny_access {
default 1;
# Allow Referer from the current domain (replace with your domain pattern)
"~*^https?://(www\.)?yourdomain\.tld($|/)" 0;
}
# Logic to return 403 if access is denied
if ($deny_access) {
return 403;
}
Main Nginx Configuration
For each virtual host, include the common file in the /api location:
server {
listen 80;
server_name domain1.com;
location /api {
include /etc/nginx/common/referer_protection.conf;
# Put your API configuration here
proxy_pass http://backend_api;
}
}
server {
listen 80;
server_name domain2.com;
location /api {
include /etc/nginx/common/referer_protection.conf;
# Put your API configuration here
proxy_pass http://another_backend_api;
}
}
This way, the map and if blocks for Referer checking are defined only once, in the referer_protection.conf file, and can be reused across all virtual hosts.
2️⃣ Wildcard Matching for Domains:
If your virtual hosts are similar (e.g., you serve multiple subdomains or sibling domains), you can generalize the map logic to work for a broad range of websites. For example, if all your domains follow the *.example.com structure:
map $http_referer $deny_access {
default 1;
# Allow Referer from any subdomain of example.com
"~*^https?://([a-z0-9-]+\.)?example\.com($|/)" 0;
}
This map block allows requests for any subdomain of example.com (e.g., www.example.com, api.example.com, etc.) without having to specify your server names individually.
3️⃣ Using a Unified Server Block for Multiple Domains:
If your websites are exactly the same for each domain and you just want to host multiple domains under the same configuration, you can define them all in a single server block:
server {
listen 80;
server_name domain1.com domain2.com domain3.com;
location /api {
include /etc/nginx/common/referer_protection.conf;
# Put your API configuration here
proxy_pass http://backend_api;
}
}
Here, server_name accepts all your domain names (domain1.com, domain2.com, etc.), and they share the same location and configuration.
4️⃣ Using Variables for Domain-Specific Configuration:
If each domain has unique configurations but shares a common template, you can define variables for domain-specific logic.
For example:
server {
listen 80;
server_name domain1.com domain2.com domain3.com;
# Define the referrer dynamically based on the current server name
set $allowed_referer_pattern "~*^https?://(www\.)?$host($|/)";
location /api {
map $http_referer $deny_access {
default 1;
$allowed_referer_pattern 0;
}
if ($deny_access) {
return 403;
}
# Put your API configuration here
proxy_pass http://backend_api;
}
}
In this example, $host dynamically refers to the current server name, making it possible to determine the Referer dynamically without hardcoding a specific domain.
5️⃣ Catch-All and Generalized Server Block:
If you want a catch-all configuration for many domains with the same logic, you can define a wildcard server_name and validate the Referer to allow requests only from valid domains.
Example:
server {
listen 80;
server_name _;
location /api {
map $http_referer $deny_access {
default 1;
"~*^https?://(www\.)?(valid-domain1\.com|valid-domain2\.com|another-allowed-domain\.com)($|/)" 0;
}
if ($deny_access) {
return 403;
}
# Your API proxy here
proxy_pass http://backend_api;
}
}
Here, server_name _ acts as a "catch-all server" for any domain, but only allows requests with specific Referer headers listed explicitly in the map.
Final Thoughts:
include files are the most practical way to avoid repeating yourself in Nginx configurations if you have a lot of shared logic across virtual hosts.
- Use wildcards (e.g., for subdomains) or template variables (e.g.,
$host) if many virtual hosts share common patterns.
- Remember to test your setup carefully to ensure the configurations work as expected for each virtual host.
Let me know if you need any clarifications or further assistance! ✨