728x90

개요

로컬과 서버의 결괏값이 다름

로컬에서와 서버에서 특정 링크(네이버 쇼핑몰)를 테스트했을 때 로컬에서는 문제가 없지만 서버에서는 og tag로 이미지 파일과 title이 null 값으로 동일한 URL인데 저장이 안 되는 문제가 발생했다.

2주 넘게 고민하다 이유를 모르겠어서 매주 주말에 참가하는 부천 모각코 모임에 여쭤봤더니 경력이 높으신 분이 문제를 접근하는 방법과 해결방법에 대해 말씀해주셨다. 이번 문제에 대해 접근하는 방법과 해결 과정 등 많은 것을 배울 수 있는 시간이었다.

로컬과 서버의 차이점

   1. 서버와 로컬에서 차이점은 서버는 앞단에 nginx가 하나 더 있음

   2. 결괏값 URL이 다름

로컬

baseURL = <https://search.shopping.naver.com/book/catalog/32490794178>
postDataSaveReq.getLink() = <https://msearch.shopping.naver.com/book/catalog/32490794178>
postDataSaveReq.getImgUrl() = <https://shopping-phinf.pstatic.net/main_3249079/32490794178.20220527093651.jpg>

서버

baseURL = <https://search.shopping.naver.com/book/catalog/32490794178>
postDataSaveReq.getLink() = <https://search.shopping.naver.com/book/catalog/32490794178>
postDataSaveReq.getImgUrl() = null

   3. 서버와 로컬의 ip 주소가 다르다.

이유

og tag 라이브러리를 보면 baseURL로 한번 더 요청을 보낸다. 아래와 같이 로컬에서는 curl 요청이 문제없이 되지만 ec2 서버에서 네이버 쇼핑몰로 curl request를 보내면 307 Temporary Redirect 상태 코드가 나온다. 307 상태 코드는 요청된 URL이 잠시 다른 URL로 바뀐 것을 알리는 상태 코드이다. (Location: header로 지정된) 바뀐 URL은 GET method로 접근해야 한다. 하지만 서버에서 요청 보낸 헤더 location을 보낸 URL을 보면 https://search.shopping.naver.com/blocked.html로 비정상적 요청이 감지되었습니다.라는 페이지가 나온다.

로컬에서 보낸 curl 요청

curl https://search.shopping.naver.com/book/catalog/32490794178

<!DOCTYPE html>
<html lang="ko">
<head><meta charSet="utf-8"/><meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>서울 시 : 네이버 도서</title><link rel="shortcut icon" href="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/img/favicon.ico"/>
<link rel="apple-touch-icon" href="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/img/favicon_60.png"/>
<meta property="og:title" content="서울 시 : 네이버 도서"/>
<meta property="og:description" content="네이버 도서 상세정보를 제공합니다."/>
<meta property="og:image" content="https://shopping-phinf.pstatic.net/main_3249079/32490794178.20220527093651.jpg"/>
<meta property="og:url" content="https://search.shopping.naver.com/book/catalog/32490794178"/>
<meta name="next-head-count" content="10"/>
<link rel="preconnect" href="https://static.nid.naver.com"/>
<link rel="preconnect" href="https://lcs.naver.com"/>
<link rel="preconnect" href="http://shopping.phinf.naver.net"/>
<link rel="preconnect" href="https://ssl.pstatic.net"/>
<link rel="preconnect" href="https://shopping-phinf.pstatic.net"/>
<link rel="preconnect" href="https://volts.shopping.naver.com"/>
<link rel="preload" href="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/_next/static/css/c1c28aedd09b2c2d.css" as="style"/>
<link rel="stylesheet" href="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/_next/static/css/c1c28aedd09b2c2d.css" data-n-g=""/>
<link rel="preload" href="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/_next/static/css/05d5e4267a2caa1a.css" as="style"/>
<link rel="stylesheet" href="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/_next/static/css/05d5e4267a2caa1a.css" data-n-p=""/>
<link rel="preload" href="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/_next/static/css/bda4bc7d3c476bd4.css" as="style"/>
<link rel="stylesheet" href="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/_next/static/css/bda4bc7d3c476bd4.css"/><noscript data-n-css="">
</noscript><script defer="" nomodule="" src="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/_next/static/chunks/polyfills-5cd94c89d3acac5f.js">
</script><script defer="" src="https://ssl.pstatic.net/shoppingsearch/static/book-catalog/book-catalog-221016-124516/_next/static/chunks/315.75129e479e1f82a7.js">
</script>

서버에서 보낸 curl 요청

curl -i https://search.shopping.naver.com/book/catalog/32490794178

HTTP/2 307 
date: Sun, 16 Oct 2022 08:33:54 GMT
content-type: text/html; charset=utf-8
content-length: 164
location: https://search.shopping.naver.com/blocked.html
referrer-policy: unsafe-url
server: nfront

<html>
<head><title>307 Temporary Redirect</title></head>
<body>
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>

https://search.shopping.naver.com/blocked.html 화면

해결 방법

  1. 네이버 쪽에서 접속이 차단되지 않은 ip 대역의 클라우드를 찾는다.
  2. 사용자가 있는 경우 프론트에서 처리를 해주어 request를 보낸다. 서버에서 request를 보내면 ec2 ip대역이라 차단이 되지만 사용자에서 요청을 보내면 각자 기기의 ip가 있어서 차단되지 않을 수 있다.
728x90

+ Recent posts