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
• 18,520 points
2,984 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
• 52,350 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,220 points
4,886 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,890 points
721 views
+1 vote
1 answer
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,041 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
• 18,520 points
5,253 views
0 votes
1 answer

How to use specific node version for yarn setup in AWS deployment?

You should be able to set your ...READ MORE

answered Jul 20, 2018 in DevOps on Cloud by Kalgi
• 52,350 points
1,019 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP