nginx 问题/导致重定向到端口 80

nginx problem / causes redirection to port 80

我想将所有流量从 http://localhost:8080 重定向到 http://my-service:8080

但是当我访问 http://localhost:8080 时,nginx 将我重定向到 http://localhost

这是我的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/;
       }
     }

}

这是我的 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

当我 运行 docker 在没有 nginx 的情况下编写时,我可以访问 http://localhost:8080 而无需重定向。

我解决了问题:

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

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