Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Created August 8, 2017 18:16
Show Gist options
  • Save paulofreitas/27b50e773f58de38c1e8c7a686fd77fb to your computer and use it in GitHub Desktop.
Save paulofreitas/27b50e773f58de38c1e8c7a686fd77fb to your computer and use it in GitHub Desktop.
Serving different API versions through custom Api-Version header with Nginx
server {
listen 80;
server_name api.domain.com;
# Set default API version
if ($http_api_version = '') {
set $http_api_version 1;
}
# Forbid unsupported API versions
if ($http_api_version ~ [^1-3]) {
return 406;
}
root /home/root/api.domain.com/v$http_api_version/public;
index index.php;
charset utf-8;
# Add headers to handle caching issues
add_header "Api-Version" $http_api_version;
add_header "Vary" "Api-Version";
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/api.domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ /\.ht {
deny all;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment