HTTPS

网站实现https访问

第一个历程:检查网站环境是否满足

nginx程序必须有支持证书功能的ssl模块(有下面这个模块就可以支持HTTPS)

--with-http_ssl_module

检查:

[root@web01 ~]# nginx -V

nginx version: nginx/1.16.0

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)

built with OpenSSL 1.0.2k-fips 26 Jan 2017

TLS SNI support enabled

configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --coor-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --loclient-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --ust --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_modutp_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-httlink_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module--with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_prerepe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-swiwith-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

第二个历程:创建存放ssl证书的路径

[root@web01 ~]# mkdir -p /etc/nginx/ssl_key

[root@web01 ~]# cd /etc/nginx/ssl_key/

第三个历程:使用openssl命令充当CA权威承认的黑户证书

ps:生产不可能使用的此方法的生成证书,不被互联网CA承认的黑户证书

(执行下面这条命令生成私钥)
[root@web01 ~]#openssl   genrsa -idea  -out server.key 2048
                                               (生成私钥) 2048代表设置密码的长度
下面是这条命令的单独解释:
创建私钥的命令    代表创建一个私钥     指定私钥加密算法    把生成的信息指定一个路径     存放的私钥的文件```

[root@web01 /etc/nginx/ssl_key]# openssl genrsa -idea -out server.key 2048

Generating RSA private key, 2048 bit long modulus

................+++

............................+++

e is 65537 (0x10001)

Enter pass phrase for server.key: (给这个证书设置个密码)

Verifying - Enter pass phrase for server.key: (在输入一遍)

然后查看当前目录,已经生成

[root@web01 /etc/nginx/ssl_key]# ll

total 4

-rw-r--r-- 1 root root 1747 Jun 19 15:53 server.key

第四个历程:生成自签证书,同时去掉私钥的密码

[root@web01 ~]#openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

参数信息

req:创建证书

-days:证书的有效期

-x509:定义证书的格式信息

-sha256 :公钥证书的加密算法

-nodes -newkey:去掉私钥文件的密码信息

-keyout:加载私钥文件

-out :输出生成证书的文件(假的)

server.crt:识别一个私钥,把一个私钥生成证书的信息指定到一个文件里

执行过程的详解

[root@web01 /etc/nginx/ssl_key]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

Generating a 2048 bit RSA private key

..............................................................................................................+++

...............................+++

writing new private key to '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) [XX]: cn (在哪个国家使用)

State or Province Name (full name) []:bj (省份)

Locality Name (eg, city) [Default City]:bj (城市)

Organization Name (eg, company) [Default Company Ltd]:oldboy (公司)

Organizational Unit Name (eg, section) []:it (使用这个证书的部门)

Common Name (eg, your name or your server's hostname) []:oldboy (给哪个主机用)

Email Address []:333@qq.com (邮箱地址)

生成私钥与证书,并检查:

[root@web01 /etc/nginx/ssl_key]# ll
total 8
-rw-r--r-- 1 root root 1350 Jun 19 16:16 server.crt
-rw-r--r-- 1 root root 1708 Jun 19 16:16 server.key

第五个历程:证书申请完成后需要让nginx服务进行加载

下面是参数

1、是否开启证书功能

Syntax:ssl on|off; 是否开启证书

Default: ssl off;

Context : http ,server

2、加载ssl crt证书文件存放路径

Syntax:ssl_certifacate file;

Default: -

Context : http ,server

3、加载ssl key私钥文件存放路径

Syntax:ssl_certifacate_key file;

Default: -

Context : http ,server

然后 在server下面添加三行,修改监听端口,改成443,在浏览器输入[https://www.oldboy.com/](https://www.oldboy.com/)即可

ssl on;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;

这是我在web01测试的文件,

[root@web01 /etc/nginx/conf.d]# vim www.conf
server {
      listen 443;
      server_name www.oldboy.com;
      ssl on;
      ssl_certificate ssl_key/server.crt;
      ssl_certificate_key ssl_key/server.key;
      access_log /var/log/nginx/access_www.log main;
      root /usr/share/nginx/html/www;

location / {
      index index.php index.html index.htm;
    }

location ~* \.(php|php5)$ {
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME 
      $document_root$fastcgi_script_name;
      include fastcgi_params;
   }
}

但是在测试的时候,如果不加https,他自动是不跳转的

第六个历程:实现HTTP到HTTPS跳转的访问效果

server {
     listen 80;  
     server_name www.oldboy.com;
     #rewrite ^/(.*) [https://www.oldboy.com/](https://www.oldboy.com/)$1 redirect;
     return 302 https://$server_name$request_uri;
}
最终的配置文件

[root@web01 /etc/nginx/conf.d]# vim www.conf
server {
listen 80;
      server_name www.oldboy.com;
      #rewrite ^/(.*) https://www.oldboy.com/$1 redirect;
      return 302 https://$server_name$request_uri;
 }

server {
    listen 443 ssl;
    server_name www.oldboy.com;
    ssl on;
    ssl_certificate         ssl_key/server.crt;
    ssl_certificate_key     ssl_key/server.key;
    access_log /var/log/nginx/access_www.log main;
    root /usr/share/nginx/html/www;
location / {
     index index.php index.html index.htm;
}
location ~* \.(php|php5)$ {
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME 
     $document_root$fastcgi_script_name;
     include fastcgi_params;
 }
 }
}

至此,是单台的HTTPS的搭建与测试。

实现网站多台服务器实现HTTPS访问nginx

在负载均衡服务器上配置私钥与证书

首先配置主配置文件,具体内容如下。
[root@lb01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;

#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

upstream web_pools {
# ip_hash;
server 10.0.0.7:443 weight=2 max_fails=3 fail_timeout=10s;
server 10.0.0.8:443 weight=1 max_fails=3 fail_timeout=10s;
}

#include /etc/nginx/conf.d/*.conf;

server {
     listen 80;
     server_name www.oldboy.com;
     #rewrite ^/(.*) https://www.oldboy.com/$1 redirect;
     return 302 https://$server_name$request_uri;
}

server {
      listen 443 ssl;
      server_name www.oldboy.com;
      ssl on ;
      ssl_certificate ssl_key/server.crt;
     ssl_certificate_key ssl_key/server.key;
location / {
    proxy_pass https://web_pools;
    include proxy_params;
 }
}

然后将web01的私钥与证书拉过来,拉到和上面在web01一样的路径中。

[root@lb01 ~]#scp -rp 172.16.1.7:/etc/nginx/ssl_key ./

下面这个是个优化,可做可不做。

[root@lb01 ~]# cat hh.txt
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;

在web上面把配置文件的跳转注释掉就OK了.

最终做完在优化一下,优化成企业的需求的类型
优化完lb01的配置文件

[root@lb01 ~]# cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    upstream web_pools {
   # ip_hash;
    server   10.0.0.7:80   weight=2   max_fails=3  fail_timeout=10s;
    server   10.0.0.8:80   weight=1   max_fails=3  fail_timeout=10s;
    }

    #include /etc/nginx/conf.d/*.conf;
server   {
    listen       80;
    server_name  www.oldboy.com;
    #rewrite   ^/(.*)   https://www.oldboy.com/$1  redirect;
    return  302       https://$server_name$request_uri;
}


     server {
     listen   443 ssl;
     server_name  www.oldboy.com;
    ssl  on ; 
    ssl_certificate        ssl_key/server.crt;
    ssl_certificate_key    ssl_key/server.key;

     location / {
     proxy_pass  http://web_pools;
     include proxy_params;
     }
}
}

优化完的web配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/www.conf 
server   {
    listen      80;
    server_name  www.oldboy.com;
    # ssl  on;
    #ssl_certificate        ssl_key/server.crt;
    #ssl_certificate_key    ssl_key/server.key;
    access_log  /var/log/nginx/access_www.log  main;
    root   /usr/share/nginx/html/www;
    location / {
    index  index.php index.html index.htm;
    }
   location ~* \.(php|php5)$ {
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
   }

}

补充:
01. 后端没有配置HTTPS功能时,前端如果是HTTPS有时加载后端页面会有问题
解决方式,在后端配置文件添加上:fastcgi_param HTTPS on;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,302评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,232评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,337评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,977评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,920评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,194评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,638评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,319评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,455评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,379评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,426评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,106评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,696评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,786评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,996评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,467评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,043评论 2 341

推荐阅读更多精彩内容