Nginx 镜像不一样端口映射情形时重定向 uri 端口不正确

使用 docker 镜像 nginx:1.17.0-alpine 构建容器。将主机端口 8080 映射到容器的 80 端口,因为
采用默认的配置访问目录名不加 / 时,不会返回其中的 index 内容,因此须要额外配置。以下的配置适用于同端口映射的情形,当宿主机的端口和容器的端口不一致时,访问 http://localhost:8080/abc, 则会重定向到 http://localhost/abc/, 这显然不是咱们想要的结果。html

listen 80;
    location / {
        index  index.html index.htm;
        try_files $uri $uri/ =404;
    }

因而我去问搜索引擎,终于找到了此情形下的配置。nginx

location / {
        index  index.html index.htm;
    }

    location ~ ^.*[^/]$ {
        try_files $uri @rewrite;
    }
    location @rewrite {
        return 302 $scheme://$http_host$uri/;
    }

参考 https://serverfault.com/quest...docker

相关文章
相关标签/搜索