【问题标题】:Configure https in nginx with php, the php is not interpreted在nginx中用php配置https,php没有被解释
【发布时间】:2014-04-29 23:55:27
【问题描述】:

当我尝试访问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 没有被解释。

【问题讨论】:

    标签: php nginx https


    【解决方案1】:

    我之前也遇到过类似的问题,每当发生 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;
    }
    

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

    【讨论】: