nginxのdeny allow はreturn と併用できない

returnがある場合はallow denyによるアクセス制御が無視されるっぽい

nginxのconfigをこのようにして

server {
    listen 8080;
    server_name localhost;
    root /var/www/html;
    location / {
        deny all;
    }
}

server {
    listen 8081;
    server_name localhost;
    root /var/www/html;
    location / {
        deny all;
        return 301 http://localhost$request_uri;
    }
}

server {
    listen 8082;
    server_name localhost;
    root /var/www/html;
    location / {
        deny all;
        proxy_pass http://127.0.0.1$request_uri;
    }
}

docker composeでnginxを起動

version: '3.7'
services:
  nginx:
    image: nginx:latest
    ports:
      - 8082:8082
      - 8081:8081
      - 8080:8080
      - 80:80
    volumes:
      - "./sites.conf:/etc/nginx/conf.d/sites.conf"

curlしてみると

$ curl -v http://localhost:8080/ 2>&1 | grep "< HTTP"
< HTTP/1.1 403 Forbidden

deny all のみ、アクセス拒否

$ curl -v http://localhost:8081/ 2>&1 | grep "< HTTP"
< HTTP/1.1 301 Moved Permanently

deny all + return 301、returnが有効

$ curl -v http://localhost:8082/ 2>&1 | grep "< HTTP"
< HTTP/1.1 403 Forbidden

deny all + proxy_pass、アクセス拒否

returnには効いてない!