iT邦幫忙

0

nginx rewrite 的「File not found.」問題

我嘗試將 htaccess 的設定檔丟到線上去轉成 nginx 的
直接轉的完全不能用
於是我參考了一下原理去改成這樣

location / {
    rewrite ^/admin/index$ /admin/templates/index.php;
    rewrite ^/admin/handlers/([\w\-]+)$ /admin/handlers/$1.php;
    rewrite ^/$ /public/templates/index.php;
    }

我發現 /, /admin/index 正常
但是我發現 /admin/handlers/ 失效了,他會說「File not found.」
其實在該目錄下是有檔案的
反而要在後面加上 .php 才會有反應,例如「/admin/handlers/signin.php」
這是哪裡設定錯誤了?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
froce
iT邦大師 1 級 ‧ 2019-11-22 10:32:11
最佳解答

我測過了,沒問題...
你檔案擺錯位置了吧?

下面是測試參數,為了簡單測試我把php改txt:
nginx.conf

location / {
            root   html;
            rewrite ^/admin/index$ /admin/templates/index.txt;
            rewrite ^/admin/handlers/([\w\-]+)$ /admin/handlers/$1.txt;
            rewrite ^/$ /public/templates/index.txt;
        }

html資料夾結構

html
  |-admin
  |  |-templates
  |  |  |-index.txt
  |  |-handlers
  |  |  |-signin.txt
  |  |  |-signout.txt
  |-public
  |  |-templates
  |  |  |-index.txt

測試過的網址:

http://127.0.0.1/admin/index
http://127.0.0.1/admin/handlers/signin
http://127.0.0.1/admin/handlers/signout
http://127.0.0.1/
看更多先前的回應...收起先前的回應...
火爆浪子 iT邦研究生 1 級 ‧ 2019-11-22 12:19:46 檢舉

我確定是對的耶,怎麼回事⋯⋯

火爆浪子 iT邦研究生 1 級 ‧ 2019-11-22 12:28:52 檢舉

不過我原本是這樣

location / {
        rewrite ^/admin/index$ /admin/templates/index.php;
        rewrite ^/admin/([\w\-]+)$ /admin/templates/$1.php;
        rewrite ^/admin/([\w\-]+)/([\w\-]+)$ /admin/templates/$1/$2.php;
        rewrite ^/admin/search/([\w\-]+)$ /admin/templates/search/index.php?search_query=$1;
        rewrite ^/admin/handlers/([\w\-]+)$ /admin/handlers/$1.php;
        rewrite ^/$ /public/templates/index.php;
        rewrite ^/([\w\-]+)$ /public/templates/$1.php;
        rewrite ^/([\w\-]+)/([\w\-]+)$ /public/templates/$1/$2.php;
        rewrite ^/handlers/([\w\-]+)$ /public/handlers/$1.php;
        rewrite ^/common/env/([\w\-]+)$ /common/env/$1.php;
        rewrite ^/common/handlers/([\w\-]+)$ /common/handlers/$1.php;
    }
froce iT邦大師 1 級 ‧ 2019-11-22 12:56:53 檢舉

這時候就要看log啦...apache也一樣吧?
一開始測試的時候錯誤的log。

2019/11/22 10:15:13 [error] 12436#29592: *1 CreateFile() "D:\nginx-1.17.3/html/admin/handlers/signin.txt" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /admin/handlers/signin HTTP/1.1", host: "127.0.0.1"

把所有的「/」跟「.」都前置一個「\」再試試。

froce iT邦大師 1 級 ‧ 2019-11-22 15:55:23 檢舉

nginx的應該不用,至少在windows下的不用

火爆浪子 iT邦研究生 1 級 ‧ 2019-11-27 10:40:41 檢舉

https://imgur.com/EbVsiDE
https://imgur.com/XzlmYUg
https://imgur.com/7n3QD1h
都試過了,都是給我「File not found.」

froce iT邦大師 1 級 ‧ 2019-11-27 15:01:05 檢舉

說過了,你這個要看error log,自己學著看debug才會進步。

我要發表回答

立即登入回答