API versioning using Wildcard Sub-domains
-
26-06-2021 - |
Question
I don't want to add any versioning logic to the API. If I use Wildcard Sub-domains and regex in nginx.conf to determine subdomain part, I can route the request to specific directory. Here is the config:
server {
listen 80;
server_name .api.domain.com;
set $version $host;
if ($version ~ "^(.+).api.domain.com") {
set $version $1;
}
access_log /var/log/nginx/$version-access.log;
error_log /var/log/nginx/$version-error.log info;
location / {
root /opt/webapps/app_$version/www/;
index index.php;
}
}
Question: Is there any disadvantage of such technique?
Solution
I found better way - named captures in server_name:
server {
server_name ~^(?<version>.+).test$;
root /your-path/$version.test;
}
This is method is better because:
- IfIsEvil
- It is much more cleaner
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow