Linux环境Nginx的Https配置
一、环境准备:
1、Linux version 3.10.0-514.26.2.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Jul 4 15:04:05 UTC 2017
2、内存:1G,CPU:1U
二、安装工具包 wget、vim、gcc
[root@VM_16_10_centos ~]# yum install -y wget
[root@VM_16_10_centos ~]# yum install -y vim-enhanced
[root@VM_16_10_centos ~]# yum install -y make cmake gcc gcc-c++
三、安装依赖包
[root@VM_16_10_centos ~]# yum install -y pcre pcre-devel
[root@VM_16_10_centos ~]# yum install -y zlib zlib-devel
[root@VM_16_10_centos ~]# yum install -y openssl openssl-devel
四、下载文件:
[root@VM_16_10_centos ~]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
五、解压文件、编译、安装
1、将nginx-1.10.2.tar.gz解压到/usr/local/目录下
2、/usr/local/nginx-1.10.2 改名为nginx-src
[root@VM_16_10_centos local]# mv nginx-1.10.2 nginx-src
[root@VM_16_10_centos local]# cd nginx-src
[root@VM_16_10_centos local]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
[root@VM_16_10_centos local]# make && make install
六、证书生成
1、创建目录:/usr/local/nginx/conf/ck
[root@VM_16_10_centos ck]# mkdir ck
[root@VM_16_10_centos ck]# cd ck
2、创建服务器证书密钥文件 server.key
###输入密码,确认密码,自己随便定义,但是要记住,后面会用到
[root@VM_16_10_centos ck]# openssl genrsa -des3 -out server.key 1024
3、创建服务器证书的申请文件 server.csr
[root@VM_16_10_centos ck]# 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 []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入
4、备份文件
[root@VM_16_10_centos ck]# cp server.key server.key.org
5、去除文件口令
[root@VM_16_10_centos ck]# openssl rsa -in server.key.org -out server.key
6、生成证书文件server.crt
[root@VM_16_10_centos ck]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
七、修改防火墙
添加443端口,重启防火墙
-A INPUT -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPT
[root@VM_16_10_centos local]# systemctl restart iptables
八、修改配置文件
配置文件路径:/usr/local/nginx/conf/nginx.conf
server {
#listen 80;
server_name localhost;
listen 443 ssl;
#charset koi8-r;
#access_log logs/host.access.log main;
#ssl on;
ssl_certificate /usr/local/nginx/conf/ck/server.crt;
ssl_certificate_key /usr/local/nginx/conf/ck/server.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:8080;
root html;
index index.html index.htm;
}
}