nginx 配置 https

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

#yum install openssl
#yum install openssl-devel

2.创建服务器私钥,命令会让你输入一个口令:

openssl genrsa -des3 -out server.key 1024  //生成私钥

#因为以后要给nginx使用.每次reload nginx配置时候都要你验证这个PAM密码的.由于生成时候必须输入密码,你可以输入后 再删掉。
 

3.创建签名请求的证书(CSR):

openssl req -new -key server.key -out server.csr                                //生成证书颁发机构,用于颁发公钥

4.在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

cp server.key server.key.org

openssl rsa -in server.key.org -out server.key                                  //除去密码以便reload询问时不需要密码
 

5.配置nginx
最后标记证书使用上述私钥和CSR:

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

6.修改Nginx配置文件,让其包含新标记的证书和私钥:

http {
 
        include server/*.cn;
}
 

7.修改Nginx配置文件,让其包含新标记的证书和私钥:

server {
        listen       443;                                                                       //监听端口为443
        server_name  www.localhost.cn;
  
        ssl                  on;                  //开启ssl
        ssl_certificate      /etc/pki/tls/certs/server.crt;      //证书位置
        ssl_certificate_key  /etc/pki/tls/certs/server.key;      //私钥位置
        ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1;            //指定密码为openssl支持的格式
        ssl_ciphers  HIGH:!aNULL:!MD5;              //密码加密方式
        ssl_prefer_server_ciphers   on;             //依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码
  
        location / {
            root   html;                        //根目录的相对位置
            index  index.html index.htm;
        }
    }

8.启动nginx服务器.

/usr/local/nginx/sbin/nginx -s reload 

 

 

 

如果出现“[emerg] 10464#0: unknown directive "ssl" in /usr/local/nginx-0.6.32/conf/nginx.conf:74”则说明没有将ssl模块编译进nginx,在configure的时候加上“--with-http_ssl_module”即可

 

在centos中,配置nginx的https时,出现如下错误。

nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/nginx.conf:102

 

到解压的nginx目录下

./configure --with-http_ssl_module

 

当执行上面语句,出现./configure: error: SSL modules require the OpenSSL library.

用 yum -y install openssl openssl-devel

再执行./configure

 

重新执行./configure --with-http_ssl_module

make ,切记不能make install 会覆盖。

 

把原来nginx备份

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

 

把新的nginx覆盖旧的

cp objs/nginx /usr/local/nginx/sbin/nginx

出现错误时cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy

 

用cp -rfp objs/nginx /usr/local/nginx/sbin/nginx解决

测试nginx是否正确

/usr/local/nginx/sbin/nginx -t

(nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful)

重启nginx

/usr/local/nginx/sbin/nginx -s reload

 

posted @ 2018-01-25 16:17  心潮澎湃  阅读(246)  评论(0编辑  收藏  举报