在Nginx服務器上安裝SSL證書


配置nginx

1.下載證書文件

2.在nginx的conf目錄中創建目錄cert目錄,並將證書文件拷貝進去。

3.配置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 {
    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  on;

    server {
        listen       80;
        server_name  xxx.com;#替換成你的域名

        location / {
            rewrite ^(.*)$ https://xxx.com/$1 permanent;#替換成你的域名
        }
    }

	server {
		listen 443;
		server_name xxx.com;  # 替換成你的域名
		ssl on;   #設置為on啟用SSL功能。
		root html;
		index index.html index.htm;
		ssl_certificate cert/2946730_www.xxx.com.pem;   #替換成你的pem文件名稱
		ssl_certificate_key cert/2946730_www.xxx.com.key;   #替換成你的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 / {
			proxy_pass http://localhost:8080/;  #請求轉發
		}
	}

}

4.啟動nginx,然后進行訪問:

啟動時nginx:[emerg]unknown directive ssl錯誤

原因是nginx缺少SSL模塊,需要重新將SSL模塊添加進去,然后再啟動nginx:

  1. 解壓目錄(不是安裝目錄)執行命令:./configure --with-http_ssl_module
  2. 繼續執行命令:make
  3. 將objs目錄下的nginx文件復制到/usr/local/nginx/sbin/下覆蓋,然后重新啟動即可。


免責聲明!

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



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