Skip to content

Instantly share code, notes, and snippets.

@j-mcnally
Created August 17, 2011 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save j-mcnally/1152677 to your computer and use it in GitHub Desktop.
Save j-mcnally/1152677 to your computer and use it in GitHub Desktop.
Nginx for API and Webapp
So you want to build a HTML Webapp and then query your JSON Api. To avoid cross domain calls use NGINX to proxy only certain requests to the API. In my case I use the Accept: header from the HTTP request to specify return data format. If that format is application/json send it to the api, otherwise try to serve the static html.
upstream app_cluster_1 {
server 127.0.0.1:1337;
}
server {
listen 80;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/apps/something/html;
index index.html index.htm;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
if ($http_accept = 'application/json') {
proxy_pass http://app_cluster_1;
}
proxy_redirect off;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment