Nginx proxy_pass config

I'm setting up an Angular app on one domain that needs to access an API on another. This seems to be working fine. In my services.js I have a service.factory that contains:

return $resource('/api/user/:email/:password',....

Typically this "$resource" is just another location on the same server/domain. However, with the limitations of JavaScript to cross-domain...one way to accomplish such a feat is to have the web server do the fetching.

From my reading of the Nginx resources this can be accomplished in the server config file. I believe that the list of "location" directives should go from complex to simple. I think I've tried all permutations on the following code (with and without trailing slashes in the "upstream" and the "location", various different "proxy_set_..." configurations, etc).

I just can't quite get it to work. In my error.log the web server is looking for the file and can't find it. Which is correct, as the path is actually on another (the API) server:

"2014/03/23 13:38:11 [error] 24529#0: *1 open() "/var/www/dev.mydomain.com/htdocs/api/user/d@com.com/asoetus" failed (2: No such file or directory), client:..."

I'm hoping that when Nginx sees a call to the URI dev.mydomain.com/api/user/:email/:password (that it actually goes and fetches api.saasdomain.com/api/user/:email/:password which does work and returns a JSON response. Actually it should get anything within that /api directory whether it's /user/ or /book/ or /whatever/).

There's no real error in the nginx error log, so I'm guessing that nginx isn't even listening for the proxy_pass call. ??? (There is a bind() error, but I have a number of other servers that nginx is serving just find, so I'm dismissing this bind() error for this discussion.) Ok, I just added debug to the end of the error_log directive. Here's the output to the debug nginx error.log http://freetexthost.com/30yxfss4r4

Any thoughts?

in the /etc/nginx/conf.d/virtual.conf file:


upstream api_alias {
   server api.saasdomain.com; 

server {
 listen 80;
 server_name dev.mydomain.com;
 access_log /var/www/dev.mydomain.com/logs/access.log;
 error_log /var/www/dev.mydomain.com/logs/error.log;

 location /api/ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://api_alias/;
    proxy_ssl_session_reuse off;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_buffering off;

 location / {
    root /var/www/dev.mydomain.com/htdocs/;
    index index.html;


On line 61 or your debug log you see that the request is actually being made to the remote server as GET /user/d@com.com/aoeu HTTP/1.0, you are missing the /api/. Try setting the proxy_pass directive like this:

proxy_pass http://api_alias/api/;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.