简体   繁体   English

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

[英]nginx problem / causes redirection to port 80

I want to redirect all traffic from http://localhost:8080 to http://my-service:8080我想将所有流量从 http://localhost:8080 重定向到 http://my-service:8080

But when I access http://localhost:8080 the nginx redirects me to http://localhost但是当我访问 http://localhost:8080 时,nginx 将我重定向到 http://localhost

在此处输入图像描述

This is my nginx.conf这是我的 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这是我的 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.当我在没有 nginx 的情况下运行 docker 组合时,我可以访问 http://localhost:8080 而无需重定向。

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/;
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM