nginx支持https访问
引子
为了提高web应用的安全性,现在基本上都需要支持https访问。在此记录一下自己在nginx下的配置过程。
准备安装包
将nginx-1.12.1.tar.gz、openssl-1.0.0e.tar.gz拷贝到 /root/ 下(如果文件已经存在,则不需要上传)。
安装NGINX+ssl模块
进入到root目录下:cd/root/nginx-1.12.1
配置NGINX参数 :./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/root/openssl-1.0.0
make (耐心等待)
make install
补充:若已安装nginx但没有安装ssl模块,按一下步骤安装
进入安装nginx/sbin,使用命令:./nginx -V 查看已配置的参数,例如
configure arguments: --prefix=/usr/local/nginx --add-module=/root/soft/ngx_devel_kit-0.3.0 --add-module=/root/soft/lua-nginx-module-0.10.9rc7
记下这里的配置参数,在下面的配置中需要用到。同时备份nginx mv nginx nginx.bak
进入到nginx源码目录下,配置nginx,需要把步骤1中的配置参数加上(若不加会影响原有的功能)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/root/openssl-1.0.0 --add-module=/root/soft/ngx_devel_kit-0.3.0 --add-module=/root/soft/lua-nginx-module-0.10.9rc7
make
进入到objs下:cd objs,拷贝nginx到安装目录下
cp nginx /usr/local/nginx/sbin
启动nginx,查看nginx模块,发现已经添加
/usr/local/nginx/sbin/nginx -V
至此,nginx支持ssl模块安装完毕!
安装证书
nginx支持https协议需要服务器证书,此证书使用openssl命令生成(确保openssl命令可用)
证书生成步骤如下:
1.进入到/usr/local/nginx/conf/下,新建目录crt(mkdir crt)
2.进入到crt(cd crt)
3.开始生成证书,使用命令:openssl genrsa -des3 -out server.key 1024 生成key,会出现以下提示
Generating RSA private key, 1024 bit long modulus
......................................................++++++
.................++++++
e is 65537 (0x10001)
Enter pass phrase for server.key:(此处随意输入证书密码开心就行,比如123456)
Verifying - Enter pass phrase for server.key:(重复输入一次)
4.使用命令openssl req -new -key server.key -out server.csr 生成csr,(注:此步骤生成证书,需要输入国家/地区/公司/个人相关信息,不需要真实,内容差不多就行,可参考下面的加粗部分)
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:CN
State or Province Name (full name) [Berkshire]:Shandong
Locality Name (eg, city) [Newbury]:liangshang
Organization Name (eg, company) [My Company Ltd]:hahah
Organizational Unit Name (eg, section) []:biubiu
Common Name (eg, your name or your server's hostname) []:nanxiaoliu
Email Address []:nanxiaoliu@channelsoft.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []: (敲回车)
5.cp server.keyserver.key.org
6.openssl rsa -inserver.key.org-out server.key
Enter pass phrase forserver.key.org:123456
writing RSA key
7.openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
到此,证书创建完毕。
修改nginx配置https
进入到/usr/local/nginx/conf
vim nginx.conf 新增server节点,配置如下:
server {
server_name localhost;
listen 443 ssl;
#ssl on;
ssl_certificate /usr/local/nginx/conf/crt/server.crt;
ssl_certificate_key /usr/local/nginx/conf/crt/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#其它的一些配置
}
重启nginx验证:/usr/local/nginx/sbin/nginx -s reload
打开浏览器验证:https://10.130.29.7/index.html
在验证原有的http是否支持:http://10.130.29.7/index.html
若都能访问,配置完收工。