Nginx Request redirection one container to another

0 votes

I am running two centos docker container using following compose file-

version: "2"
services:
  nginx:
    build:
      context: ./docker-build
      dockerfile: Dockerfile.nginx
    restart: always
    ports:
      - "8080:8080"
    command: "/usr/sbin/nginx"
    volumes:
      - ~/my-dir:/my-dir
  data:
    build:
      context: ./docker-build
      dockerfile: Dockerfile.data
    restart: always
    ports:
      - "8081:8081"
    command: "/usr/sbin/nginx"
    volumes:
     - ~/my-dir-1:/my-dir-1

I’m trying to redirect request http://host-IP:8080/my-data/ to the data container using nginx. Below is my Nginx configuration for nginx container

/etc/nginx/conf.d/default.conf

server {
    listen 8080;
  location / {
       root   /my-dir/;
       index  index.html index.htm;
       }
}

I am able to access my-dir directory using http://host-IP:8080 URL and my-dir-1 using http://host-IP:8081 URL, how can I configure Nginx to redirect request on data container using http://host-IP:8080/my-data URL

Jul 20, 2018 in DevOps on Cloud by Hannah
• 16,220 points
51 views

2 answers to this question.

0 votes

You can use a proxy

http {
  upstream data_container {
    server data:8081;
  }
  server {
    listen 8080;
    location / {
        root   /my-dir/;
        index  index.html index.htm;
    }
    location /my-data {
      proxy-pass http://data_container$request_uri;
    }
  }
}

answered Jul 20, 2018 by Kalgi
• 41,990 points
0 votes

try adding a / at the end of 8080 like below. It will no longer append "route" to the forwarded request now.

location /route {
    proxy_pass  http://127.0.0.1:8080/;
}
answered Oct 25, 2018 by Haseeb

Related Questions In DevOps on Cloud

+4 votes
2 answers

unable to get subdomain of URL in NGINX

You just need to add .+ after ...READ MORE

answered Oct 11, 2018 in DevOps on Cloud by lina
• 8,110 points
652 views
0 votes
1 answer
0 votes
1 answer

Adding public contact from request to aws

Third one seems to be the best ...READ MORE

answered Jun 28, 2018 in DevOps on Cloud by DareDev
• 6,810 points
37 views
+3 votes
2 answers
0 votes
1 answer
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
209 views
+2 votes
3 answers

can't make nginx try_files to work with default_index.html

Here's another convenient use of try_files, as ...READ MORE

answered Oct 18, 2018 in DevOps on Cloud by Hannah
• 16,220 points
410 views
0 votes
1 answer