nginx配置https


前提准备:域名的ssl证书+nginx

ssl证书有很多免费的,腾讯云和阿里云都提供免费的证书。我的证书就是在阿里云领取的。

首先打开阿里云官网:aliyun.com

在导航栏的产品分类里搜索证书,点击SSL证书

进入云盾证书服务,选购证书免费版。

选购完成之后,域名dns解析校验或者txt文件校验都可以,几乎都是秒通过。

证书颁发后,点击右下方下载,选择nginx版,会得到一个压缩包,这个压缩包里有两个文件,一个是.pem文件,一个是.key文件,这个都是我们马上会用到的。

nginx: Nginx (engine x) 是一个高性能的HTTP反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。

其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东新浪网易腾讯淘宝等。

ubuntu server安装nginx apt install nginx -y

来看下nginx的默认配置,在/etc/nginx/nginx.conf文件中。

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#    # See sample authentication script at:
#    # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#    # auth_http localhost/auth.php;
#    # pop3_capabilities "TOP" "USER";
#    # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#    server {
#        listen     localhost:110;
#        protocol   pop3;
#        proxy      on;
#    }
# 
#    server {
#        listen     localhost:143;
#        protocol   imap;
#        proxy      on;
#    }
#}

接下来我们修改这个配置即可给网站套上https

  1. /etc/nginx目录下创建cert目录,上传刚才的证书文件到/etc/nginx/cert,即.pem文件和.key文件,可以选择winscp或者ftp之类的工具

  2. 配置nginx.conf

    include /etc/nginx/sites-enabled/*后加上下面代码

    把其中的域名全部换成你自己的即可。

    server {
              listen 443;
              server_name jaav.com.cn;
              ssl on;
              root /var/www/html;
              index index.html index.htm;
              ssl_certificate  cert/jaav.com.cn.pem;
              ssl_certificate_key cert/jaav.com.cn.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 / {
                  index index.html index.htm;
            }
       }
        server {
              listen 80;
              server_name jaav.com.cn;
              rewrite ^(.*)$ https://jaav.com.cn;   
       }

    第二个server的作用是http自动跳转到https

  3. 重启nginx

     service nginx restart

此时,ctrl+f5刷新你的网页,可以看到已经上了把小小的锁。


文章作者: 空指针异常
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 空指针异常 !
评论