Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Last active August 8, 2017 18:48
Show Gist options
  • Save paulofreitas/f44713ef4b51bc22f3cd9e834c997148 to your computer and use it in GitHub Desktop.
Save paulofreitas/f44713ef4b51bc22f3cd9e834c997148 to your computer and use it in GitHub Desktop.
Serving different API versions through content negotiation with Nginx
map $http_accept $api_version {
default 1;
"~application/vnd\.namespace\.v1(\+json)?" 1;
"~application/vnd\.namespace\.v2(\+json)?" 2;
"~application/vnd\.namespace\.v3(\+json)?" 3;
}
server {
listen 80;
server_name api.domain.com;
root /home/root/api.domain.com/v$api_version/public;
index index.php;
charset utf-8;
# Add header to expose the API version being consumed
add_header "Api-Version" $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