Search code examples
dockernginxdocker-composereverse-proxy

nginx problem / causes redirection to port 80


I want to redirect all traffic from http://localhost:8080 to http://my-service:8080

But when I access http://localhost:8080 the nginx redirects me to http://localhost

enter image description here

This is my nginx.conf

events {
  worker_connections  1024;  ## Default: 1024
}

http{

    server {
       listen 8080 default_server;
       listen [::]:8080 default_server;

       server_name localhost;

       location / {
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_pass http://my-service:8080/;
       }
     }

}

And this is my docker-compose

version: '2.1'
services:

  nginx-proxy:
    image: nginx:stable-alpine
    container_name: nginx-proxy
    ports:
      - "8080:8080"
    volumes:
      - ./data/nginx/nginx.conf:/etc/nginx/nginx.conf
    networks:
     - no-internet
     - internet


  my-service:
    ....
    expose:
     - "8080"
    networks:
     - no-internet


networks:
  internet:
    driver: bridge
  no-internet:
    internal: true
    driver: bridge

when I run the docker compose without the nginx, I can access http://localhost:8080 without redirection.


Solution

  • I solved the problem:

    server {
       listen 8080;
       listen [::]:8080;
    
       server_name localhost;
       
        location / {
          proxy_pass http://ap-service:8080;
          proxy_redirect http://ap-service:8080/ $scheme://$host:8080/;
        }
     }