Search code examples
apinginxdocker-composereverse-proxyproxypass

Docker-compose: Nginx proxy_pass my api


I am trying proxy my api to my client with nginx, everything is dockerized.

I got my client container which is a simple angular2 client hosted with nginx linked to my api, my api container linked to my mongo container.

The problem is my localhost:80/api is a 404 everytime.

I must be missing something....

Here is my nginx.conf

  server {
        listen       80;


    # location /api/ {
    #     proxy_pass http://api:3000/;
    #     proxy_redirect http://api:3000/ http://localhost:80/api/;
    #     proxy_set_header Host $host;
    # }

    # location ~ /api/(?<section>.+) {
    #     proxy_pass http://api:3000/api/$section;
    #     proxy_set_header Host $host;
    # }
        location /api {
            proxy_pass         http://api:3000/api/;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }

Here is my docker-compose

services:
  mongo:
    image: mongo
    ports:
      - "27017:27017"
  api:
    build: ./server
    ports:
      - "3000"
    volumes:
      - .:/server
    depends_on:
      - mongo
  web:
    build: ./client
    ports: 
      - "80:80"
    volumes:
      - .:/client
    depends_on:
      - api

Solution

  • I workarounded it by doing a node.js reverse proxy instead of nginx