简体   繁体   中英

nginx upstream configuration always returning 404

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM