2

I want everything starting with /api to be directed to http://localhost:3007

This is my nginx.conf

user nginx;
worker_processes  1;

daemon off;

events {
    worker_connections 1024;
}

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

    sendfile        on;
    keepalive_timeout  65;


    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/html;
            index  index.html index.htm;
        }

        location /api {
            proxy_pass http://localhost:3007;
            proxy_read_timeout 5m;
        }

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

It works when I run it locally on my mac. But it's not working when I run it in a docker-container.

This is my docker-file:

FROM smebberson/alpine-nginx:latest
COPY /dist /usr/html/
COPY nginx.conf /etc/nginx/nginx.conf

This is my docker-compose:

version: "2"
services:
  web:
    build: .
    ports:
     - "80:80"

The error I'm getting from nginx:

2017/06/28 13:06:51 [error] 200#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: localhost, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:3007/api", host: "localhost"
Joe
  • 129
  • 2
  • 8
  • So, you want to run a nginx in docker container ? – P0pR0cK5 Jun 28 '17 at 13:41
  • @JulienGuerder Yes. I've my express-API in one container and nginx serving the static content in another container. – Joe Jun 28 '17 at 13:44
  • You need to use the docker network function for connect everything together. Look at this link, it explain how to use docker network with example. http://www.dasblinkenlichten.com/docker-networking-101-host-mode/ – P0pR0cK5 Jun 28 '17 at 14:02
  • Seems heavy. Is there a lot of conf to get this up and running? – Joe Jun 28 '17 at 14:07
  • Not really, you just have to understand how nework work – P0pR0cK5 Jun 28 '17 at 14:22

1 Answers1

2

Most probably, you want not localhost:3007 but something like api-upstream-server:3007 - a separate container running the app server code and exposing port 3007.

Inside the container, localhost is the container, not the host machine. Docker isolates the container from the host node.

But notice, you will probably have to run everything else (database, etc.) in docker containers as well.

saabeilin
  • 409
  • 1
  • 4
  • 11