skip to Main Content

I have the following nginx config:

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name ce.mysite.com;

  location /api/ {
    resolver 127.0.0.11 valid=30s;
    set $api ce-api;
    proxy_pass http://$api:3100/;
  }

}

On the oter end of ce-api (docker container), I have a nodejs server running listening on port 3100. This is working and I can curl http://localhost:3100/auth/ping inside the container and get a valid response back.

However, if I access my site via ce.mysite.com/api/auth/ping. I get a 404. Adding some logging into the container to check the route, it would appear that auth/ping is missing from the route.

My understand was that using trailing slashes on the route and the proxy should mean that everything after api will be forwarded (in fact, I have a similar setup working where the nodejs server isn’t inside a docker container).

Any idea what’s happening?

Note: The nodejs server is an express server running on PM2.

2

Answers


  1. check the network driver the container of app using.

    I’m not sure but as per my knowledge I got from you, I can also doubt that the issue might be related to how the proxy_pass directive is configured in your nginx setup.

    In your case, since you’re accessing ce.mysite.com/api/auth/ping, nginx will remove /api/ before passing the request to the backend server, which means the backend server will receive a request for /auth/ping. If your backend server is expecting requests with /api/ prefix, it won’t find the route /auth/ping and hence return a 404.

    location /api/ {
        resolver 127.0.0.11 valid=30s;
        set $api ce-api;
        proxy_pass http://$api:3100/api/;
    }
    

    or try

    location /api/ {
        resolver 127.0.0.11 valid=30s;
        set $api ce-api;
        rewrite ^/api(/.*)$ $1 break;
        proxy_pass http://$api:3100;
      
    

    in the second configuration, the rewrite directive is used to capture the part of the URI that comes after /api/ and pass it to the proxy without including the /api part. This way, the request to ce.mysite.com/api/auth/ping should correctly be forwarded to http://ce-api:3100/auth/ping.

    save then run syntax error test by nginx

    sudo nginx -t 
    

    if everything is good, restart nginx to get the new configurations:

    sudo service nginx reload
    
    Login or Signup to reply.
  2. That case, I think you need samples for Nginx proxy and zero-downtime deployment method.

    What about referring to some samples like this?

    https://github.com/Andrew-Kang-G/docker-blue-green-runner

    • Use Docker & Docker-Compose
    • With your project and its only Dockerfile, Docker-Blue-Green-Runner handles the rest of the Continuous Deployment (CD) process with Consul. Nginx allows your project to be deployed without experiencing any downtime.
    • Consul & Registrator supported but they doesn’t affect the availability of Nginx.
    • Consider careful exceptional cases
    • Available on one server machine
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search