검색엔진 자동 색인(Fast Indexing) 구축 가이드

사이트에 글을 발행하는 즉시 구글, 네이버, 빙(Bing) 봇을 호출하여 인덱싱 속도를 극대화하는 기술 가이드입니다.


1. 구글 (Google) – Indexing API

구글은 보안 인증(OAuth 2.0) 문제로 Python을 사용하는 것이 가장 안정적입니다.

사전 준비:

  1. Google Cloud Console 프로젝트 생성 및 Indexing API 활성화.
  2. 서비스 계정(Service Account) 생성 및 JSON 키 파일 다운로드.
  3. Google Search Console > 설정 > 사용자 추가에 서비스 계정 이메일(owner 권한) 추가.

Python 스크립트 (google_index.py):

import requests
from google.oauth2 import service_account
from google.auth.transport.requests import Request

# 설정: 다운로드 받은 JSON 키 파일 경로
JSON_KEY_FILE = 'service_account.json' 
SCOPES = ["[https://www.googleapis.com/auth/indexing](https://www.googleapis.com/auth/indexing)"]
ENDPOINT = "[https://indexing.googleapis.com/v3/urlNotifications:publish](https://indexing.googleapis.com/v3/urlNotifications:publish)"

def request_google_indexing(url):
    try:
        # 1. 인증 토큰 생성
        creds = service_account.Credentials.from_service_account_file(JSON_KEY_FILE, scopes=SCOPES)
        creds.refresh(Request())

        # 2. 요청 헤더 및 데이터 설정
        headers = {
            "Authorization": "Bearer " + creds.token,
            "Content-Type": "application/json"
        }
        content = {
            "url": url,
            "type": "URL_UPDATED"
        }

        # 3. 전송
        response = requests.post(ENDPOINT, headers=headers, json=content)

        if response.status_code == 200:
            print(f"[Google] 색인 요청 성공: {url}")
        else:
            print(f"[Google] 실패: {response.status_code} - {response.text}")

    except Exception as e:
        print(f"[Google] 에러 발생: {str(e)}")

# 실행부 (PHP에서 호출 시 인자를 받아 처리하도록 수정 가능)
if __name__ == "__main__":
    # 테스트 URL
    request_google_indexing("[https://mysite.com/new-post-url](https://mysite.com/new-post-url)")

2. 빙 (Bing) & 야후 – IndexNow

Bing, Yahoo, Yandex 등은 IndexNow 프로토콜을 사용합니다. 구현이 간단하여 PHP로 즉시 처리가 가능합니다.

사전 준비:

  1. Bing Webmaster Tools > IndexNow 메뉴에서 API Key 생성.
  2. Key 값과 동일한 이름의 텍스트 파일(예: abc123.txt, 내용도 abc123)을 사이트 루트 디렉토리에 업로드.

PHP 함수:

function requestIndexNow($url) {
    // 설정값
    $host = "mysite.com";          // 본인 도메인
    $key = "xxxxxxxxxxxx";         // Bing에서 받은 API Key
    $keyLocation = "https://" . $host . "/" . $key . ".txt"; // 키 파일 위치

    $endpoint = "[https://api.indexnow.org/indexnow](https://api.indexnow.org/indexnow)";

    $data = [
        "host" => $host,
        "key" => $key,
        "keyLocation" => $keyLocation,
        "urlList" => [$url]
    ];

    $ch = curl_init($endpoint);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    // 200 OK 또는 202 Accepted가 뜨면 성공
    if ($httpCode == 200 || $httpCode == 202) {
        echo "[Bing] IndexNow 요청 성공\n";
    } else {
        echo "[Bing] 실패 ($httpCode): $response\n";
    }
}

3. 네이버 (Naver) – Sitemap Ping

네이버는 개별 URL 수집 API를 제공하지 않으므로, 사이트맵 핑(Sitemap Ping)을 통해 봇을 호출해야 합니다.

사전 준비:

  1. 새 글 발행 시 sitemap.xml이 자동으로 업데이트되도록 CMS 설정.

PHP 함수:

function pingNaver($sitemapUrl) {
    // 네이버 핑 엔드포인트
    $endpoint = "[http://searchadvisor.naver.com/ping_sitemap.php?sitemap=](http://searchadvisor.naver.com/ping_sitemap.php?sitemap=)" . urlencode($sitemapUrl);

    $ch = curl_init($endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 네이버는 User-Agent가 없으면 차단될 수 있음
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Compatible; MyBot/1.0)'); 

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode == 200) {
        echo "[Naver] 사이트맵 핑 전송 성공\n";
    } else {
        echo "[Naver] 실패 ($httpCode)\n";
    }
}

4. 전체 실행 로직 (PHP Controller)

글 발행 버튼을 눌렀을 때 실행되는 백엔드 로직 예시입니다.

// [1] 데이터베이스에 글 저장
$newPostId = savePostToDatabase($postData);
$newUrl = "[https://mysite.com/horoscope/](https://mysite.com/horoscope/)" . $date;

// [2] 사이트맵 파일 갱신 (반드시 색인 요청 전에 최신화되어 있어야 함)
updateSitemapXml(); 
$sitemapUrl = "[https://mysite.com/sitemap.xml](https://mysite.com/sitemap.xml)";

// [3] 색인 요청 실행

// A. Bing (PHP 함수 호출)
requestIndexNow($newUrl);

// B. Naver (PHP 함수 호출)
pingNaver($sitemapUrl);

// C. Google (Python 스크립트 호출)
// 서버 환경에 따라 python3 또는 python 명령어가 다를 수 있음
// 실제 운영 시엔 비동기(Queue)로 처리하는 것을 권장
$command = escapeshellcmd("python3 /path/to/google_index.py " . $newUrl);
exec($command, $output);

echo "모든 검색엔진에 색인 요청을 완료했습니다.";

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

위로 스크롤