【问题标题】:PHP doesn't work with https, instead it download filePHP不适用于https,而是下载文件
【发布时间】: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;
}
}

【问题讨论】:

标签: php nginx https centos


【解决方案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; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 2013-07-02
  • 2017-11-16
  • 2011-11-18
  • 2017-04-13
  • 2021-10-11
相关资源
最近更新 更多