nginx-proxy 总是返回 301 Moved Permanently

nginx-proxy 总是返回 301 Moved Permanently

我正在关注docker compose 示例对于 nginx-proxy,但它总是返回错误301 Moved Permanently。我正在使用托管在 Ubuntu 22.04 中的 docker。

我的 docker-compose.yml 文件

version: '2'

services:
  nginx-proxy:
    image: nginxproxy/nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ../certificates:/etc/nginx/certs:ro
    depends_on:
      - whoami

  whoami:
    image: jwilder/whoami
    expose:
      - "8000"
    environment:
      - VIRTUAL_HOST=whoami.media.local
      - VIRTUAL_PORT=8000

我的测试命令和输出:

user@media:~/docker/nginx-test$ curl -H "Host: whoami.media.local" media.local
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.25.1</center>
</body>
</html>

nginx 日志的输出

nginx-proxy_1  | nginx.1     | whoami.media.local 192.168.0.2 - - [11/Aug/2023:16:57:07 +0000] "GET / HTTP/1.1" 301 169 "-" "curl/7.81.0" "-"

我尝试添加192.168.0.2 whoami.media.local到我的主机的/etc/hosts文件,结果是一样的:

user@media:~/docker/nginx-test$ curl -I whoami.media.local
HTTP/1.1 301 Moved Permanently
Server: nginx/1.25.1
Date: Fri, 11 Aug 2023 17:00:57 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://whoami.media.local/

根据网站示例,预期输出是:I'm 5b129ab83266

我的系统配置出了什么错误?

答案1

您遇到的问题可能是nginx-proxy重定向到 HTTPS,但您尚未为其设置 SSL whoami.media.local

可能的解决方案

  1. 添加 SSL:确保 SSL 证书whoami.media.local../certificates目录中。
  2. 禁用 HTTPS 重定向:添加HTTPS_METHOD=noredirectnginx-proxy您的环境中docker-compose.yml
yaml
nginx-proxy:
  ...
  environment:
    - HTTPS_METHOD=noredirect
  1. 重新开始:更改后,运行:
bash
docker-compose down
docker-compose up -d
  1. 测试
bash
curl -H "Host: whoami.media.local" media.local

这应该可以解决301 Moved Permanently问题。

相关内容