liunx下nginx靜態服務器配置SSL證書

一.準備crt文件和key文件(如下爲測試文件)

一、首先生成一個keyhtml

openssl genrsa -des3 -out ssl.key 2048

二、以後會要求你輸入這個key對應的密碼,不推薦輸入。由於之後要給nginx使用。每次reload nginx配置時候都要你驗證這個PAM密碼的。
 因爲生成時候必須輸入密碼。你能夠輸入後 再刪掉。nginx

mv ssl.key xxx.key
openssl rsa
-in xxx.key -out ssl.key rm xxx.key

三、 而後根據這個key文件生成證書請求文件api

openssl req -new -key ssl.key -out ssl.csr

四、以後須要鍵入的信息能夠任意
五、最後根據這2個文件生成crt證書文件tomcat

openssl x509 -req -days 3650  -in ssl.csr -signkey ssl.key -out ssl.crt

五、存放文件到 /usr/loca/nginx/ssl/服務器

二.配置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;

    upstream  tomcat8080_api {  
        server 127.0.0.1:8080  weight=1;
    }
    
    server {
        listen       80;                      #http默認訪問端口
        server_name  test.com;                  
        #http轉https(前提是已經配置nginx ssl證書)
        rewrite ^(.*)$  https://$host$1 permanent;       #重定向到https
    }
    
    # HTTPS 
    server {  
        listen 443;                          #HTTPS默認訪問端口
        server_name huituanquan.com;                         #網站域名
      
        ssl on;                             #nginx1.5之後不須要配置這一項
        ssl_certificate /usr/local/nginx/ssl/ssl.crt;         #(證書公鑰)
        ssl_certificate_key /usr/local/nginx/ssl/ssl.key;     #(證書私鑰)
     ssl_session_timeout 5m; 
     #ssl_protocols SSLv2 SSLv3 TLSv1; 
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     #ssl_ciphers HIGH:!aNULL:!MD5; 
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
      ssl_prefer_server_ciphers on;


        location / {
            proxy_pass http://tomcat8080_api;  
            proxy_redirect default;      
            #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        } 
    }

}

注意:session

nignx安裝模塊查看:app

/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

配置SSL證書必須有上面紅色的模塊!!!!!!tcp

相關文章
相關標籤/搜索