意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

nginx如何将http转成https

来源:恒创科技 编辑:恒创科技编辑部
2023-12-13 10:53:59
一、采用 nginx 的 rewrite 方法下面是将所有的 http 请求通过 rewrite 重写到 https 上。
例如将所有的 dev.wangshibo.com 域名的 http 访问强制跳转到 https。
下面配置均可以实现:
server {
    listen 80;
    server_name dev.wangshibo.com;
    index index.html index.php index.htm;

    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;

    # 方法1 这是ngixn早前的写法,现在还可以使用

    rewrite ^(.*)$  https://$host$1 permanent;

    # 方法2 这是nginx最新支持的写法

    # return  301 https://$server_name$request_uri;

    # 方法3 这种方式适用于多域名的时候,即访问wangshibo.com的http也会强制跳转到https://dev.wangshibo.com上面
    # 例如 server_name dev.wangshibo.com wangshibo.com *.wangshibo.com;

    # if ($host ~* "^wangshibo.com$") {
    #   rewrite ^/(.*)$ https://dev.wangshibo.com/ permanent;
    # }

    # 下面是最简单的一种配置
    # if ($host = "dev.wangshibo.com") {
    #   rewrite ^/(.*)$ /news/upload/ueditor/image/202208/02ur2o5jjp1.com permanent;
    # }

    location ~ / {
      root /var/www/html/8080;
      index index.html index.php index.htm;
    }
  }

上面的跳转配置rewrite ^(.*)$ https://$host$1 permanent;

也可以改为下面
rewrite ^/(.*)$ /news/upload/ueditor/image/202208/02ur2o5jjp1.com/$1 permanent;

或者
rewrite ^ /news/upload/ueditor/image/202208/02ur2o5jjp1.com$request_uri? permanent;

二、通过proxy_redirec方式
# re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;
参考链接Nginx 之 https 配置 - 运维笔记 (http->https 强转)Nginx百科全书-nginx 这一篇就够了


nginx如何将http转成https

上一篇: Kubernetes网络入门 下一篇: 网站搭建、域名和证书配置