zoukankan      html  css  js  c++  java
  • Nginx设置支持Https

    记录一下Nginx支持Https的配置

    前期准备

    首先,需要申请一个域名和对应的SSL证书。腾讯云、阿里云都有的,这里就不说了

    申请下来的证书选择nginx对应的文件,解压后有类似下面两个文件,后面需要用到。

    1_www.yourdomianname.com_bundle.crt

    2_www.yourdomianname.com.key

    配置server

          server {
            listen       80;
            server_name  www.yourdomainname.com; // 你自己的域名
    		
    	return  301  https://$server_name$request_uri; // 将http请求重定向到https
          }
    	
          server {
            listen       443 ssl;
            server_name  www.yourdomianname.com; // 你自己的域名
    
    	charset 'utf-8';
    
            ssl_certificate      1_www.yourdomianname.com_bundle.crt; // 证书文件
            ssl_certificate_key  2_www.yourdomianname.com.key; // 密钥文件
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
    
            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
          }
    

    这里需要注意一个地方,证书文件的位置及配置。

    1. 要么绝对路径,即/开头。写文件的全路径。
    2. 要么相对路径,即从当前目录开始找。
    3. 不要写./../,这里不支持这样写,会报错。

    我是直接把证书扔在了nginx的conf目录,即和nginx.conf同目录。所以我写文件名即可。

    添加SSL模块

    通常,我们之前安装的nginx并没有带上ssl模块,那么在完成上述配置后,nginx启动会报错

    nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/nginx.conf

    接下来我们解决这个问题

    1 重新配置ssl模块
    先切换到源目录,注意是nginx的源码解压目录

    cd /usr/local/nginx-1.16.1
    

    执行命令

    ./configure --with-http_ssl_module
    

    如果报错 ./configure: error: SSL modules require the OpenSSL library,则执行

    yum -y install openssl openssl-devel
    ./configure
    ./configure --with-http_ssl_module
    

    2 重新编译

    make
    

    注意这里不能make install,会覆盖已有安装目录

    3 备份原来nginx

    cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
    

    4 将新编译的nginx覆盖旧安装目录

    先停止nginx

    /usr/local/nginx/sbin/nginx -s stop
    

    覆盖

    cp objs/nginx /usr/local/nginx/sbin/nginx
    

    如果报错,执行

    cp -rfp objs/nginx /usr/local/nginx/sbin/nginx
    

    5 测试新nginx是否可用

    /usr/local/nginx/sbin/nginx -t
    

    测试返回结果应该类似

    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    

    再次启动nginx即可。

    cd /usr/local/nginx/sbin
    ./nginx
    
  • 相关阅读:
    python print()输出指定小数位数的数字
    P35 线性回归两种求解方式总结
    P34 线性回归的策略、优化、案例
    P33 线性回归的定义及矩阵的运算
    P53 trainable 学习率的调整,梯度爆炸
    P52 线性回归的原理的复习及实现
    P51 可视化学习
    P50 运算API介绍
    P49 张量的定义以及数据
    P48 会话的run()方法
  • 原文地址:https://www.cnblogs.com/tenny-peng/p/12887594.html
Copyright © 2011-2022 走看看