I use jwilder/nginx-proxy to make a reverse proxy. I try to redirect http://localhost:8000/api
to a specific php service.
The 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 fine for the nodejs
service.
Now, I would like to redirect calls to http://localhost:8000/api
to the php
service. I imagine that I have to add to the nginx conf
something like:
server {
location /api {
proxy_pass http://php:80/api;
proxy_set_header Host $host;
}
}
This feature is not built into the lib. So, how can I achieve that?
One approach is to give the php app its own virtual host:
Then, it will be accesible as this:
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:So you can curl as this:
This is how set custom headers, for example in node:
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.