server的配置以php为例,如下:
1 server{ 2 root /var/webproject/www/mytools-php; 3 index index.html index.php; 4 5 location ~.+?\.php(/|$) { 6 fastcgi_split_path_info ^(.+?\.php)(.*)$; 7 if (!-f $document_root$fastcgi_script_name){ 8 return 404; 9 } 10 if ($fastcgi_script_name != /index.php){ 11 return 404; 12 } 13 fastcgi_pass 127.0.0.1:9000; 14 fastcgi_index index.php; 15 include /opt/nginx-1.8/conf/fastcgi_php_params; 16 } 17 }
nginx中的if无法进行&&、||等逻辑运算,所以我们需要一步一步的进行判断,上面配置首先判断网站根目录下是否存在请求的文件,如果不存在返回404,如果存在接着判断请求的文件是不是index.php,如果不是则返回404。
判断变量和字符串是否相等使用‘=‘或者‘!=‘;
‘-f‘和‘!-f‘能够判断判断文件是否存在;
‘~‘和‘!~‘是区分大小写的正则判断,‘~*‘和‘!~*‘是不区分大小写的正则判断;
‘-d‘判断目录是否存在;
‘-e和‘!-e‘用来判断文件、目录、符号连接是否存在;
‘-x‘和‘!-x‘用来判断可执行文件是否存在;
原文:http://www.cnblogs.com/zouyitangzaishuo/p/5267539.html