Nginx下载PHP文件而不是显示它们(不是套接字问题)

时间:2015-01-16 15:47:53

标签: php nginx vps

所以我正在设置我的第一个Linode(一种新的自己管理一切)。但是,我有以下问题。浏览器下载php文件而不是执行它,MS Internet Explorer显示文件的内容而不是下载。

我已经阅读了很多有关此问题的内容/答案,但似乎没有任何效果,所以我感谢您的帮助。

重要的是要注意网站"崩溃"仅当我将以下行添加到虚拟主机文件

location ~* .(ico|jpg|webp|jpeg|gif|css|png|js|ico|bmp|zip|woff)$ { expires 365d; }

以下是完整的两个文件 的 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 10s;
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;

##
# SSL Settings
##

# ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
# ssl_prefer_server_ciphers on;

##
# Logging Settings
##

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

##
# 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/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

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

}

以及网站可用/默认文件

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

root /www/bloggingwithdani.com;

index index.html index.php index.htm;

server_name localhost;

# pagespeed On;
# pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
# pagespeed EnableFilters combine_css,combine_javascript;

location / {
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~* .(ico|jpg|webp|jpeg|gif|png|ico|bmp|zip|woff|css|js|)$ {
expires 365d;
}


location ~ /\. {
deny all;
}

location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}

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

}

}

1 个答案:

答案 0 :(得分:0)

你的php位置块看起来对我不对。这是我的php位置块

location ~ \.(php)$ {

    try_files $uri = 404;
    location ~ \..*/.*\.php$ {return 404;}
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_keep_conn on;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    # fastcgi_pass   127.0.0.1:9000; #passing directly to the socket
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;

}

另外,您的静态文件缓存错误,并且具有错误的通配符参数。删除最后一个|,并可选择添加一些额外的配置选项,以进一步优化静态内容的传送。

location ~* .(ico|jpg|webp|jpeg|gif|png|ico|bmp|zip|woff|css|js)$ {

    expires max;
    add_header Vary Accept-Encoding;
    access_log off;

}