1、配置nginx支持https協議訪問,須要在編譯安裝nginx的時候添加相應的模塊--with-http_ssl_modulenginx
查看nginx編譯參數:/usr/local/nginx/sbin/nginx -V服務器
以下所示:tcp
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.35memcached
若是沒有--with-http_gzip_static_module這個參數,須要從新編輯nginxgoogle
2、防火牆開啓https協議默認端口443rest
vi /etc/sysconfig/iptables #編輯防火牆配置文件,添加如下代碼code
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPTserver
:wq! #保存退出ip
service iptables restart #重啓防火牆ssl
3、建立https證書
確保機器上安裝了openssl和openssl-devel
yum 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 []:[email protected] #郵箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A 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
4、修改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 #重啓nginx
rewrite ^(.*) https://www.osyunwei.com$1 permanent; #能夠把http協議重定向到https上面
使用https協議打開網址,以下圖所示: