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

Posted

技术标签:

【中文标题】PHP不适用于https,而是下载文件【英文标题】:PHP doesn't work with https, instead it download file 【发布时间】:2019-07-22 13:07:45 【问题描述】:

我使用 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;


【问题讨论】:

***.com/questions/25591040/… 这可能会有所帮助 @pr1nc3 嗯,它没有解决我的问题 【参考方案1】:

您的 php 位置标记仅存在于 listen 80 服务器块中。 因此,如果请求被发送到443 服务器块,它不会处理任何 php 文件。您应该将相同的 php 块添加到 443 server 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 
        
    

【讨论】:

您在 nginx 日志文件中看到任何错误吗?如果是这样的话;请将它们添加到您的原始帖子中 我成功了,我需要添加index index.php index.html index.htm; location / try_files $uri $uri/ =404;

以上是关于PHP不适用于https,而是下载文件的主要内容,如果未能解决你的问题,请参考以下文章

PHP LongBlob上传/下载不适用于图片

mPDF 库不适用于 php7.2 codeigniter

如何用PHP下载大文件?

CSS不适用于包含的PHP文件

AddHandler 不适用于 .php 文件 - Apache 2.4

动态 PHP 变量不适用于使用 mPDF 的 codeigniter php 视图文件