为什么index.php会被下载而不是被Nginx渲染?

为什么index.php会被下载而不是被Nginx渲染?

我已经为此绞尽脑汁两天了。问题是index.php被下载了,而不是被处理了。我正在使用Ubuntu 14.04Nginxphp5。 如下所示。我按照许多帖子的要求nginx/site-available/default添加了条目。index.php

    server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }


    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #   root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #   # With php5-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php5-fpm:
    #   fastcgi_pass unix:/var/run/php5-fpm.sock;
    #   fastcgi_index index.php;
    #   include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}



    #server {
    #   listen 8000;
    #   listen somename:8080;
    #   server_name somename alias another.alias;
    #   root html;
    #   index index.php index.html index.htm;
    #
    #   location / {
    #       try_files $uri $uri/ =404;
    #   }
    #}


    # HTTPS server
    #
    #server {
    #   listen 443;
    #   server_name localhost;
    #
    #   root html;
    #   index index.php index.html index.htm;
    #
    #   ssl on;
    #   ssl_certificate cert.pem;
    #   ssl_certificate_key cert.key;
    #
    #   ssl_session_timeout 5m;
    #
    #   ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    #   ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    #   ssl_prefer_server_ciphers on;
    #
    #   location / {
    #       try_files $uri $uri/ =404;
    #   }
    #}

如下nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
    }

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

解决方案一直延续到现在。

  1. 添加了条目index.php
  2. default_type application/octet-stream;删除了nginx.conf 中的行
  3. 取消注释文件fastcgi中的条目default

我也针对同一问题尝试了这些解决方案,但它们对我没有作用。

答案1

有时它可以简单到清除浏览器缓存。如果存在某些配置错误但确实正在下载文件,则在修复配置后,需要告知浏览器停止使用缓存下载。

答案2

确保你安装了 php-fpm,

sudo apt-get update && sudo apt-get install php5-fpm

现在编辑你的nginx/site-available/default文件并location ~ \.php$阻止

 location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

重启 nginx 服务

sudo service nginx reload

并重新加载 php5-fpm

sudo service php5-fpm restart

答案3

首先你必须Remove cache在浏览器中

然后打开终端并运行以下命令:

sudo apt-get install php-gettext
sudo nano /etc/nginx/sites-available/default

然后在文件中添加如下代码default

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

如果有任何不匹配,只需通过以下命令从终端更正并重新启动 Nginx

sudo systemctl 重启 nginx

然后进入浏览器并享受...

答案4

可能有两个原因

  1. 服务器配置错误. 修复配置,清除浏览器缓存。

如果您正在使用 c-panel 并且更改了网站的 PHP 版本(那么 c-panel 将创建一个具有某些配置的 .htaccess 文件)但是上传新.htaccess文件时将替换旧文件(由 c-panel 创建)。

  1. 浏览器缓存.解决方法很简单,清除浏览器缓存即可。

相关内容