国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

Nginx的try_files指令使用實(shí)例

2024-08-30 12:30:15
字體:
供稿:網(wǎng)友

try_files指令說明

try_files指令
語法:try_files file ... uri 或 try_files file ... = code
默認(rèn)值:無
作用域:server location

其作用是按順序檢查文件是否存在,返回第一個找到的文件或文件夾(結(jié)尾加斜線表示為文件夾),如果所有的文件或文件夾都找不到,會進(jìn)行一個內(nèi)部重定向到最后一個參數(shù)。

需要注意的是,只有最后一個參數(shù)可以引起一個內(nèi)部重定向,之前的參數(shù)只設(shè)置內(nèi)部URI的指向。最后一個參數(shù)是回退URI且必須存在,否則會出現(xiàn)內(nèi)部500錯誤。命名的location也可以使用在最后一個參數(shù)中。與rewrite指令不同,如果回退URI不是命名的location那么$args不會自動保留,如果你想保留$args,則必須明確聲明。

try_files $uri $uri/ /index.php?q=$uri&$args;

實(shí)例分析

示例一

try_files 將嘗試你列出的文件并設(shè)置內(nèi)部文件指向。

例如:

try_files /app/cache/ $uri @fallback; index index.php index.html;

它將檢測$document_root/app/cache/index.php,$document_root/app/cache/index.html 和 $document_root$uri是否存在,如果不存在著內(nèi)部重定向到@fallback(@表示配置文件中預(yù)定義標(biāo)記點(diǎn)) 。

你也可以使用一個文件或者狀態(tài)碼(=404)作為最后一個參數(shù),如果是最后一個參數(shù)是文件,那么這個文件必須存在。

示例二

例如nginx不解析PHP文件,以文本代碼返回

try_files $uri /cache.php @fallback;

因?yàn)檫@個指令設(shè)置內(nèi)部文件指向到 $document_root/cache.php 并返回,但沒有發(fā)生內(nèi)部重定向,因而沒有進(jìn)行l(wèi)ocation段處理而返回文本 。

(如果加上index指令可以解析PHP是因?yàn)閕ndex會觸發(fā)一個內(nèi)部重定向)

示例三

跳轉(zhuǎn)到變量

server { listen 8000; server_name 192.168.119.100; root html; index index.html index.php;  location /abc {   try_files /4.html /5.html @qwe;    #檢測文件4.html和5.html,如果存在正常顯示,不存在就去查找@qwe值} location @qwe {  rewrite ^/(.*)$  http://www.baidu.com;    #跳轉(zhuǎn)到百度頁面 }

示例四

跳轉(zhuǎn)指定文件

server {  listen 8000;  server_name 192.168.119.100;  root html;  index index.php index.html;  location /abc {    try_files /4.html /5.html /6.html; }

示例五

將請求跳轉(zhuǎn)到后端

upstream tornado {    server 127.0.0.1:8001;} server {    server_name imike.me;    return 301 $scheme://www.imike.me$request_uri;} server {    listen 80;    server_name www.imike.me;     root /var/www/www.imike.me/V0.3/www;    index index.html index.htm;     try_files $uri @tornado;     location @tornado {        proxy_pass_header Server;        proxy_set_header Host $http_host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Scheme $scheme;         proxy_pass http://tornado;    }}

常見錯誤

常見錯誤一

try_files 按順序檢查文件是否存在,返回第一個找到的文件,至少需要兩個參數(shù),但最后一個是內(nèi)部重定向也就是說和rewrite效果一致,前面的值是相對$document_root的文件路徑。也就是說參數(shù)的意義不同,甚至可以用一個狀態(tài)碼 (404)作為最后一個參數(shù)。如果不注意會有死循環(huán)造成500錯誤。

location ~.*/.(gif|jpg|jpeg|png)$ {    root /web/wwwroot;    try_files /static/$uri $uri;}

原意圖是訪問http://example.com/test.jpg時先去檢查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg

但由于最后一個參數(shù)是一個內(nèi)部重定向,所以并不會檢查/web/wwwroot/test.jpg是否存在,只要第一個路徑不存在就會重新向然后再進(jìn)入這個location造成死循環(huán)。結(jié)果出現(xiàn)500 Internal Server Error

location ~.*/.(gif|jpg|jpeg|png)$ {    root /web/wwwroot;    try_files /static/$uri $uri 404;}

這樣才會先檢查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg再不存在則返回404 not found

常見錯誤二

Nginx try_files $query_string為空的解決辦法

server {  listen 80;  server_name localhost.dev;  index index.php index.html index.htm;  set $root_path '/var/www/phalcon/public';   root $root_path;  location / {    try_files $uri $uri/ /index.php;  }  location ~ /.php$ {    try_files $uri =404;    fastcgi_split_path_info ^(.+/.php)(/.+)$;    fastcgi_pass 127.0.0.1:9000;    fastcgi_index index.php;    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;  }  location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {    root $root_path;  }  location ~ //.ht {    deny all;  }}

發(fā)現(xiàn)PHP無法獲取$_GET信息

try_files $uri $uri/ /index.php;

改為

try_files $uri $uri/ /index.php?$query_string;

即可解決,以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到服務(wù)器教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 河东区| 屏东县| 广西| 湛江市| 和顺县| 黔西县| 栖霞市| 沅陵县| 六安市| 文安县| 定陶县| 乐亭县| 玉林市| 绥化市| 黄平县| 岳普湖县| 梅州市| 黄大仙区| 汝南县| 思南县| 崇左市| 灌云县| 松溪县| 沁源县| 屏东县| 恩平市| 全南县| 竹北市| 乐陵市| 新巴尔虎右旗| 永安市| 枣强县| 永嘉县| 台前县| 漳州市| 德格县| 陇南市| 乌苏市| 岚皋县| 库尔勒市| 徐水县|