這裡記錄一下,在 Nginx + PHP 環境無法取得 Http Get Request 的問題
在本機端,使用的是 apache 環境,會需要取得網址上的Get參數
public function index(Request $request) {
$message = $request->message;
...
}
上傳至 Server Side (Nginx 環境) 卻發現無法取得參數
嘗試取得完整網址與$_GET是否能取得值
這時先測看看完整網址以及檢查$_GET
public function index(Request $request) {
echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
print_r($_GET);
確定在 localhost 確實能完整捕捉到
Localhost 結果如:
http://localhost:8000?message=hello
Array
(
[message] => hello
)
但是在 server 上面有捕捉到完整網址,但同樣輸出的是空的 $_GET
Server 輸出結果如:
http://example.com?message=hello
Array
(
)
檢查Server side 設定
正式 Server 使用的是 Nginx
前往Server 設定中,檢查一下 nginx.conf ,發現原因是
try_files 沒有配置到 get 參數
location / {
try_files $uri $uri/ /index.php;
}
改成下面這樣就能正常取得 get params
location / {
try_files $uri $uri/ /index.php?$query_string;
}
或者
location / {
try_files $uri $uri/ /index.php?$args;
}
聲明,近期發現有平台大量盜用文章內容,若您發現內文非來自關於網路那些事 [https://adon988.logdown.com](https://adon988.logdown.com) ,希望您與我聯繫 [https://www.facebook.com/ThinkingWebsite](https://www.facebook.com/ThinkingWebsite)
(本文章源自"關於網路那些事)
最後,如果你喜歡這篇文章,請幫忙點個讚
Tweet