Score:1

Unable to forward nginx in a docker container to localhost 3000

cy flag

I'm currently trying to configured nginx within a docker container to redirect to localhost:3000 when I navigate to localhost:8080. From reading the documentation it seems I need to map the ports to redirect requests to port 80 in the container. However, when I build an image based on the following Dockerfile:

FROM alpine

RUN apk --no-cache add nginx

COPY ./nginx.conf /etc/nginx/nginx.conf

RUN mkdir -p /run/nginx

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

and run the container mapping the port 8080 to port 80 with the following command:

docker run -p 8080:80  nginx-server

When I navigate to localhost:8080 I'm not being redirected to localhost:3000 and I get

localhost didn’t send any data.

After doing some digging it seems I need to change my nginx configuration to point to proxy_pass http://host.docker.internal.However, this did not work and when I make a request to localhost:8080 I get nothing. here is my nginx configuration:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       127.0.0.1;
        server_name  localhost;

        location / {
            proxy_pass http://host.docker.internal:3000;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    include servers/*;
}

Any idea what am I doing wrong?

cn flag
Could you try to add the port for the listener? E.g.: `listen 127.0.0.1:80;`
The Fool avatar
br flag
this is probably not what you want. Since you are likely to use nginx for external traffic. External traffic wont understand host.docker.internal. You probably want to use the actual address of your server for the redirect.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.