With jwilder nginx-proxy, how to proxy a subdirectory url into a specific container?

I am using jwilder / nginx-proxy to create a reverse proxy. I am trying to redirect http://localhost:8000/apito a specific php service.

Directory structure:

.
+-- docker-compose.yml
+-- nginx
+-- nodejs
|   +-- index.js
|   +-- …
+-- php
|   +-- api

docker-compose.yml:

version: "3.1"

services:

  nginx-proxy:
    image: jwilder/nginx-proxy:alpine
    ports:
      - "8000:80"
    volumes:
      - ./php:/srv/www
      - /var/run/docker.sock:/tmp/docker.sock:ro

  nodejs:
    image: node:alpine
    environment: 
      - NODE_ENV=production
      - VIRTUAL_HOST=localhost
      - VIRTUAL_PORT=8080
    expose:
      - "8080"
    working_dir: /home/app
    restart: always
    volumes:
      - ./nodejs:/home/app
    command: ["node", "index.js"]

  php:
    image: php:apache
    environment:
      - VIRTUAL_HOST=localhost
    volumes:
      - ./php:/var/www/html

This works great for a service nodejs.

Now I would like to redirect calls to http://localhost:8000/apithe service php. I assume that I need to add to nginx confsomething like:

server {
  location /api {
    proxy_pass http://php:80/api;
    proxy_set_header Host $host;
  }
}

This function is not built into lib . So how can I achieve this?

+3
source share
1 answer

- php- :

  php:
    image: php:apache
    environment:
      - VIRTUAL_HOST=api.localhost
    volumes:
      - ./php:/var/www/html

:

curl -H 'Host: api.localhost' http://localhost:8000/api

. , , DNS /etc/hosts :

127.0.0.1 api.localhost

, :

curl -H http://api.localhost:8000/api

, , node:

var request = require('request')

var formData = {}

request({
    headers: {
      'Host': 'api.localhost'
    },
    uri: 'http://localhost:8080',
    method: 'POST'
  }, function (err, res, body) {
      console.log("it works")
      console.log(res)
    }
)

/etc/hosts, (DNS-). node.

+2

Source: https://habr.com/ru/post/1680919/


All Articles