Nginx在运行.PHP文件时在浏览器中显示HTML代码

时间:2018-01-05 18:55:11

标签: php html nginx webserver

当我运行此代码时

<html>
<head> 
<title></title>
</head>
<body>
   <?php
        header('Content-type:application/json;charset=utf-8');
        $html = file_get_contents('https://api.com/auth');
        $json = json_decode($html,true);     
        $token= $json['token'];  
        $postdata = http_build_query(array('token' => $token));                          

        $opts = array('http' =>array('method'=>'POST','header'=>'Content-type: application/x-www-form-urlencoded','content'=>$postdata));                       
        $context = stream_context_create($opts);
        // display results
        $result=file_get_contents('https://api.com/api-mc', null, $context);                                
        $json_result = json_decode($result,true);
        echo $json_result["USD"]["SELL"];
   ?>
</body>
</html>

在我本地php服务器的一个.php文件中(在os x终端&#34; php -S localhost:9000&#34;),它只显示PHP代码的输出。但是当我在Digital Ocean服务器上运行NGINX时,它会显示PHP代码的正确输出,但它也会显示html标签本身。 HTML没有被解释。我怀疑问题出在我的NGINX配置文件中:

server {
    listen 80;
    listen [::]:80;

    root /var/www/example;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name example.com www.example.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?q=$uri&$args;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

谢谢

0 个答案:

没有答案