SSL证书配置

一. 环境准备

需要apache服务至少加载 如下ssl相关模块,可以的话,推荐开启所有模块
# vim /etc/httpd/conf/httpd.conf Include conf/extra/ssl.conf
LoadModule ssl_module modules/mod_ssl.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so 需要nginx服务支持编译支持ssl模块

证书签发说明:

1)一般,自签证书均在同一台服务器执行进行签署,按照如下操作即可
2)如果,CA证书服务器和用户请求的服务器是不同机器,CA服务器只负责生成CA根证书和签发,用户服务器负责进行申请,CA服务器将用户服务器发送给自己的httpd.csr 证书请求文件进行签发,并生成httpd.crt证书文件,然后发放给用户服务器端,进行配置即可,用户服务器目的是要获取httpd.crt证书文件。

二. 部署Apache跳转

1.  部署SSL(自签证书)

  1.1 签发端-生成CA自签根证书

    A. 生成CA证书私钥

cd /etc/pki/CA  &&  (umask 077; openssl genrsa -out private/cakey.pem 2048)

    B. 生成自签根证书

    修改openssl.cnf文件,生成过程中减少二次输入相关内容

[root@localhost CA]# vim /etc/pki/tls/openssl.cnf

130 countryName_default             = CN//修改为CN
135 stateOrProvinceName_default = Beijing//省份名称
138 localityName_default = Beijing//城市名称
141 0.organizationName_default = zxl//组织名称
148 organizationalUnitName_default = Tech//部门名称 [root@localhost CA]# pwd
/etc/pki/CA
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
[root@localhost pki]# vim ../tls/openssl.cnf
42 dir             = /etc/pki/CA   //修改CA证书路径

    如果certs crl newcerts目录不存在手动创建目录即可  

[root@localhost CA]# touch index.txt
[root@localhost CA]# echo 01 > serial
[root@localhost CA]# ls
cacert.pem certs crl index.txt newcerts private serial

  1.2 申请端-申请签发证书

    A. 安装apache服务

创建存放证书的目录  /app/httpd2.4.25/conf/ssl

    B. 生成证书私钥

[root@localhost ssl]# pwd
/app/httpd2.4.25/conf/ssl
[root@localhost ssl]# (umask 077; openssl genrsa 2048 > httpd.key)

    C. 生成证书签署请求文件

//生成证书请求,由于是自签证书,所以以下信息必须和CA服务器根证书签发一致
[root@localhost ssl]# openssl req -new -key httpd.key -out httpd.csr

    D. CA证书服务器用刚签署的请求文件进行签发证书

//签发证书十年3650天,信息内容,回车,会输入两个y
[root@localhost ssl]# openssl ca -in httpd.csr -out httpd.crt -days 3650
[root@localhost CA]# ls /etc/pki/CA/
cacert.pem certs crl index.txt index.txt.attr
index.txt.old newcerts private serial serial.old
[root@localhost CA]# cat /etc/pki/CA/index.txt
V     210418070116Z         01    unknown    /C=CN/ST=BeiJing/O=pxjy/OU=IT\xC3\xA4\xC2\xBA\xC2\xA7\xC3\xA5\xC2\x93\xC2\x81\xC3\xA7\xC2\xA0\xC2\x94\xC3\xA5\xC2\x8F\xC2\x91\xC3\xA9\xC2\x83\xC2\xA8/CN=www.pdjy.com

    E. 申请的私钥和证书齐全如下

httpd.key   httpd.crt

  1.3 配置SSL证书访问

[root@localhost conf]# vim httpd-vhosts.conf
……自签证书,配置两行证书文件即可实现和购买的证书有点区别
SSLEngine on
SSLCertificateFile conf/ssl/httpd.crt
SSLCertificateKeyFile conf/ssl/httpd.key

2.  部署SSL(购买证书)

  2.1  配置全局站点

# For ssl证书配置-针对泛域名
<VirtualHost *:80>
ServerAlias *.pxjy.com
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
   RewriteRule ^(.*)?$ https://%{SERVER_NAME}$1
</VirtualHost>

  2.2 方法二

# For ssl证书配置-针对单域名
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/tz.php
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

  解释:

%{SERVER_PORT} —— 访问端口
%{REQUEST_URI} —— 比如如果url是 http://localhost/tz.php,则是指 /tz.php。
%{SERVER_NAME} —— 比如如果url是 http://localhost/tz.php,则是指 localhost。
以上规则的意思是,如果访问的url的端口不是443,且访问页面不是tz.php,则应用RewriteRule这条规则。
这样便实现了:访问了 http://localhost/index.php 或者 http://localhost/admin/index.php 等页面的时候会自动跳转到 https://localhost/index.php
或者 https://localhost/admin/index.php,但是访问 http://localhost/tz.php 的时候就不会做任何跳转,也就是说 http://localhost/tz.php
和 https://localhost/tz.php 两个地址都可以访问

  2.3 方法三

RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.firx.cn/$1 [R,L]
#这样跳转的好处是独立IP主机也支持,访问ip能自动跳转到https

  2.4 方法四 

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]
#整站跳转

  2.5 方法五

RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#以上至针对某个目录跳转, yourfolder就是目录名

  2.6 方法六

redirect 301  /你的网页  https://你的主机+网页
#至针对某个网页跳转

  2.7 方法七

RewriteCond  %{http_host}  ^firx.cn [nc]
RewriteRule ^(.*)?$ https://www.firx.cn$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://www.firx.cn/$1 [R=301,L]

  2.8 方法八-ssl跳转出现跨域的问题

# For ssl证书配置-针对单域名

ServerName   proxy.mimvp.com
Redirect permanent / https://xxx.xxx.com/
<Directory "/app/website/jituanguanwang_web">
……
Header set Access-Control-Allow-Origin https://xxx.xxx.com
……
</Directory> 附:
如上,标红处配置,是解决http协议跳转https协议时,出现跨域的问题;
后面的域名,可以用 * ,或者指定域名均可;
有跨域的问题就可以加上此配置,若无此问题,也可以不加

  2.9 apache案例线上SSL配置参考

……
SSLEngine on
SSLCertificateFile conf/ssl/xxx.com.cer
SSLCertificateKeyFile conf/ssl/xxx.com.key
SSLCertificateChainFile conf/ssl/xxx.com.crt
……

三. 部署Nginx跳转 

1.  自签名证书Nginx

  1.1 生成私钥

openssl genrsa -des3 -out ssl.key 1024

  1.2 创建证书签名请求

openssl req -new -key ssl.key -out ssl.csr

  1.3 清除SSL启动nginx时提示必须输入密钥

cp ssl.key ssl.key.org
openssl rsa -in ssl.key.org -out ssl.key

  1.4 使用刚生成的私钥和CSR进行证书签名

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

  1.5 把私钥和证书加入到nginx.conf的配置文件中

ssl_certificate      /etc/nginx/ssl/ssl.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;

2.  配置线上Nginx证书

注意:

A. 准备好SSL证书或采购或自签生成
B. Nginx需要支持ssl模块

   2.1 方法一(推荐)

#cat  /usr/local/nginx1.10.3/conf/conf.d/project.conf
server {
listen 80;
listen 443 ssl;
server_name pxsnx.pxjy.com; ### 配置网站域名
root /app/webroot/project;
access_log /app/applog/project.log main;
error_log /app/applog/project.error.log error;
ssl_certificate ssl/pxsnx.pxjy.com.crt; ### 路径需要进行创建,证书文件
ssl_certificate_key ssl/pxsnx.pxjy.com.key; ### 证书秘钥文件 # 协议优化(可选,优化https协议,增强安全性)
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on; # 自动跳转到https //注释此跳转,则实现的是兼容80和443端口访问
if ($server_port = 80) {
rewrite ^(.*)$ https://$host$1 permanent;
} location / {
index index.php;
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?s=$1;
}
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
} location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param PHP_VALUE "open_basedir=/app/webroot/project:/tmp/";
include fastcgi_params;
}
}

   2.2 方法二

#cat  /usr/local/nginx1.10.3/conf/conf.d/project.conf
server {
listen 80;
server_name pxsnxg.pxjy.com; ### 配置网站域名
rewrite ^(.*)$ https://$host$1 permanent; ### 配置永久重定向
}
#附: 如果想用泛域名进行配置的话,可以配置 “server_name *.pxjy.com;” ,单独命名一个全局的配置文件,便于管理 server {
listen 443 ssl;
server_name pxsnx.pxjy.com; ### 配置网站域名
root /app/webroot/project;
access_log /app/applog/project.log main;
error_log /app/applog/project.error.log error;
ssl_certificate ssl/pxsnx.pxjy.com.crt; ### 路径需要进行创建,证书文件
ssl_certificate_key ssl/pxsnx.pxjy.com.key; ### 证书秘钥文件 # 协议优化(可选,优化https协议,增强安全性)
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on; location / {
index index.php;
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?s=$1;
}
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param PHP_VALUE "open_basedir=/app/webroot/project:/tmp/";
include fastcgi_params;
}
}

  

lnmp之Nginx配置https加密访问的更多相关文章

  1. 【转】Linux下nginx配置https协议访问的方法

    一.配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块--with-http_ssl_module 查看nginx编译参数:/usr/local/nginx/sbin/ ...

  2. Linux下nginx配置https协议访问

    一.配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块--with-http_ssl_module 查看nginx编译参数:/usr/local/nginx/sbin/ ...

  3. Linux下nginx配置https协议访问的方法

    一.配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块--with-http_ssl_module 查看nginx编译参数:/usr/local/nginx/sbin/ ...

  4. Nginx采用https加密访问后出现的问题

    线上的一个网站运行了一段时间,应领导要求,将其访问方式更改为https加密方式.更改为https后,网站访问正常,但网站注册功能不能正常使用了! 经过排查,是nginx配置里结合php部分漏洞了一个参 ...

  5. CentOS 7 配置HTTPS加密访问SVN

    上一篇文章已经介绍了如何在CentOS7环境下安装SVN并整合HTTP访问 http://www.cnblogs.com/fjping0606/p/7581093.html 那么本文则介绍如何添加HT ...

  6. Linux服务之nginx服务篇四(配置https协议访问)

    一.配置nginx支持https协议访问 编译安装nginx的时候需要添加相应的模块--with-http_ssl_module和--with-http_gzip_static_module(可通过/ ...

  7. nginx配置ssl加密(单双向认证、部分https)

    nginx配置ssl加密(单双向认证.部分https) nginx下配置ssl本来是很简单的,无论是去认证中心买SSL安全证书还是自签署证书,但最近公司OA的一个需求,得以有个机会实际折腾一番.一开始 ...

  8. [转帖]nginx配置ssl加密(单/双向认证、部分https)

    nginx配置ssl加密(单/双向认证.部分https) https://segmentfault.com/a/1190000002866627   nginx下配置ssl本来是很简单的,无论是去认证 ...

  9. nginx配置https及Android客户端访问自签名证书

    前一篇随笔通过keytool生成keystore并为tomcat配置https,这篇随笔记录如何给nginx配置https.如果nginx已配置https,则tomcat就不需要再配置https了.通 ...

随机推荐

  1. debian-pve-ceph

    从头开始安装debian9.8 debian-9.8.0-amd64-netinst.iso vi /etc/vim/vimrc.tinyset nocompatibleset backspace=2 ...

  2. day01代码

    1. 使用while循环打印1,2,3,4,5,7,8,9 # 使用while循环打印1,2,3,4,5,7,8,9 count = 0 while count < 10: count += 1 ...

  3. Linux基础入门-文件系统操作与磁盘管理

    一.简单文件系统操作: df (-h) 查看磁盘容量: rootfs作为系统启动时内核载入内存之后,在挂载真正的磁盘之前的一个临时文件系统: /dev/sda2 对应主机硬盘的分区,后面的a表示第几块 ...

  4. 浏览器打开aspx文件 ,提示:XML 解析错误:找不到根元素

    在使用VS2013这个IDE创建aspx文件后,在浏览器打开后居然发现了以下错误:XML 解析错误:找不到根元素         详细查看里面的信息,发现了这么一个链接位置:http://localh ...

  5. C#遍历枚举中所有值

    public enum EnumColor { 红色=1, 黑色=2, 白色=3 } foreach (EnumColor item in Enum.GetValues(typeof(EnumColo ...

  6. 7.4 electirc.c -- 计算电费

    // 7.4 electirc.c -- 计算电费 #include <stdio.h> #define RATE1 0.13230 // 首次使用 360 kwh 的费率 #define ...

  7. MFC 使用Skin++ 美化皮肤

    查了好几天关于MFC应用程序换肤的资料,经过各种莫名其妙的问题的困扰,现分享一下自己的体会.希望可 以避免一些弯路.另外会在附上一些资源. 环境:Windows 7 + VS2012 + SkinSh ...

  8. 微信小程序企业给零钱打款 提示未配置api发起,请查看产品中心企业付款配置

    商户平台:

  9. 让WebStrom支持SSH协议的子项目

    让WebStrom支持SSH协议的子项目 在大项目中, 经常会遇到子项目(submodule)使用ssh的情形, 但是WebStrom不直接支持它. 下面以MAC为例,在PC中的处理类似. 打开ter ...

  10. Python 编码的一些问题

    >>> ord('中') 20013 >>> chr(65) 'A' >>> chr(20013) '中' - Python文件默认是UTF-8编 ...