webman怎样实现用户输入http开头的url自动跳转到https开头的url

jones

【配置方法】
按照https://www.workerman.net/q/5796
的方法进行了配置,并且在webman的config/server.php中设置了监听443端口:
'listen' => 'http://0.0.0.0:443',

目前实现了https的访问。
注意的是:这里如果是https:开头,反而会报错,导致webman启动不了:Uncaught Exception: class \Protocols\Https not exist)。

我的问题:
1、当在浏览器输入http开头的地址时,就报错了:

无法访问此网站
拒绝了我们的连接请求。
请试试以下办法:

检查网络连接
检查代理服务器和防火墙
ERR_CONNECTION_REFUSED

2、有什么方法可以让用户输入http 开头的地址访问80端口时不报错,并且能自动跳转到https 开头的地址呢?


3、这种情况下,webman能同时监听80、443两个端口么?

1578 1 1
1个回答

six

用nginx代理,80端口自动跳转到443端口,也就是http跳转到https,完整nginx配置类似如下。你得有域名的https证书才行

upstream webman {
    server 127.0.0.1:8787;
}

server {
  server_name example.com  www.example.com;

  root /home/www/www.example.com/public;

  if ($host = example.com) {
      return 301 https://www.$host$request_uri;
  }

  location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      if (!-f $request_filename){
          proxy_pass http://webman;
      }
  }  

   listen 443 ssl; 
    ssl_certificate /your/path/of/www.example.com.pem;
    ssl_certificate_key /your/path/of/www.example.com.key;
    ssl_session_cache shared:le_nginx_SSL:1m;
    ssl_session_timeout 1440m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS";

}

server {
  listen 80;
  server_name example.com www.example.com;
  rewrite ^/(.*)$ https://www.example.net/$1 permanent;
}
  • jones 2022-05-26

    感谢您的回复,追问一下,如果想实现某些url只访问http的80端口,其它url全部转https该怎么实现?

年代过于久远,无法发表回答
×
🔝