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

半城伤御伤魂 提交于 2019-12-04 08:47:31

One approach is to give the php app its own virtual host:

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

Then, it will be accesible as this:

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

Setting the header can be achieved in practically any language. Or to avoid setting custom header, you can add that DNS to your /etc/hosts file:

127.0.0.1 api.localhost

So you can curl as this:

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

This is how set custom headers, for example in 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)
    }
)

But, I recommend you to go for the /etc/hosts approach, that should has an equivalent in production environment (a DNS server). So you don't need to touch node code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!