Categories

Tags

nginx启用ssl模块以支持https

http://www.cnblogs.com/piscesLoveCc/p/6120875.html

启用https需要安装ssl模块

/usr/local/nginx/sbin/nginx -V

查看nginx版本与编译安装了哪些模块

如果没有看到 ‘OpenSSL’等字眼说明还没安装ssl模块,继续下面的操作

添加模块(非覆盖安装)
切换到你的nginx安装包解压目录下

cd /nginx-1.10.3

使用参数重新配置

./configure --with-http_stub_status_module --with-http_ssl_module

#--with-http_ssl_module这是ssl模块
编译

make

#不要make install,否则就是覆盖安装
替换nginx二进制文件:

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

#如果覆盖过程中提示‘cp: 无法创建普通文件”/usr/local/nginx/sbin/nginx”: 文本文件忙’ ,说明nginx在运行中,先暂停nginx服务

至此,ssl模块已成功添加

使用阿里云的话,可以在 安全->证书服务 里面申请证书
把nginx.conf的server节点配置一下再重启就可以了
https默认使用443端口

server {
    listen 443;
    server_name localhost;
    ssl on;
    root html;
    index index.html index.htm;
    ssl_certificate   cert/214186130880445.pem;
    ssl_certificate_key  cert/214186130880445.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        root html;
        index index.html index.htm;
    }
}

强制使用https访问(自动重定向到https)

server {  
    listen 80;  
    server_name www.mytest.cn;
    rewrite ^(.*)$  https://$host$1 permanent;
}

#这样在浏览器输入http://www.mytest.cn/art/index会自动跳转到https://www.mytest.cn/art/index