nginx代理设置自签ssl证书并进行https方式访问,浏览器中会报不安全的告警,记录一下处理过程
本文内容摘自CSDN博主「Dylanu」的原创文章 解决https网站通过nginx+openssl自签名证书访问,在谷歌浏览器报不安全告警的问题
使用指定-subj “/C=CN/ST=MyProvince/L=MyCity/O=MyOrganization”,生成根证书私钥和根证书
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -subj "/C=CN/ST=MyProvince/L=MyCity/O=MyOrganization" -keyout CA-private.key -out CA-certificate.crt -reqexts v3_req -extensions v3_ca
###示例:生成100年的
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -subj "/C=CN/ST=guangdong/L=guangzhou/O=gzsendi" -keyout CA-private.key -out CA-certificate.crt -reqexts v3_req -extensions v3_ca
openssl genrsa -out private.key 2048
###示例:
openssl genrsa -out private.key 2048
####命令:
openssl req -new -key private.key -subj "/C=CN/ST=MyProvince/L=MyCity/O=MyOrganization/CN=192.168.56.101" -sha256 -out private.csr
###示例:
openssl req -new -key private.key -subj "/C=CN/ST=guangdong/L=guangzhou/O=gzsendi/CN=192.168.56.101" -sha256 -out private.csr
定义自签名证书扩展文件(解决chrome安全告警)。在默认情况下生成的证书一旦选择信任,在 Edge, Firefox 等浏览器都显示为安全,但是Chrome仍然会标记为不安全并警告拦截,这是因为 Chrome 需要证书支持扩展 Subject Alternative Name, 因此生成时需要特别指定 SAN 扩展并添加相关参数,将下述内容放到一个文件中,命名为private.ext
cat > private.ext << EOF
[ req ]
default_bits = 1024
distinguished_name = req_distinguished_name
req_extensions = san
extensions = san
[ req_distinguished_name ]
countryName = CN
stateOrProvinceName = Definesys
localityName = Definesys
organizationName = Definesys
[SAN]
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = IP:192.168.56.101
EOF
根据根证书私钥及根证书-CA CA-certificate.crt -CAkey CA-private.key、自签名证书申请文件 -in private.csr、自签名证书扩展文件 -extfile private.ext,生成自签名证书 -out private.crt
##命令,设置100年的证书有效期
openssl x509 -req -days 36500 -in private.csr -CA CA-certificate.crt -CAkey CA-private.key -CAcreateserial -sha256 -out private.crt -extfile private.ext -extensions SAN
将上面生成的private.crt和private.key放在nginx下的ssl目录(ssl可以自己建立,然后放置对应的文件)。
示例如:
server {
listen 8443 ssl;
server_name localhost;
ssl_certificate /home/testuser/nginx/ssl/private.crt;
ssl_certificate_key /home/testuser/nginx/ssl/private.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_connect_timeout 30;
proxy_http_version 1.1;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_set_header Host $host:$server_port;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}