0

I'm trying to set up a nginx image that redirects requests depending on a url to a service port. I'm using docker-compose and this is my docker-compose.yml:

version: '3.3'

services:

  nginx:
    image: nginx:latest
    build: .
    container_name: nginx
    depends_on:
        - service-1
    ports:
      - "80:80"

  service-1:
    image: dockerhub/image:latest
    container_name: service-1
    ports:
      - "8080:8080"
    restart: on-failure

The dockerfile used to build nginx contains the following:

FROM nginx:latest
COPY ./nginx-html-template/ /usr/share/nginx/html/
COPY ./nginx.conf /etc/nginx/conf.d/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]

and last but not least the nginx.config:

server {
    listen [::]:80;
    listen 80 default_server;
    server_name localhost;

    location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
            try_files $uri $uri/ =404;
    }

    location /service-1 {
        proxy_pass http://service-1:8080/;
     }
}

if I hit localhost/service-1 I would expect nginx to redirect the request to port 8080. But instead I get a 404. If instead I hit localhost:8080 then it works fine. What am I doing wrong? Thanks in advance

user3353167
  • 101
  • 1

1 Answers1

1

Try to modify it like this:

    location /service-1/ {
        proxy_pass http://127.0.0.1:8080/;
     }
George Y
  • 380
  • 2
  • 11