2016-10-06 38 views
0

我試圖nginx的到達路線的所有請求開始/嵌入到/home/forge/dev.tline.io/embed/index.phpNgnix下載PHP

我的Nginx的配置:

location /embed { 
    root /home/forge/dev.tline.io; 
    try_files /embed/index.php =404; 
} 

location/{ 
    root /home/forge/dev.tline.io; 
    index index.html index.htm; 
    try_files $uri$args $uri$args/ $uri $uri/ /index.html =404; 
} 

location ~ \.php$ { 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 
} 

所有請求去/embed/index.php但不運行PHP文件下載它。

注:http://dev.tline.io/embed/index.php編譯沒有下載

我得到它,如果添加

fastcgi_split_path_info ^(.+\.php)(/.+)$; 
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
fastcgi_index index.php; 
include fastcgi_params; 

location /embed但應該有一個更好的方式來做到這一點

回答

0

請嘗試以下工作代碼,

map $request_uri $rot { 
    "~ /embed" /home/forge/dev.tline.io/embed/; 
    default /home/forge/dev.tline.io/; 
} 
map $request_uri $ind { 
    "~ /embed" index.php; 
    default index.html; 
} 
server { 
    ... 
    root $rot; 
    index index.php index.html index.htm; 
    ... 
    location/{ 
     try_files $uri$args $uri$args/ $uri $uri/ /$ind =404; 
    } 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 
    } 
    ... 
} 

如果這樣做無法解決問題,請嘗試切換$ind位置,並在發現多餘的'/'的情況下檢查錯誤日誌。

+0

我已刪除從'location'裏面的'root',但它並沒有改變什麼,我已經有了一個'location'外的''root'定義 – Amitay

+0

你把try_files放在php位置塊嗎? – Satys

+0

我補充說,也沒有工作。 如果我將php位置代碼添​​加到/ embed塊中,它可以工作,但它的一個難看的解決方案 – Amitay

0

這應該爲所有/嵌入的URL執行/embed/index.php:

server { 
    root /home/forge/dev.tline.io; 

    location/{ 
     index index.html index.htm; 
     try_files $uri$args $uri$args/ $uri $uri/ /index.html =404; 
    } 

    location /embed { 
     fastcgi_param SCRIPT_NAME $document_root/embed/index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root/embed/index.php; 
     fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     include fastcgi_params; 
    } 
} 
+0

我想要所有呼叫/嵌入/任何運行/embed/index.php 什麼我想使用默認位置塊 – Amitay

+0

我已經相應地調整了我的答案。 – iquito

+0

爲什麼不是內部重定向到'location〜\ .php $'塊? 爲什麼我必須將'fastcgi_params'添加到'/ embed'塊中? – Amitay