ÔõÑùʹÓÃNginxʵÏÖ»ùÓÚÇëÇóÒªÁìµÄ»á¼û¿ØÖÆ
ÔõÑùʹÓÃnginxʵÏÖ»ùÓÚÇëÇóÒªÁìµÄ»á¼û¿ØÖÆ
NginxÊÇÒ»¸öºÜÊÇÊÊÏàÖúΪ·´ÏòÊðÀíЧÀÍÆ÷ºÍ¸ºÔØƽºâÆ÷µÄ¿ªÔ´Èí¼þ¡£Ëü¼È¿ÉÒÔ×÷Ϊ¾²Ì¬Ð§À͵ÄÎļþЧÀÍÆ÷£¬Ò²¿ÉÒÔ´¦Öóͷ£¶¯Ì¬ÇëÇ󣬲¢ÇÒ»¹Ö§³Ö¶àÖÖÉèÖÃÑ¡ÏÒÔ±ãʵÏÖÌض¨ÐèÇóµÄ»á¼û¿ØÖÆ¡£
±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃnginxʵÏÖ»ùÓÚÇëÇóÒªÁìµÄ»á¼û¿ØÖÆ¡£ÎÒÃǽ«Ê¹ÓÃNginxµÄ¹Ù·½Ä£¿éngx_http_access_moduleÀ´ÏÞÖÆ»á¼ûµÄHTTPÒªÁì¡£
Ê×ÏÈ£¬È·±£ÒѾװÖÃÁËNginx²¢Ê¹ÓÃĬÈÏÉèÖÃÔËÐС£½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÐÞ¸ÄNginxµÄÉèÖÃÎļþ£¬Í¨³£Î»ÓÚ/etc/nginx/nginx.conf¡£ÕÒµ½http²¿·Ö£¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏ´úÂ룺
http { ... server { ... location / { # ÔÊÐíGETºÍHEADÒªÁìµÄÇëÇó if ($request_method !~ ^(GET|HEAD)$ ) { return 405; } # ´¦Öóͷ£ÆäËûÇëÇó ... } ... } ... }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽ«GETºÍHEADÒªÁì×÷ΪÆÆÀý£¬²¢ÔÊÐíÕâЩÇëÇóͨ¹ý¡£¹ØÓÚÆäËûÒªÁ죨ºÃ±ÈPOST¡¢PUT¡¢DELETEµÈ£©£¬ÎÒÃÇʹÓÃifÓï¾ä·µ»ØÒ»¸öHTTP״̬Âë405£¬ÌåÏÖÒªÁì²»±»ÔÊÐí¡£
ͨ¹ýÒÔÉÏÉèÖã¬ÎÒÃÇÒѾʵÏÖÁË»ùÓÚÇëÇóÒªÁìµÄ»á¼û¿ØÖÆ¡£¿ÉÊÇ£¬ÐèҪעÖصÄÊÇ£¬NginxµÄifÓï¾ä²¢·ÇËùÓÐÇéÐζ¼ÊÊÓã¬ÓÉÓÚËüÖ»ÄÜÔÚÌض¨µÄÉÏÏÂÎÄÖÐʹÓã¬ÇÒÖ»ÄÜÓÃ×÷¿ØÖÆ»á¼ûµÄ×îºóÒ»µÀ·ÀµØ¡£
ÈôÊÇÎÒÃÇÏëÒªÔ½·¢Ï¸ÄåµØ¿ØÖÆÇëÇóÒªÁìµÄ»á¼ûȨÏÞ£¬¿ÉÒÔʹÓÃNginxµÄrewriteÄ£¿é¾ÙÐд¦Öóͷ£¡£ÒÔÏÂÊÇÒ»¸ö´úÂëʾÀý£º
http { ... server { ... location / { # ´¦Öóͷ£POSTÒªÁìµÄÇëÇó if ($request_method = POST ) { # ·µ»Ø×Ô½ç˵µÄHTTP״̬Âë493 return 493; } # ´¦Öóͷ£ÆäËûÇëÇó ... } ... } ... }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃifÓï¾äÅжÏÇëÇóÒªÁìÊÇ·ñΪPOST£¬ÈôÊÇÊÇ£¬ÔòʹÓÃreturnÓï¾ä·µ»ØÒ»¸ö×Ô½ç˵µÄHTTP״̬Âë493¡£Í¨¹ýÕâÖÖ·½·¨£¬ÎÒÃÇ¿ÉÒÔƾ֤ÏêϸÐèÇó×Ô½ç˵´¦Öóͷ£²î±ðÇëÇóÒªÁìµÄÂß¼¡£
³ýÁËʹÓÃngx_http_access_moduleºÍrewriteÄ£¿é£¬Nginx»¹ÌṩÁËÆäËûÐí¶àÄ£¿éºÍ¹¦Ð§£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖ¸üΪÖØ´óºÍÎÞаµÄ»á¼û¿ØÖÆÕ½ÂÔ¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃngx_http_auth_basic_moduleÄ£¿éʵÏÖ»ù±¾µÄHTTPÉí·ÝÑéÖ¤£¬»òÕßʹÓÃngx_http_limit_req_moduleÄ£¿éʵÏÖÇëÇóƵÂÊÏÞÖÆ¡£
×ܽáÆðÀ´£¬Í¨¹ýNginxµÄÉèÖã¬ÎÒÃÇ¿ÉÒÔʵÏÖ»ùÓÚÇëÇóÒªÁìµÄ»á¼û¿ØÖÆ¡£±¾ÎÄÌṩÁËÒÔÉÏÁ½ÖÖ³£¼ûµÄÒªÁ죬²¢¸ø³öÁËÏìÓ¦µÄ´úÂëʾÀý¡£¶ÁÕß¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐнøÒ»²½µÄÉèÖúÍÀ©Õ¹£¬ÔÚ°ü¹ÜϵͳÇå¾²ÐÔµÄͬʱÌá¸ß»á¼û¿ØÖƵÄÎÞаÐÔºÍϸÄå¶È¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginxʵÏÖ»ùÓÚÇëÇóÒªÁìµÄ»á¼û¿ØÖƵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡