Nginx 配置Https 协议

1. 去阿里云SSL证书购买证书

 2.  审核通过后下载nginx 的ssl证书

3. 解压出来两个文件 xxx.pem, xxx.key 

4. 在服务器 nginx安装目录(一般/usr/local/nginx/conf)下创建目录cert,并将两个文件上传至此目录下

5.修改Nginx安装目录/conf/nginx.conf文件

# HTTPS server
  server {
  listen 443;
  server_name localhost; ssl on; ssl_certificate cert.pem; ssl_certificate_key cert.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; location / { 

按照下文中注释内容修改nginx.conf文件:



# 以下属性中以ssl开头的属性代表与证书配置有关,其他属性请根据自己的需要进行配置。
server {
listen 443 ssl;   #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
server_name localhost;  #将localhost修改为您证书绑定的域名,例如:www.example.com。
root html;
index index.html index.htm;
ssl_certificate cert/domain name.pem;   #将domain name.pem替换成您证书的文件名。
ssl_certificate_key cert/domain name.key;   #将domain name.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;   
}
}                     

6.保存nginx.conf文件

7.重启nginx  

   $ systemctl stop nginx.service

 $ systemctl start nginx.service

8.开启nginx SSL模块

Nginx如果未开启SSL模块,配置Https时提示如下错误:

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf

解释:nginx缺少http_ssl_module模块,需要在已安装的nginx中添加ssl模块。

说明:我的nginx安装目录为:/usr/local/nginx , 源码包在/usr/src/nginx-1.5.9目录

Nginx开启SSL模块:

切换到源码包:

cd /usr/src/nginx-1.5.9

查看nginx原有的模块

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

 在configure arguments:后面显示的原有的configure参数如下:

--prefix=/usr/local/nginx --with-http_stub_status_module

 9. 从新配置SSL模块

进入nginx源码包目录,运行:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

配置完成后,运行命令make命令:

make

注意:此处不能进行make install,否则就是覆盖安装

10:替换已安装好的nginx包
替换之前先备份:

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

先停止nginx服务:

复制代码
//查询nginx主进程号
$ ps -ef | grep nginx

//从容停止Nginx:
$kill -QUIT 主进程号

//快速停止Nginx:
kill -TERM 主进程号

//强制停止Nginx:
pkill -9 nginx
复制代码

将刚刚编译好的nginx覆盖掉原有的nginx

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

然后启动nginx,仍可以通过命令查看是否已经加入成功

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

此时应该显示为即配置成功:

configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

补充:

nginx配置ssl,支持https的配置:

复制代码
server {
        listen       443 ssl;
        server_name  aa.abc.com;

        ssl_certificate      cert/2643408_xxx.pem;
        ssl_certificate_key  cert/2643408_xxx.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            proxy_pass http://127.0.0.1:8886;
        }
    }

nginx http 跳转 https:

复制代码
server {
        listen       80;
        server_name  aa.abc.com;
      rewrite ^(.*)$ https://$host$1 permanent;# 把http的域名请求转成https
        
    }

11. 在阿里控制台开启443安全组

安全组-》配置规则-》添加安全组规则

12. 开启防火墙443端口,开启https

// 开启http

firewall-cmd --permanent --zone=public --add-service=http  

// 开启https

firewall-cmd --permanent --zone=public --add-service=https

// 开启80 和 443 端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --zone=public --add-port=443/tcp --permanent

// 重启防护墙

firewall-cmd --reload

猜你喜欢

转载自www.cnblogs.com/zhukaijie/p/12349161.html
今日推荐