nginx 配置https, 服務器是阿里雲的ECS(親測)


准備材料:1 已經備案的域名   ,  2, 有ecs服務器

 

開始:

  進入阿里雲搜索ssl.。 選擇購買證書。

 

 

 

 

 

 

 

 

 

 

 

 證書下載成功后上傳服務器,

  mkdir  /usr/local/nginx/ssl  ,把下載的證書解壓放進去。

 

然后就是配置,我用的是nginx  ,配置方法如下:

 1     server {
 2         listen       443;
 3         ssl              on;
 4         server_name  你的域名;
 5         ssl_certificate                          /usr/local/nginx/ssl/3337959_hunlihongbao.top.pem;
 6         ssl_certificate_key                        /usr/local/nginx/ssl/3337959_hunlihongbao.top.key;
 7         ssl_session_timeout 5m;
 8         ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
 9         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
10         ssl_prefer_server_ciphers on;
11 
12     
13         location / {
14             root   /wanfu/dist/;
15             index  index.html;
16               try_files $uri $uri/ /index.html;        
17         }
18         
19         location  ^~/api/ {
20             proxy_pass http://127.0.0.1:18080/;
21             
22             proxy_set_header  X-Real-IP  $remote_addr;
23             proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
24             proxy_set_header  Host $http_host;
25             proxy_redirect    off;
26             proxy_ssl_session_reuse off;
27         }
28         
29         location ~ .*\.(gif|jpg|jpeg|png)$ {  
30             expires 24h;  
31             root /wanfu/; #指定圖片存放路徑 
32         }
33         
34         error_page   500 502 503 504  /50x.html;
35         location = /50x.html {
36             root   html;
37         } 
38     }
39     
40     server{
41         listen       80;
42         server_name  你的域名;
43         return 301 https://$server_name$request_uri;
44     }

 

檢測 nginx :    ./usr/loca/nginx/sbin/nginx -t

檢查配置如果發現了報錯,說明你沒有安裝ssl的nginx擴展

  nginx: [emerg] unknown directive "ssl"

接下來 叫大家 安裝 插件包:

  先進入你的nginx  源碼包路徑 , 就是你當初下載 nginx.tar 解壓下來的那個文件夾 。 我的是這里:

 

 

  cd nginx-1.14.0

 

 

./configure --prefix=/usr/local/nginx --with-http_ssl_module // 加上http的ssl支持模塊
make //注意 這里千萬補要install 不然會覆蓋已有 的nginx 配置 , 我們只需要編譯一下。
cp /objs/nginx   /usr/local/nginx/sbin/nginx //然后把薪的nginx , 替換原來的
/usr/local/nginx/sbin/nginx  -V

 

  至此已經按照 ssl 模塊 , 然后就重啟 nginx  ./usr/localnginx/sbin/nginx -s reload

最后 阿里雲ecs 那里 設置 443 端口開放

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM