redirect 오류

Junyoung·2024년 5월 23일

trouble

목록 보기
7/8

Nginx 에서 http 요청은 https로 변환해 redirect 보내고 있었다.

get 요청은 제대로 전환이 됬지만, Post, Patch 요청은 Get 메소드로 변경되어 Spring Server에 요청을 보내고 있었다.

server {
    listen 80;
    listen [::]:80;
    server_name dangil.store;
    access_log off;

    location /.well-known/acme-challenge/ {
        allow all;
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
    listen 443 ssl;
    server_name dangil.store;
    include /etc/nginx/conf.d/service-color.inc;

    # 서트봇이 볼륨으로 남겨두는 인증서를 가지고 이미지를 만들었고 엔진엑스 컨테이너에서 해당 인증서를 사용한다.
    ssl_certificate /etc/letsencrypt/live/dangil.store/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dangil.store/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # 부적절한 헤더도 요청을 허용한다.
    ignore_invalid_headers off;

    location /api {
        resolver 127.0.0.11 valid=30s;
        proxy_pass http://spring_$spring_color:8080;
        proxy_set_header X-Forwarded-Host $server_name;

        # 디폴트로 IP는 프록시로 변경되고 난뒤에 (리버스 프록시 기능)
        # 원래의 요청의 헤더에 설정을 수정 및 추가한다
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

현재 코드를 확인해보면 http 요청을 https 로 리다이렉트를 진행한다 !

get 요청은 다음과 같이 정상적으로 http, https 요청이 리다이렉트 된다.

post 요청의 경우 https 는 적용이 되나

http 요청의 경우에는 서버에서 매칭되는 method가 없다고 한다

이처럼 서버측에서 해당 url에 맞는 method 가 없다고 한다.


이전에 학습했던 자료를 찾아보니

301 의 경우에는 주소만 가지고 get 요청으로 리다이렉트를 보내는 것을 확인할수 있다

따라서 308으로 바꿔서 리다이렉트를 보내려고한다 !

    location / {
        return 301 https://$host$request_uri;
    }

http의 post 요청도 잘 보내지는 것을 확인했다 !

온라인 블로그의 코드들을 그대로 따라하기 보다는 원리를 이해하고 구현한다면 이처럼 여러가지의 오류를 해결할수 있다 !

profile
라곰

0개의 댓글