PHP 不适用于 https,而是下载文件

标签 php nginx https centos

我将 nginx 作为 PHP-fpm 的网络服务器。我可以使用 http 成功查看我的网站,但如果我使用 https,我将下载索引页面,并且该页面不会显示。

我的 nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    server_tokens off;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    fastcgi_read_timeout 300s;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    client_max_body_size 2M;

    include /etc/nginx/conf.d/*.conf;
}

    server {
        listen 443 ssl;
        server_name  www.domain.net;
        root         /usr/share/nginx/html;


        add_header X-Frame-Options "SAMEORIGIN";
        ssl_certificate /etc/letsencrypt/live/www.domain.net/fullchain.pem; 
        ssl_certificate_key /etc/letsencrypt/live/www.domain.net/privkey.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
        ssl_prefer_server_ciphers on;

        location / {
            index index.php;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

和conf.d/default.conf

server {
listen   80;
server_name  www.domain.net;

root   /usr/share/nginx/html;
index index.php index.html index.htm;

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_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
}

最佳答案

您的 php 位置标记仅存在于 listen 80 服务器 block 中。 因此,如果请求发送到 443 服务器 block ,它不会处理任何 php 文件。您应该将相同的 php block 添加到 443 服务器 block

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    server_tokens off;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    fastcgi_read_timeout 300s;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    client_max_body_size 2M;

    include /etc/nginx/conf.d/*.conf;
}

    server {
        listen 443 ssl;
        server_name  www.domain.net;
        root         /usr/share/nginx/html;


        add_header X-Frame-Options "SAMEORIGIN";
        ssl_certificate /etc/letsencrypt/live/www.domain.net/fullchain.pem; 
        ssl_certificate_key /etc/letsencrypt/live/www.domain.net/privkey.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
        ssl_prefer_server_ciphers on;

        location / {
            index index.php;
        }

        # NEW
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
          }
        ## NEW

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

关于PHP 不适用于 https,而是下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54928717/

相关文章:

php - 如何在 Apache 服务器(和其他服务器)上的 PHP 中检测对 .htaccess 的支持

wordpress - 禁用我网站上的所有 HTTPS 页面

php - LOAD DATA INFILE + 进度跟踪

php - 如何在 Laravel 中对数组的值进行排序

php - 对 Laravel 的简单 Ajax 请求不起作用

jquery - Ruby 瘦服务器 ActionView::模板错误

ssl - Kubernetes: Nginx Ingress注解----> nginx.ingress.kubernetes.io/secure-backends

python - 如何在 Kubernetes 集群中部署简单的 python HTTPS 服务器?

jsf - session bean 在 HTTPS-DNS 组合上丢失

php - 如何从客户端接收 json 对象到服务器的 php 二维数组中?