簡體   English   中英

NGINX下載php文件

[英]NGINX downloads php file

我有自己的MVC,其中包含許多具有此文件夾結構的應用程序:

  • 主要項目
    • 的index.php
    • 型號(目錄)
    • 查看(目錄)
    • 控制器(目錄)
    • 子項目1(目錄)
      • 的index.php
      • 型號(目錄)
      • 查看(目錄)
      • 控制器(目錄)
    • 子項目2(目錄)
      • 的index.php
      • 型號(目錄)
      • 查看(目錄)
      • 控制器(目錄)

apache一切正常。 我想使用Nginx嘗試它,但是當我在瀏覽器中訪問它時,它會下載index.php,但它不執行它。

這是我在nginx上的設置:

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            if (!-e $request_filename){ rewrite ^(.*)$ /index.php?rt=$1 break; }
            try_files $uri $uri/ /index.php$is_args$args;
    }

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

兩行:

if (!-e $request_filename){ rewrite ^(.*)$ /index.php?rt=$1 break; }
try_files $uri $uri/ /index.php$is_args$args;

試圖執行相同的功能。 如果文件不存在,請嘗試/index.php

第一行由於break失敗,它嘗試在location /塊而不是location ~ \\.php$塊中處理/index.php

要更正第一行,應使用last而不是break 有關詳細信息,請參見此文檔

但是,如果包含try_files行,則不需要if ... rewrite語句。 以下執行類似的功能:

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

我更喜歡try_files方法,但是任何一種都可以,但是您不需要兩者。 try_files 在此處記錄 注意在使用if塊時的注意事項

另外,請注意您location ~ \\.php$某些問題location ~ \\.php$塊:

  • 您在正則表達式中缺少反斜杠
  • try_files指令應以URI或= 404結尾-不能同時

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM