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'用來判斷可執行文件是否存在;