nginx添加ssl證書升級爲https(免費證書)

琢磨着想弄一個小程序玩玩,因此根據小程序接口要求,必需要有https,遂加ssl之
免費證書本身能夠百度,我選擇了certbot,阿里雲也有免費證書,這個隨便選擇,重要的是實現過程。php

一、證書獲取

clipboard.png

根據網站上面提示步驟一一實現,不過我在第五步選擇了下面的方式,由於我nginx是本身源碼安裝的css

clipboard.png

注意一下生成的證書位置html

clipboard.png

二、nginx修改

clipboard.png

server {
    listen 443 ssl;
    server_name www.***.com;
    root /www/****/****;
    index index.html index.htm index.php;
    ssl_certificate /etc/letsencrypt/live/www.***.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.***.com/privkey.pem;
    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 ~* \.(png|jpg|jpeg|gif|css|js|html)$ {
    try_files $uri $uri/ =404;
  }
 location / {
        try_files $uri $uri/ /index.php$is_args$query_string;
    }

  location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
  }
}
server {
    listen 80;
    server_name www.***.com;
    rewrite ^(.*)$ https://$host$1 permanent;
}
三、重啓nginx測試網站,很nice

clipboard.png

四、坑點
tip nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid

nginx啓動沒有指定配置文件,啓動時添加參數便可nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

tip [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
寫法問題,網上找到的不少ssl寫法都是:shell

listen 443;
ssl on;

正確寫法:小程序

listen 443 ssl;
tip nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhosts/testssl.conf:4
nginx缺乏ssl模塊

解決方法:
① 到源碼目錄添加ssl模塊 ./configure --with-http_ssl_module
若是須要openssl則安裝yum -y install openssl openssl-devel,安裝完畢再./configure
② make編譯
③ 以前的文件備份:mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.pre
④ 新文件引入:cp objs/nginx /usr/local/nginx/sbin/nginx
⑤ 查看是否安裝./sbin/nginx -V
v是大寫
⑥ 重啓nginx查看centos

相關文章
相關標籤/搜索