0

I am setting up nginx for a website and I want it to route to 3 locations - the main frontend server, api server and to the wordpress blog server. I can get it working for the frontend server and wordpress, but the upstream api server is always giving 404 when accessing the API's via the frontend. The wordpress is running on port 8080, while the 2 NodeJS servers are running on 8015 & 8016. While hitting mysite.com frontend server on 8015 shows up the UI, but on calling the login API on port 8016 it throws 404 error. mysite.com/blog shows up the Worpress Blog after rewriting the url to mysite.com:8080

The nginx config is given:

upstream backend {
    server <IP>:8016
}

server {
   listen 80;


  server_name mysite.com;
  location / {

    root /code/public;
    index index.html
    try_files $uri $uri/ /index.html;

  }

  location /api/{
    proxy_set_header Host $http_host;
    proxy_pass http://backend/;
  }

  location /blog {
    root /var/www/html;
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php;
  }

  location ~\.php$ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://<IP>:8080;
  }

  location~/\.ht {
    deny all;
  }
}

What could be wrong here?

Dhanush Gopinath
  • 5,652
  • 6
  • 37
  • 68
  • If you go directly to :8016 does it work? If it isn't working it's not an Nginx problem, check Node. – michelem Dec 07 '15 at 07:57
  • 1
    Try to remove the final backslash `location /api {` – michelem Dec 07 '15 at 08:03
  • That Worked!! What a minor issue. I was biased by this answer here http://stackoverflow.com/questions/16157893/nginx-proxy-pass-404-error-dont-understand-why Could you please add an answer? I shall accept it :) Also why does it work that way – Dhanush Gopinath Dec 07 '15 at 08:14

1 Answers1

1

You should remove the trailing slash because /api/ it's different by /api for your Node instance:

location /api {
  proxy_set_header Host $http_host;
  proxy_pass http://backend/;
}

Also note this:

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass, then in response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended.

michelem
  • 14,430
  • 5
  • 50
  • 66