Heim  >  Artikel  >  Backend-Entwicklung  >  PHP https entfernen

PHP https entfernen

王林
王林Original
2023-05-28 18:27:08966Durchsuche

随着互联网的快速发展,https已经逐渐成为了主流的网络连接方式。因为其能够为用户提供更为安全的数据传输和防止窃听的保障。然而,对于一些特定的开发场景,可能需要在php中将https去掉,本文将详细介绍如何实现这一需求。

一、什么是https

HTTPS全称为Hyper Text Transfer Protocol Secure,即是HTTP的安全形式。被广泛应用于安全性要求比较高的场景,如电子商务、网络银行、教务系统等。其通过SSL/TLS协议来保证网络数据传输的安全性,防止黑客和中间人攻击。

二、什么情况下需要去掉https

  1. 部分网站对性能要求比较高,https会降低web服务器的性能,去掉https能够提高网站的性能;
  2. 有些项目未考虑到安全性问题,待协议修改后未作相应的修改,不能直接使用https协议。
  3. 部分网站采用第三方平台提供的框架开发,这样不可控的第三方服务可能会影响他们的协议,导致开发者需要禁用https协议。

三、如何去掉https

  1. 修改网站配置文件

绝大部分的Web服务器应用都是从HTTP升级至HTTPS的。然而,恢复回HTTP也是同样可行的。在这个过程中,你无须修改代码,仅需改动web服务器应用配置文件即可。 以Nginx为例:

在http部分监听80端口,server部分重新定义相关配置

server{
    listen 80;
    server_name www.yourdomain.com;
    rewrite ^(.*) https://$server_name$1 permanent;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
}

server{
    listen 443 ssl;
    server_name www.yourdomain.com;
    ssl_certificate /etc/nginx/certs/yourdomain.crt;
    ssl_certificate_key /etc/nginx/certs/yourdomain.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers on;
    location / {
       proxy_pass http://127.0.0.1:8080;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

通过修改上述配置中的server段的监听端口号即可禁用https。

  1. 修改php.ini文件

也可以直接修改php.ini文件:

extension=php_openssl.dll

改为

;extension=php_openssl.dll

这样php代码中所有的https请求都将失效。

  1. 在php代码中禁用https

对于某些特定的php业务场景,需要在php代码中直接禁用https协议,代码如下:

$header = [
    'Content-Type: application/json',
];
$options = [
    'http' => [
        'header' =>$header,
        'method' => 'POST',
        'content' => json_encode($postData),
        'timeout' => 15 * 60, // 设置超时时间
        'ignore_errors' => true,
    ],
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
    ],
];
$context = stream_context_create($options);
// 发送请求
$res = file_get_contents($url, false, $context);

通过使ssl参数中的verify_peer和verify_peer_name为false来禁用https协议。

四、总结

https虽然已成为主流的网络连接方式,但在部分业务场景下,禁用https具有一定的必要性。上述三种禁用方式中,建议使用第一个方法,即通过修改web服务器应用配置文件来禁用https,通用性更加广泛。若禁用了https协议,一定要格外注意数据的安全哦!

Das obige ist der detaillierte Inhalt vonPHP https entfernen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn