You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP8.1.17+Laravel10+Nginx环境下加密相关报错求助

Laravel 10 + Nginx环境下加密相关报错处理

环境信息

  • PHP 8.1.17
  • Laravel 10
  • Nginx 1.24(Windows环境)

报错情况

  1. 浏览器访问应用时,初始报错:Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
  2. 修改config/app.php中'cipher'为'AES-128-CBC'并重新执行php artisan key:generate后,报错变为:Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.

已排查内容

  • php.ini中已启用openssl扩展,php --ini显示加载的配置文件为E:\xampp\php\php.ini,extension_dir设置为"E:\xampp\php\ext"
  • 使用tinker执行openssl_cipher_iv_length('AES-128-CBC')可正常返回16
  • 已执行php artisan key:generate命令,初始cipher配置为'AES-256-CBC',修改后为'AES-128-CBC'
  • 查阅过多数相关方案,但均针对PHP+Apache环境,当前使用Nginx,请勿标记为重复问题

Nginx配置信息

server {
        listen       8282 ssl;
        #server_name  localhost;
        server_name  abc.example.com:8282;
        client_max_body_size 11M;
        #more_clear_headers Server;
        server_tokens off;
        
        ssl_certificate      example_com.crt;
        ssl_certificate_key  private-key2022.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        
        error_page 404 /html/404.html;
        location = /404.html {
        root /nginx-1.24.0/html;
        internal;
        }
        
        error_page 405 500 502 503 504 /500.html;
        location = /500.html {
        root /nginx-1.24.0/html;
        internal;
        }
        
        root "E:/project/public";
        index index.php;
        
        if ($request_method ~ ^(OPTIONS|HEAD)$ )
        {
            return 405;
        }
        
        location / {
        try_files $uri $uri/ /index.php$is_args$args;
        }
        
        location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME     $fastcgi_script_name;
        include fastcgi_params;
    }
}

解决方案

1. 确认PHP-FPM与CLI使用的php.ini一致

CLI下的php.ini和PHP-FPM加载的可能不是同一个文件:

  • 在项目public目录创建phpinfo.php,内容为:
<?php phpinfo(); ?>
  • 浏览器访问该文件,查看Loaded Configuration File项,对比php --ini的输出路径。若不一致,修改PHP-FPM配置文件(通常是php-fpm.conf或对应pool配置),指定正确的php.ini路径,然后重启PHP-FPM和Nginx。

2. 验证PHP-FPM中openssl扩展已启用

通过phpinfo.php页面搜索openssl,确认OpenSSL support为enabled。若未启用:

  • 检查php.ini中extension=openssl是否被注释(去掉前面的;)
  • 确认extension_dir路径正确,且php_openssl.dll存在于该目录
  • 重启PHP-FPM和Nginx

3. 清理缓存并重新生成密钥

执行以下命令:

php artisan cache:clear
php artisan config:clear
php artisan key:generate --force

注:--force会覆盖现有APP_KEY,若有依赖旧密钥的加密数据请提前备份。

4. 检查Nginx路径配置

确认Nginx的fastcgi_param SCRIPT_FILENAME配置正确,$document_root指向项目public目录的绝对路径,可尝试统一使用正斜杠路径格式。

5. 匹配cipher与密钥长度

Laravel的APP_KEY长度需对应cipher类型:

  • AES-128-CBC对应16字节密钥(base64编码后22字符)
  • AES-256-CBC对应32字节密钥(base64编码后44字符)
    确保config/app.php中cipher配置正确后,再执行key:generate命令。

内容的提问来源于stack exchange,提问作者user2338456

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 13:30:03