---
title: "nginx: [emerg] unknown directive"
author: "study121007"
date: "2016-07-18T22:43:44Z"
tags: []
categories: ["操作系统"]
slug: "nginx-emerg-unknown-directive"
draft: false
lastmod: "2016-07-18T22:43:44Z"
id: 2312034
url: "https://study121007.iteye.com/blog/2312034"
---

服务器遭到一个agent包含java/1.6.0的频繁攻击，本打算按照如下的方式配置nginx来屏蔽请求

 location /{

       if($http\_user\_agent ~* "java/1.6.0"){

           rewrite ^(.\*)$ http://img.mp.itc.cn/upload/20160716/6362d52b6ade45cdb6e354fd9de64d4a\_th.jpg redirect;

       }

    }

nginx重启的时候出现如下异常：

nginx: \[emerg] unknown directive "if($http\_user\_agent" in /usr/local/nginx/conf/nginx.conf:86

原因是：

    if 和 ( 缺一个空格 ，如果没有空格他把if($http\_user\_agent当成一个指令去执行，很显然这样是错误的。