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


12

我已经撞了两天了。问题是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

我也在相同的问题上尝试了这些解决方案,但它们对我没有用。

Answers:



8

确保您安装了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


fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;似乎在此块中发了短信。参见:stackoverflow.com/q/23443398/2010467
LiveWireBT

4
我的位置块与此相同,除了用include fastcgi.conf代替fastcgi_params(我在ubuntu 14.10上),并且它仍在下载PHP文件而不是执行它们。
nnyby 2014年

0

首先,您必须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

然后转到浏览器并享受...


0

检查fpm php-mysql服务器上和php-fpm.sock配置文件中安装的版本。

sudo nano /etc/nginx/sites-enabled/example.com

FPM版本应相同。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.