Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend and backend run on different ports gets 502 Bad Gateway #939

Closed
thegenius314 opened this issue Sep 26, 2017 · 3 comments · Fixed by #2434
Closed

Frontend and backend run on different ports gets 502 Bad Gateway #939

thegenius314 opened this issue Sep 26, 2017 · 3 comments · Fixed by #2434
Labels
scope/multiport Issue or PR related to multi port proxying

Comments

@thegenius314
Copy link

thegenius314 commented Sep 26, 2017

Hi Peoples
I'm currently running a container with my web application and it communicates through two ports,
for the frontend and backend.
When I run the docker compose file (which starts the app and the proxy) it gives me a 502 Bad gateway
when I run with only one port it serves that part of the app.
Im passing the port with the "VIRTUAL_PORT=80" is there a way to pass more than one port or if I make a separate container for the Frontend how would I get the proxy to speak to both containers with one request?
Thank in advance,

docker-compose

`reverseproxy:
image: jwilder/nginx-proxy

ports:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/tmp/docker.sock

myapp:
depends_on:
- reverseproxy
build: ./app-files
environment:
- "VIRTUAL_HOST=my-domain.com"
- "VIRTUAL_PORT=80,8080"
expose:
- 80
- 8080`

@kamermans
Copy link
Contributor

kamermans commented Oct 20, 2017

Hi,

is there a way to pass more than one port?

No, currently you can only expose one port per container.

if I make a separate container for the Frontend how would I get the proxy to speak to both containers with one request?

One request can only go to one place. I imagine in this scenario your browsers sends a request to the Frontend, and the Frontend needs to send a request to the Backend. It would be good practice to separate them anyway, and with nginx-proxy, the solution to this problem is to give the Frontend and the Backend different hostnames, like www.foo.com and backend.foo.com.

Here's an example (untested) based on your config:

myapp-backend:
    build: ./app-files
    environment:
        - "VIRTUAL_HOST=backend.my-domain.com"
        - "VIRTUAL_PORT=8080"
    expose:
        - 8080

myapp-frontend:
    depends_on:
        - myapp-backend
    build: ./app-files
    environment:
        - "VIRTUAL_HOST=my-domain.com"
        - "VIRTUAL_PORT=80"
    expose:
        - 80

reverseproxy:
    image: jwilder/nginx-proxy
    depends_on:
        - myapp-frontend
        - myapp-backend
    ports:
        - "80:80"
    volumes:
        - /var/run/docker.sock:/tmp/docker.sock

Note that in this example, my-domain.com and backend.my-domain.com would both resolve to the same IP, and the from the outside, the requests to both servers would go to port 80, but based on the hostname, the traffic would be passed to the appropriate container.

If the Frontend needs to talk to the backend directly, it can choose to do that via either myapp-backend:8080, or through nginx-proxy at backend.my-domain.com:80.

@buchdag buchdag added kind/feature-request Issue requesting a new feature scope/multiport Issue or PR related to multi port proxying labels Dec 19, 2023
@Coinhexa
Copy link

@kamermans thank you for the example above. how would it work if your frontend and backend were separate vscode projects and each had their own docker-compose file?

@buchdag
Copy link
Member

buchdag commented Sep 29, 2024

@Coinhexa you have to ensure that your various services (in this case, backend and frontend) are in the same Docker network. If they are in different compose files, that won't be the case by default. The recommended solution is to manually create a Docker network and use it in the various compose files, rather than letting compose automatically create and use a different network for each files.

@buchdag buchdag removed the kind/feature-request Issue requesting a new feature label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope/multiport Issue or PR related to multi port proxying
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants