问题
i am new to nginx and I am not sure is this normal behavior...
Here is the lib I am using: https://github.com/jwilder/nginx-proxy
I will explain here what I trying to accomplish...
I have 2 additional services service1
and service2
those services are simple node.js images with API endpoints
service1 have routes:
- service1/api/first
- service1/api/second
`
`
service2 have routes:
- service2/api/third
- service2/api/fourth
`
So is possible to be able to access this services from same host, like this:
localhost/service1/api/first
localhost/service2/api/third
?
I tried like this:
My `docker-compose.yml` file:
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
whoami:
image: jwilder/whoami
environment:
- VIRTUAL_HOST=whoami.local
service1:
image: mynode:1.1
volumes:
- .:/app
restart: always
environment:
- VIRTUAL_HOST=service1.local
- VIRTUAL_PORT=8080
service2:
image: mynodeother:1.2
volumes:
- .:/app
restart: always
environment:
- VIRTUAL_HOST=service2.local
- VIRTUAL_PORT=8081
Here is generated config file from command docker exec nginx-proxy cat /etc/nginx/conf.d/default.conf
:
http://pushsc.com/show/code/58f739790a58d602a0b99d22
Also when I visit localhost in browser I get:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.
Thank you for using nginx.
回答1:
Try not to use IP addresses inside nginx
config file.
Also, you should use the same port number for both services: 8080 (if this is the port that nodejs application is listening).
Then you should properly define your routes to each service using location
in each server
context.
So, you should modify /etc/nginx/conf.d/default.conf
inside nginx
container like this:
# service1.local
upstream service1.local {
## Can be connect with "nginxproxy_default" network
# nginxproxy_service1_1
server service1:8080;
}
server {
server_name service1.local;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
location /service1 { #note this line
proxy_pass http://service1.local;
}
}
# service2.local
upstream service2.local {
## Can be connect with "nginxproxy_default" network
# nginxproxy_service2_1
server service2:8080; #same port
}
server {
server_name service2.local;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
location /service2 { #note this line
proxy_pass http://service2.local;
}
}
来源:https://stackoverflow.com/questions/43492974/multiple-docker-services-to-listen-on-same-host-and-port