使用nginx将阿里云服务器升级为https

最近对自己的博客网站进行HTTPS化,打造一个安全的博客,下面是具体步骤:

  1. 下载nginx安装包
wget -i -c http://nginx.org/download/nginx-1.17.5.tar.gz
  1. 安装依赖包,新版本nginx依赖zlib-devel、pcre-devel,这里我们不妨把其他的也一并安装
在这里插入代码片
  1. 把下载的文件解压
tar -zxvf nginx-1.17.5.tar.gz

  1. 安装nginx:执行以下命令
//进入nginx
cd nginx-1.17.5
//执行命令
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
//执行make命令
make
//执行make install命令
make install
  1. 安装完毕之后, 我们就可以启动nginx
/usr/local/nginx/sbin/nginx

  1. 然后访问这个地址localhost:80,就可以看到nginx的简单界面了,nginx默认端口为:80
以下是nginx常用命令,首先进入到命令目录:
cd /usr/local/nginx/sbin

然后想做什么操作,就执行什么命令吧
# 启动
./nginx
# 关闭
./nginx -s stop
# 重启
./nginx -s reload
  1. HTTPS具体配置
    首先需要申请SSL安全证书,这里使用的阿里云DigiCert证书签发服务,DigiCert在2017年12月1日并购了Symantec,大家不用担心证书安全问题,这里选择免费型DV SSL为例子演示,然后根据阿里云提示操作一步步申请即可:
    搜索SSL证书

    1
    这里我们选择右边的免费版
    2
    点击证书申请
    3
    申请审核的时间很快的,两分钟成功后如下
    4
    下载证书,这里使用的是nginx ,所以
    5
  2. 证书申请下来以后,下载nginx版本的证书,解压文件以后,里面有两个文件:
www.toptech.top.pem #证书文件
www.toptech.top.key #证书的密钥文件
  1. 把这两个文件上传到nginx服务器,例如:我把文件放在了/usr/local/nginx/cert下
  2. 现在配置nginx的https,编辑nginx.conf文件:
vim /usr/local/nginx/conf/nginx.conf

内容如下

#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
        # 服务器主机配置:server ip:port weight=权重;
        upstream toptech{
         #中间写自己的ip
          server 120.76.62.79:8080 weight=1;
         }
    # 设置nginx允许上传文件的大小
    client_max_body_size 10m;
    include       mime.types;
    default_type  application/octet-stream;
 
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
 
    #access_log  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    # 开启gzip压缩
    gzip  on;
 
    server {
        # 监听80端口
        listen       80;
        # 配置服务名称
        server_name  www.toptech.top;
        # 监听到http的80端口请求,进行https跳转
        return 301 https://$server_name$request_uri;
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        # location / {
        #  proxy_pass http://toptech;
        # }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    # 这里就是HTTPS的配置了
    server {
        # 监听443端口
        listen       443 ssl;
        # 服务名称
        server_name  www.toptech.top;
        # 这里配置SSL证书,自己放的地址
        ssl_certificate      /usr/local/nginx/cert/www.toptech.top.pem;
        # 这里配置SSL证书的密钥
        ssl_certificate_key  /usr/local/nginx/cert/www.toptech.top.key;
 
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
 
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
 
        location / {
            # 这里是监听到443端口的请求的跳转链接
            proxy_pass http://toptech.top:8080;
        }
    }
 
}
  1. 配置完毕重启nginx
./nginx -s reload
以上端口为8080,是因为我的项目运行端口为8080,这个看情况而定,
如果网站显示还是不安全,说明网页内存在不是https的图片
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值