กลับหน้าแรก
Next Js 16.2.2 Proxy
📂 Coding

Next Js 16.2.2 Proxy

Next Js 16.2.2 ฟังก์ชั่น Proxy ที่มาแทน Middleware
ทดสอบ บล็อก Bot หาช่องโหว่ มีทั้งหา PHP , Router 
เริ่มต้นก็จะเขียนประมาณนี้ รวมถึงเขียน 403 เอง


```javascript

import { NextResponse ,NextRequest } from 'next/server';


const FORBIDDEN_PATHS = [
  '.aws','aws-','.php', '.env', '.git', 'wp-admin', 'wp-content', 'wp-login','admin','config' ,'_debug','test'
];

export default async function proxy(request: NextRequest) {
  const url = new URL(request.url);
  const pathname = url.pathname.toLowerCase();

  // ตรวจสอบเส้นทางที่เป็นบอทสแกน
  if ((FORBIDDEN_PATHS.some(path => pathname.includes(path))) || (request.method === 'POST' && url.pathname === '/')) {
   
    // 403 | Access Denied.
    //return new Response('Access Denied', { status: 403 });

    const url = request.nextUrl.clone();
    url.pathname = '/forbidden';    
    return NextResponse.rewrite(url,{status: 403, headers: { 'content-type': 'text/html' }});
  }

  return NextResponse.next();
}

export const config = {
  // กำหนดให้ตรวจสอบทุกเส้นทาง ยกเว้นไฟล์ static และ api
  matcher: [
    '/((?!api|_next/static|_next/image|favicon.ico).*)',
  ],
}```
แชร์บทความนี้:

💬 ความคิดเห็น (0)

ยังไม่มีใครมาคุยด้วยเลย... เริ่มคอมเมนต์คนแรกกัน!