• centos7通过nginx搭建SSL


    今天给大家带来的是一篇关于通过nginx搭建HTTPS访问转跳后端HTTP的教程,部署方式如下:

    安装基础组件
    yum -y isntall firewalld
    yum -y install gcc gcc-c++
    yum -y install pcre-devel
    yum -y install zlib-devel
    yum -y install openssl openssl-devel
    1
    2
    3
    4
    5
    下载源码与编译
    下载与代码(假设当前在 ~/ 目录下)

    wget https://nginx.org/download/nginx-1.12.2.tar.gz
    wget https://www.openssl.org/source/openssl-1.0.2n.tar.gz
    1
    2
    解压

    cd /opt
    tar zxf nginx-1.12.2.tar.gz
    tar zxf openssl-1.0.2n.tar.gz
    1
    2
    3
    编译前配置,让nginx支持ssl_module与openssl

    cd nginx-1.12.2
    ./configure --with-http_ssl_module --with-openssl=/opt/openssl-1.0.2n
    1
    2
    编译

    make
    make install
    1
    2
    安装完成后的nginx路径是: /usr/local/nginx

    配置环境
    到腾讯云申请证书(自创建证书参看网上其他资料)


    把nginx目录下的2个文件复制到 /usr/local/nginx/conf 目录下

    配置nginx

    vim /usr/local/nginx/conf/nginx.conf
    1
    在nginx.conf中增加HTTP/HTTPS配置

    upstream tomcat {
    server 127.0.0.1:9081 fail_timeout=0; #后端服务地址
    }
    server {
    listen 443;
    ssl on;
    server_name host.httpsDomain.com; #申请证书的域名

    ssl_certificate 1_host.httpsDomain.com_bundle.crt; #证书压缩包中nginx文件夹下crt文件,相对路径是在 [/usr/local/nginx/conf]
    ssl_certificate_key 2_host.httpsDomain.com.key; #证书压缩包中nginx文件夹下crt文件,相对路径是在 [/usr/local/nginx/conf]
    ssl_session_timeout 5m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3; #指定SSL服务器端支持的协议版本
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #指定加密算法
    ssl_prefer_server_ciphers on; #在使用SSLv3和TLS协议时指定服务器的加密算法要优先于客户端的加密算法

    location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto https;
    proxy_redirect off;
    proxy_connect_timeout 240;
    proxy_send_timeout 240;
    proxy_read_timeout 240;
    # note, there is not SSL here! plain HTTP is used
    proxy_pass http://tomcat;
    }
    }
    server {
    listen 80;
    server_name host.httpsDomain.com; #http访问入口
    location / {
    rewrite ^ https://$http_host$request_uri? permanent; #强制跳转到HTTPS上
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    保存退出

    :wq
    1
    启动nginx

    #启动
    /usr/local/nginx/sbin/nginx

    #重启
    /usr/local/nginx/sbin/nginx -s reload

    #关闭
    /usr/local/nginx/sbin/nginx -s stop
    1
    2
    3
    4
    5
    6
    7
    8
    9
    配置防火墙

    把firewalld注册成服务并启动

    systemctl enable firewalld
    systemctl start firewalld
    1
    2
    允许TCP协议下的 80,443 端口 暴露到互联网

    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --zone=public --add-port=443/tcp --permanent
    firewall-cmd --reload
    1
    2
    3
    运行测试
    检查防火墙端口

    firewall-cmd --list-ports
    #看看输出有没有80/tcp与443/tcp
    1
    2
    PS:如果用的是用阿里云/腾讯云,则还需要到XX云控制台检查安全策略组是否开放了TCP下的80与443

    检查nginx服务

    ss -ntlp | grep nginx
    #确认一下 80/tcp 跟 443/tcp 是否被nginx所使用
    1
    2
    浏览器测试

    如果大家有什么问题或建议,欢迎留言 ^_^

  • 相关阅读:
    18种典型算法
    幂法和反幂法
    关于Ubuntu下安装Win8和Win8下安装Ubuntu的注意事项
    静态链接库与动态链接库
    面向对象系列二(封装)
    基于ASP.NET WPF技术及MVP模式实战太平人寿客户管理项目开发(Repository模式)
    不要对终于用户谈云
    cocos2d-x 3.0 创建项目
    Android OpenGL ES 画球体
    设计模式 适配器模式 以手机充电器为例
  • 原文地址:https://www.cnblogs.com/valu/p/10527287.html
Copyright © 2020-2023  润新知