Nginx 配置 HTTPS 服務器

一、HTTPS簡介

1.https簡介

HTTPS其實是有兩部分組成:HTTP + SSL / TLS,也就是在HTTP上又加了一層處理加密信息的模塊。服務端和客戶端的信息傳輸都會通過TLS進行加密,所以傳輸的數據都是加密後的數據

2.https協議原理

首先,客戶端與服務器建立連接,各自生成私鑰和公鑰,是不同的。服務器返給客戶端一個公鑰,然後客戶端拿着這個公鑰把要搜索的東西加密,稱之爲密文,並連並自己的公鑰一起返回給服務器,服務器拿着自己的私鑰解密密文,然後把響應到的數據用客戶端的公鑰加密,返回給客戶端,客戶端拿着自己的私鑰解密密文,把數據呈現出來

二、證書和私鑰的生成

注意:一般生成的目錄,應該放在nginx/conf/ssl目錄

1.創建服務器證書密鑰文件 server.key:

openssl genrsa -des3 -out server.key 1024

輸入密碼,確認密碼,自己隨便定義,但是要記住,後面會用到。

2.創建服務器證書的申請文件 server.csr

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

輸出內容爲:
Enter pass phrase for root.key: ← 輸入前面創建的密碼 
Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不輸入 
Common Name (eg, YOUR name) []: ← 此時不輸入 
Email Address []:[email protected] ← 電子郵箱,可隨意填
Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不輸入 
An optional company name []: ← 可以不輸入

4.備份一份服務器密鑰文件

cp server.key server.key.org

5.去除文件口令

openssl rsa -in server.key.org -out server.key

6.生成證書文件server.crt

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

三、配置文件

 

server {
    listen       8082;
    server_name  localhost;
    location / {
         rewrite ^(.*)$ https://localhost$1 permanent; 
    }
}

server {
        listen       443 ssl;
        server_name   localhost;
        root /var/www/lumen/public;    
        ssl_session_timeout 10m;
        keepalive_timeout   70;
   
        error_log /var/log/nginx/lumen_error.log;
        ssl_certificate  /home/ssl/server.crt;
        ssl_certificate_key  /home/ssl/server.key;
        ssl_session_cache shared:SSL:1m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        add_header X-Frame-Options SAMEORIGIN;
        add_header X-Content-Type-Options nosniff;
        add_header X-Xss-Protection 1;
        add_header strict-transport-security "max-age=31536000; includeSubDomains;preload" always;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
        }
    location ~ /\.ht {
            allow  all;
        }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章