本文記錄從生成自簽名 SSL 證書到在 Laravel 項目中應用的全流程配置。
生成自簽名證書文件#
首先,使用openssl
命令生成自簽名證書和私鑰。以下是具體步驟:
# 生成帶有密碼保護的私鑰
openssl genrsa -des3 -out server.key 4096
# 基於私鑰生成證書簽名請求文件
openssl req -new -key server.key -out server.csr
# 備份原始私鑰文件
cp server.key server.key.org
# 移除密碼保護以便Nginx能夠使用
openssl rsa -in server.key.org -out server.key
# 使用私鑰簽名生成自簽名證書,有效期為10年(3650天)
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
修改中間件的配置文件應用我們生成的證書文件#
在配置文件中,使用如下配置以應用生成的證書文件。
server {
listen 80;
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/local/openresty/nginx/conf/cert/server.crt;
ssl_certificate_key /usr/local/openresty/nginx/conf/cert/server.key;
ssl_session_cache shared:SSL:5m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
access_log /data/wwwlogs/access_nginx.log combined;
root /data/wwwroot/risk.sdzc.test/public;
index index.html index.htm index.php;
#error_page 404 /404.html;
#error_page 502 /502.html;
#location /nginx_status {
# stub_status on;
# access_log off;
# allow 127.0.0.1;
# deny all;
#}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
deny all;
}
location /.well-known {
allow all;
}
}
驗證並重啟您的中間件 (nginx)#
nginx -t
nginx -s reload