Score:0

nginx-proxy always returning 301 Moved Permanently

us flag

I'm following the example docker compose for an nginx-proxy, but it always returns a 301 Moved Permanently error. I'm using docker hosted in Ubuntu 22.04.

my docker-compose.yml file

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

my test command and output:

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>

output from the nginx log

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" "-"

I've tried adding 192.168.0.2 whoami.media.local to my host's /etc/hosts file and the result is the same:

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/

per the website example, the expected output is: I'm 5b129ab83266

What am I getting wrong in my system configuration?

Ginnungagap avatar
gu flag
You've taken the working example and associated test command, made modifications to the docker-compose but kept the test command. Have you tried the basic example without editing anything and then started editing to see where you went wrong? Your `curl -I` looks pretty clear to me.
PaulH avatar
us flag
my only change of consequence was to update the VIRTUAL_HOST parameter to match my system. I can remove the port 443, certificate directory, and dependency and the result is the same.
Ginnungagap avatar
gu flag
Surprising given the block in which the redirection to https occurs in the [nginx configuration template](https://github.com/nginx-proxy/nginx-proxy/blob/main/nginx.tmpl#L541).
Ginnungagap avatar
gu flag
Are you saying that even without exposing certificates the `cert_ok` condition is true?
PaulH avatar
us flag
that's a compelling point... I'll re-verify my assumptions.
PaulH avatar
us flag
you were correct. it was the cert modifications I made combined with bad assumptions. thank you for your help.
Score:0
us flag

It is possible your issue is that nginx-proxy is redirecting to HTTPS, but you haven't set up SSL for whoami.media.local.

Possible Solutions:

  1. Add SSL: Ensure SSL certificates for whoami.media.local are in the ../certificates directory.
  2. Disable HTTPS Redirect: Add HTTPS_METHOD=noredirect to the nginx-proxy environment in your docker-compose.yml.
yaml
nginx-proxy:
  ...
  environment:
    - HTTPS_METHOD=noredirect
  1. Restart: After changes, run:
bash
docker-compose down
docker-compose up -d
  1. Test:
bash
curl -H "Host: whoami.media.local" media.local

This should resolve the 301 Moved Permanently issue.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.