使用php在nginx中配置https,不解释php

时间:2014-03-22 14:13:32

标签: php nginx https

当我尝试访问:https://www.mysite.com时,会下载一个php文件,但是当我访问https://www.mysite.com/index.php时,没有问题。

server {
listen 443;
server_name www.mysite.com;

ssl on;
ssl_certificate /etc/nginx/ssl/public.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;

ssl_session_timeout 5m;

root /var/www/mysite.com;
index index.php index.html index.htm;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ^~ /app/                { deny all; }
location ^~ /includes/           { deny all; }
location ^~ /lib/                { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/            { deny all; }
location ^~ /report/config.xml   { deny all; }
location ^~ /var/                { deny all; }

location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    deny all;
}

location ~ \.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;
    fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param HTTPS on;
}

}

所以,php不会被解释。

1 个答案:

答案 0 :(得分:0)

之前我遇到过类似的问题,只要发生404错误,页面就会显示index.php的源代码。

try_files $uri $uri/ /index.php?$query_string;

试试这个:

try_files $uri $uri/ =404;
error_page    404 = /index.php?$query_string;

如果问题仍然存在,请尝试添加以下内容:

location ~ ^/?$ {
    try_files $uri $uri/ index.php =404;
}

这是一个丑陋的黑客,但它在我的旧服务器上对我有用。由于某种原因,在我的新设置中不需要它。