问题:ssl证书安装后Nginx不运行php

我刚刚为一个新站点设置了 ubuntu 18.04 服务器。问题是通过 certbot 安装 ssl 证书后 php 停止工作。

这是位于 /etc/nginx/sites-available 的 example.com 配置文件:

server {

        root /var/www/example.com/html;
        index index.html index.htm index.php index.nginx-debian.html;

        server_name example.com www.example.com;

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

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name example.com www.example.com;
        return 404; # managed by Certbot


}

现在https://example.com返回 application/octet-stream 而不是运行 index.php

我检查了防火墙是否允许连接:lsof -i :443

nginx   30662     root   13u  IPv6  26978      0t0  TCP *:https (LISTEN)
nginx   30662     root   14u  IPv4  26979      0t0  TCP *:https (LISTEN)
nginx   30665 www-data   13u  IPv6  26978      0t0  TCP *:https (LISTEN)
nginx   30665 www-data   14u  IPv4  26979      0t0  TCP *:https (LISTEN)

系统:

Ubuntu 18.04.3 LTS x86_64
nginx version: nginx/1.14.0
PHP Version 7.0.33-0ubuntu0.16.04.7
firewall: ufw 0.36

解答

您必须将此指令添加到您的配置 nginx :

location ~* \.php$ {
    #you have to put the path your php-fpm socket file
    fastcgi_pass unix:/path_to_your_socket_file/php7.3-fpm.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

然后你必须重新启动你的 nginx 服务。

来源:https://www.linode.com/docs/web-servers/nginx/serve-php-php-fpm-and-nginx/

让我知道是否有问题

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐