Created
August 17, 2011 21:24
-
-
Save j-mcnally/1152677 to your computer and use it in GitHub Desktop.
Nginx for API and Webapp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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