[英]nginx configuration for a RESTful API
我是nginx和php的初學者,所以請原諒我的基本問題。
對於基於RESTful的API(nginx + php),我需要一些關於nginx配置的幫助。
以下是nginx配置的相關片段(如此處所示 ),用於將所有/ api / v1 / *請求重定向到我的apiv1.php腳本:
server {
server_name myServer;
root /usr/share/nginx/html;
location /api/v1/ {
try_files $uri $uri/ /apiv1.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
現在的問題是,當我在瀏覽器中輸入http:// myServer // api / v1 / resource / GetInfo時,apiv1.php腳本似乎沒有收到“resource / GetInfo”。 實際上,_GET和_REQUEST是空的,但_SERVER看起來還不錯!
在我的/etc/php5/fpm/php.ini中,啟用了以下相關配置:
request_order = "GP"
variables_order = "GPCS"
register_argc_argv = Off
auto_globals_jit = On.
你可能知道為什么php _GET和_REQUEST是空的嗎? 這只與我的php配置有關嗎?
最好的問候,M。
替換這個:
location /api/v1/ {
try_files $uri $uri/ /apiv1.php?$args;
}
在服務器塊中包含以下內容:
rewrite ^/api/v1/([^/]+)/([^/]+)/?$ /apiv1.php?class=$1&method=$2? last;
創建一個名為apiv1.php的php文件,並使用以下代碼行放在Web服務器的根目錄中:
<?php
$class = filter_input(INPUT_GET, 'class', FILTER_SANITIZE_STRING);
$method = filter_input(INPUT_GET, 'method', FILTER_SANITIZE_STRING);
echo $class;
echo '<br />';
echo $method;
通過訪問瀏覽器中的以下鏈接進行測試:
http://myServer/api/v1/members/getInfo
如果其他人點擊此頁面,經過一番研究后我會得到解決方案:
location ~ ^/api/v0/(.*)/?$ {
try_files $uri $uri/ /v0.php?req=$1&$args;
}
這里我不限於類/方法結構,位置似乎比重寫更具可讀性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.