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;
        }
}

 

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