簡體   English   中英

Nginx運行除index.php之外的所有php文件

[英]Nginx runs all php files, except index.php

所以在我的服務器上我有一個info.php文件,我可以正常運行,所有讀數看起來都不錯。 我也可以運行其他幾個文件,沒有問題。 Buuuuut,index.php作為包含所有PHP原始代碼的文件發送給我,這非常糟糕。 除此之外,進入基頁,根本不提供單個文件,特別是index.php。

這是我的nginx配置:

server {
        server_name somedamnserver;
        root /var/www;
        index index.php index.html;

        location / {
                # This is cool because no php is touched for static content
                try_files $uri $uri/ /index.php;
        }

        location ~ \.php$ {
                #fastcgi_split_path_info ^(.+\.php)(/.+)$;
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_intercept_errors on;
                include fastcgi_params;
        }
}

我的php.ini和fpm / pool.d / www.conf文件似乎設置正確,但我也可以在這里鏈接它們。

我在這里結束了我的智慧,我完全不明白為什么這個服務器的惡霸會對我這么做。 :(

這是由兩個問題引起的。

首先,當你執行try_files $uri $uri/ /index.php; 因為index.php是最后一個元素,nginx重新處理整個服務器塊。

其次,因為第一個位置塊是/匹配/index.php ,所以第二個位置塊被重新處理為原始文件。

你幾乎肯定不想像你那樣寫你的積木。 您應該明確列出您的靜態內容類型,它們將作為原始文件顯示給服務器。 其他所有內容都應該傳遞給PHP后端。 例如

location ~* ^[^\?\&]+\.(html|jpg|jpeg|json|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|svg|woff|ttf)$ {
    try_files $uri /index.php?file=$1;

    #access_log off;
    expires 24h;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}


location  / {
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            set $originalURI  $uri;
            try_files $uri /index.php /50x_static.html;
            fastcgi_param  QUERY_STRING  q=$originalURI&$query_string;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_intercept_errors on;
            include fastcgi_params;
 } 

請注意,在最后一個塊中,有一個小的jiggery pokery來保存查詢字符串,您可能需要也可能不需要。

btw啟用rewrite_log on; 可以幫助您解決類似的問題。

在研究了日志之后,我發現了實際問題是什么,感謝Danack建議重寫日志選項。 所以問題主要是PHP依賴,如php5-json和php5-mysql更精確(不知道其中任何一個被卸載)然后以mysql服務器設置問題結束,這顯然意味着127.0。 0.1和localhost是不一樣的東西。

顯然頁面空白或不存在的原因是因為他們的500錯誤頁面要么丟失,要么在我的安裝中只是空白。

希望這對其他人有所幫助,我現在為這一周努力了。

更新:再次向Danack致敬,他們基本上寫了這個配置。 這是我目前正在使用的工作配置,雖然不是很安全,但它可以工作。 需要兩種類型的文件類型解決方案,否則您將無法訪問該靜態內容。 啟用了自動gzipping,但建議對其添加限制,甚至概述它拉鏈的內容。

server {
        server_name somedamnserver;
        root /var/www;
        rewrite_log on;

        gzip_static on;
        # This is file type association solution 1
        # location ~ \.css {
            # add_header  Content-Type    text/css;
        # }
        # location ~ \.js {
            # add_header  Content-Type    application/x-javascript;
        # }
        # This is file type association solution 2
        location ~* ^[^\?\&]+\.(html|jpg|jpeg|json|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|svg|woff|ttf)$ {
            try_files $uri /index.php?file=$1;
            #access_log off;
            expires 24h;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
        # This will make PHP files run as intended, I have uncommented a few options as I do not need them.
        location  / {
            #set $originalURI  $uri;
            try_files $uri /index.php /50x_static.html;
            #fastcgi_param  QUERY_STRING  q=$originalURI&$query_string;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            #fastcgi_intercept_errors on;
            include fastcgi_params;
        }
}

暫無
暫無

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

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