Linux下nginx配置https协议访问
2016-06-23 18:59
711 查看
一、配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块--with-http_ssl_module查看nginx编译参数:/usr/local/nginx/sbin/nginx -V
如下所示:configure arguments: --prefix=/usr/local/nginx --with-google_perftools_module --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35如果没有--with-http_gzip_static_module这个参数,需要重新编辑nginx二、防火墙开启https协议默认端口443vi /etc/sysconfig/iptables #编辑防火墙配置文件,添加以下代码-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT:wq! #保存退出service iptables restart #重启防火墙三、创建https证书确保机器上安装了openssl和openssl-develyum install openssl openssl-devel #CentOS使用yum命令安装mkdir /usr/local/nginx/conf/ssl #创建证书存放目录cd /usr/local/nginx/conf/ssl #进入目录创建服务器私钥:openssl genrsa -des3 -out server.key 1024 #根据提示输入证书口令
创建签名请求的证书(CSR):openssl req -new -key server.key -out server.csr #输入上面设置的口令#根据提示输入相应的信息Country Name (2 letter code) [XX]:cn #国家State or Province Name (full name) []:zhejiang #省份Locality Name (eg, city) [Default City]:hangzhou #城市Organization Name (eg, company) [Default Company Ltd]:osyunwei #公司Organizational Unit Name (eg, section) []:sys #部门Common Name (eg, your name or your server's hostname) []:osyunwei #主机名称Email Address []:xxx@qq.com #邮箱Please enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:123456 #证书请求密钥,CA读取证书的时候需要输入密码An optional company name []:osyunwei #公司名称,CA读取证书的时候需要输入密码openssl rsa -in server.key -out server_nopassword.key #对key进行解密openssl x509 -req -days 365 -in server.csr -signkey server_nopassword.key -out server.crt#标记证书使用上述私钥和CSR
四、修改nginx配置文件,加载ssl证书vi /usr/local/nginx/conf/nginx.conf #编辑listen 80;listen 443;ssl on;ssl_certificate /usr/local/nginx/conf/ssl/server.crt;ssl_certificate_key /usr/local/nginx/conf/ssl/server_nopassword.key;fastcgi_param HTTPS $https if_not_empty; #有https协议时自动使用https,否则忽略这个参数。:wq! #保存退出service nginx restart #重启nginxrewrite ^(.*) https://www.osyunwei.com$1 permanent; #可以把http协议重定向到https上面使用https协议打开网址,如下图所示:
如下所示:configure arguments: --prefix=/usr/local/nginx --with-google_perftools_module --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35如果没有--with-http_gzip_static_module这个参数,需要重新编辑nginx二、防火墙开启https协议默认端口443vi /etc/sysconfig/iptables #编辑防火墙配置文件,添加以下代码-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT:wq! #保存退出service iptables restart #重启防火墙三、创建https证书确保机器上安装了openssl和openssl-develyum install openssl openssl-devel #CentOS使用yum命令安装mkdir /usr/local/nginx/conf/ssl #创建证书存放目录cd /usr/local/nginx/conf/ssl #进入目录创建服务器私钥:openssl genrsa -des3 -out server.key 1024 #根据提示输入证书口令
创建签名请求的证书(CSR):openssl req -new -key server.key -out server.csr #输入上面设置的口令#根据提示输入相应的信息Country Name (2 letter code) [XX]:cn #国家State or Province Name (full name) []:zhejiang #省份Locality Name (eg, city) [Default City]:hangzhou #城市Organization Name (eg, company) [Default Company Ltd]:osyunwei #公司Organizational Unit Name (eg, section) []:sys #部门Common Name (eg, your name or your server's hostname) []:osyunwei #主机名称Email Address []:xxx@qq.com #邮箱Please enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:123456 #证书请求密钥,CA读取证书的时候需要输入密码An optional company name []:osyunwei #公司名称,CA读取证书的时候需要输入密码openssl rsa -in server.key -out server_nopassword.key #对key进行解密openssl x509 -req -days 365 -in server.csr -signkey server_nopassword.key -out server.crt#标记证书使用上述私钥和CSR
四、修改nginx配置文件,加载ssl证书vi /usr/local/nginx/conf/nginx.conf #编辑listen 80;listen 443;ssl on;ssl_certificate /usr/local/nginx/conf/ssl/server.crt;ssl_certificate_key /usr/local/nginx/conf/ssl/server_nopassword.key;fastcgi_param HTTPS $https if_not_empty; #有https协议时自动使用https,否则忽略这个参数。:wq! #保存退出service nginx restart #重启nginxrewrite ^(.*) https://www.osyunwei.com$1 permanent; #可以把http协议重定向到https上面使用https协议打开网址,如下图所示:
相关文章推荐
- Linux下nginx配置https协议访问
- Android之判断当前网络状态
- 多网卡时如何使用源地址ping目的地址,检测网络连通性
- 通过Http接口及SolrNet 两种方法基于Solr5.5.1 实现CURD
- httplib、urllib、urllib2的区别
- tomcat 解析(四)-处理http请求过程
- BP神经网络梯度下降算法
- 详解android高仿微信后台接受消息,发送通知,监测网络连接
- Tomcat、http、get/post
- camel 配置 https访问配置
- c版http服务器 shttpd-1.38 vs2013
- iOS - HTTPS接口加密和身份认证
- 不用网线搭建IPv6网络测试环境
- springmvc restful 出现HTTP Status 406 -
- java 通过Httpclient模拟https Post 请求,实现双向认证
- Android工具类系列-HttpUtil
- 网络原理,以及对VMware Workstation虚拟网络VMnet0、VMnet1、VMnet8的图解
- HTTP协议
- centos apache 最新版HTTPS配置
- +=与=..+..的区别(转自http://www.ticmy.com/?p=285)