Home > Net >  Calling API endpoint inside docker container
Calling API endpoint inside docker container

Time:01-19

I have a small express application running inside a docker container. The endpoint is accessible locally through http://localhost:8888/api/run . The docker container was run using this command:

docker run -dp 8888:8888 code-editor

I configured NGINX to serve the response from docker using the location block:

server {

    server_name www.baseURL.tech baseURL.tech;
      -------------------CONNECT WITH APP INSIDE DOCKER--------------------
    location /compiler {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8888/;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

     -------------------CONNECT WITH MAIN NODE APP--------------------
    location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8000/;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

The path being called is https://baseURL/compiler/api/run as an ajax request from the main website https://baseURL but it is returning 404.

CodePudding user response:

You have

location /compiler

which results in Nginx passing on the entire URL, i.e. compiler/api/run to the Express app. You want it to remove the compiler part and the easiest way to do that is to add a slash at the end of the location, like this

location /compiler/

Then Nginx will only pass on api/run to Express.

  • Related