CentOS7 NGINX内网配置自签名HTTPS

测试环境需要使用https时,使用IP访问,无法申请正式证书,所以就需要自签名证书用于测试。

  • 首先确保机器上安装了openssl和openssl-devel

yum install openssl

yum install openssl-devel

  • 然后自己给自己颁发证书(制作秘钥,单项认证)

mkdir /usr/local/nginx/conf/cert
cd /usr/local/nginx/conf/cert
openssl genrsa -des3 -out server.key 1024 (建立服务器私钥,在这个过程中需要输入密码短语,需要记住这个密码)
openssl req -new -key server.key -out server.csr

  • 输入命令以后,需要填写如下内容:

Country Name(国家:中国填写CN)
State or Province Name(区域或是省份:CHONGQING)
Locality Name(地区局部名字:CHONGQING)
Organization Name(机构名称:填写公司名)
Organizational Unit Name(组织单位名称:部门名称)
Common Name(网站域名)
Email Address(邮箱地址)
A challenge password(输入一个密码)
An optional company name(一个可选的公司名称)

  • 输入完这些内容,就会在当前目录生成server.csr文件

cp server.key server.key.org
openssl rsa -in server.key.org -out server.key (对于使用上面的私钥启动具有SSL功能的NGINX)
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt (使用上面的密钥和CSR对证书进行签名)

  • 配置Nginx

vim /usr/local/nginx/conf/vhost/test.com.conf(自己的配置文件路径)

server
    {
        listen 443 ssl ;
        #listen [::]:80;
        server_name localhost ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/shihua;


        ssl_certificate cert/server.crt;
        ssl_certificate_key cert/server.key;


        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }


        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/access.log;
    }

Comments

No comments yet. Why don’t you start the discussion?

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注