zoukankan      html  css  js  c++  java
  • 16.Nginx HTTPS实践

    1.不做任何修改实现http跳转https(协议间的跳转): return

    [root@web01 conf.d]# cat url.cheng.com.conf 
    server {
    	listen 80;
    	server_name url.cheng.com;
    	return 302 https://$http_host;	
    }
    

    2.http跳转https [rewrite]

    [root@web01 conf.d]# cat url.cheng.com.conf 
    server {
    	listen 80;
    	server_name url.cheng.com;
    	rewrite ^/$ https://$http_host;
    	}
    

    3.rewrite中的flag

    • 地址做改写 rewrite 跳转
    1. redirect	302		临时跳转	 旧网站无影响,新网站无排名
    
    [root@web01 conf.d]# cat url.cheng.com.conf 
    server {
    	listen 80;
    	server_name url.cheng.com;
    	rewrite ^/$ https://$http_host redirect;
    	}
    
    2. permanent	301		永久跳转     新跳转网站有排名,旧网站排名清空
    
    [root@web01 conf.d]# cat url.cheng.com.conf 
    server {
    	listen 80;
    	server_name url.cheng.com;
    	rewrite ^/$ https://$http_host permanent;
    	}
    
    http  ---> https   302		浏览器不会记住新域名
    http  ---> https   301		浏览器会记录新域名
    

    4.last 本条规则匹配完成后,继续向下匹配新的location URI规则

    [root@web01 conf.d]# cat url.cheng.com.conf 
    server {
        listen 80;
        server_name url.cheng.com;
        root /code;
    
        location / {
            rewrite /1.html /2.html last;
            rewrite /2.html /3.html;
        }
    
        location /2.html {
            rewrite /2.html /a.html;
        }
    
        location /3.html {
            rewrite /3.html /b.html;
        }
    } 
    

    5.break #本条规则匹配完成即终止,不再匹配后面的任何规则

    [root@web01 conf.d]# cat url.cheng.com.conf 
    server {
        listen 80;
        server_name url.cheng.com;
        root /code;
    
        location / {
            rewrite /1.html /2.html break;
            rewrite /2.html /3.html;
        }
    
        location /2.html {
            rewrite /2.html /a.html;
        }
    
        location /3.html {
            rewrite /3.html /b.html;
        }
    } 
    

    当rewrite规则遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。
    当rewrite规则遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。

    6.需求: 公司网站在停机维护时,指定的IP能够正常访问,其他的IP跳转到维护页。10.0.0.1 10.0.0.100

    方法一:

    [root@web01 conf.d]# cat sl.cheng.com.conf 
    server {
    	listen 80;
    	server_name sl.cheng.com;
    	root /data;
    	
    	if ($remote_addr !~* "10.0.0.4|10.0.0.55") {
    	rewrite ^(.*)/$ /wh.png break;
    	}
    
    	location / {
    	index index.html;
    	}
    }
    

    方法二:

    [root@web01 conf.d]# cat sl.cheng.com.conf 
    server {
    	listen 80;
    	server_name sl.cheng.com;
    	root /data;
    	
    	set $ip 0;
    	if ($remote_addr !~* "10.0.0.4|10.0.0.55") {
    		set $ip 1;
    }
    	if ($ip = "0") {
    		rewrite ^(.*)/$ /wh.png break;
    	}
    
    	location / {
    	index index.html;
    	}
    }
    

    7.需求:公司网站后台/admin,只允许公司的出口公网IP可以访问,其他的IP访问全部返回500,或直接跳转至首页。

    方法一:

    [root@web01 conf.d]# cat sl.cheng.com.conf 
    server {
    	listen 80;
    	server_name sl.cheng.com;
    	root /data;
    	
    	location /admin {
    	index index.html;
    	}
    
    	if ($remote_addr !~* "10.0.0.4|10.0.0.55") {
    	return 500 ;
    	}	
    }
    

    方法二:

    [root@web01 conf.d]# cat sl.cheng.com.conf 
    server {
    	listen 80;
    	server_name sl.cheng.com;
    	root /data/admin;
    
    	if ($remote_addr !~* "10.0.0.4|10.0.0.55") {
    	rewrite ^(.*)/$ https://www.xuliangwei.com;
    	}
    	
    	location / {
    	index index.html;
    	}
    }
    

    方法三:

    [root@web01 conf.d]# cat sl.cheng.com.conf 
    server {
    	listen 80;
    	server_name sl.cheng.com;
    	root /data/;
    
    	if ($remote_addr !~* "10.0.0.4|10.0.0.55") {
    	return 500;
    	}
    	
    	location /admin {
    	index index.html;
    	}
    }
    

    8.模拟不使用Https的劫持和篡改

    [root@lb01 conf.d]# cat proxy_a.cheng.com.conf 
    server {
    	listen 80;
    	server_name a.cheng.com;
    	
    
    	location / {
    	proxy_pass http://10.0.0.7;
    	sub_filter 'hello PC...' '123456';
    	include proxy_params;
    	}
    }
    
    PS:将hello pc 替换为123456
    

    9.HTTPS证书购买指南 cheng.com

    保护1个域名 www								docs.cheng.com
    保护5个域名 www images cdn test m			docs.cheng.com  www.cheng.com iamges.cheng.com
    通配符域名 *.cheng.com
    		1套证书
    		保护所有的域名
    

    10.HTTPS注意事项

    Https不支持续费,证书到期需重新申请新并进行替换。
    Https不支持三级域名解析,如 test.m.oldboy.com。
    Https显示绿色,说明整个网站的url都是https的,并且都是安全的。
    Https显示黄色,说明网站代码中有部分URL地址是http不安全协议的。
    Https显示红色,要么证书是假的,要么证书已经过期。

    11.实现单台Https

    0.创建存放ssl证书的路径
    [root@Nginx ~]# mkdir -p /etc/nginx/ssl_key
    [root@Nginx ~]# cd /etc/nginx/ssl_key
    
    1.生成证书 (密码1234)
    [root@Nginx ~]# openssl genrsa -idea -out server.key 2048
    
    2.生成自签证书,同时去掉私钥的密码
    openssl req -days 36500 -x509 
    -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
    	
    3.配置nginx
    [root@web01 conf.d]# cat s.cheng.com.conf 
    server {
    	listen 443 ssl;
    	server_name s.cheng.com;
    	charset utf-8;
    	root /code;
    	ssl_certificate ssl_key/server.crt;
    	ssl_certificate_key ssl_key/server.key;
    
    	location / {
    	index index.html;
    	}
    
    }
    
    server {
    	listen 80;
    	server_name s.cheng.com;
    	return 302 https://$http_host$request_uri;
    	}
    
    >>思路:		
    1.先配置好后端的web节点
    2.在负载均衡上申请证书(如果之前申请过也可以推送)      <----
    3.配置nginx负载均衡--->http协议
    4.配置域名劫持
    5.配置nginx负载均衡--->转为https协议
    
    [root@lb01 conf.d]# cat proxy_s.oldxu.com.conf 
    upstream webs {
    	server 172.16.1.7:80;
    	server 172.16.1.8:80;
    }
    
    server {
    	listen 443 ssl;
    	ssl_certificate ssl_key/server.crt;
    	ssl_certificate_key ssl_key/server.key;
    
    	server_name s.oldxu.com;
    	location / {
    		proxy_pass http://webs;
    		include proxy_params;
    	}
    }
    
    server {
    	listen 80;
    	server_name s.oldxu.com;
    	return 302 https://$http_host$request_uri;
    }
    

    12.Wordpress网站加HTTPS

    [root@lb01 conf.d]# cat proxy_word.cheng.com.conf 
    upstream word {
    	server 172.16.1.7;
    }
    
    server {
    	listen 443 ssl;
    	server_name word.cheng.com;
    	
    	ssl_certificate ssl_key/server.crt;
    	ssl_certificate_key ssl_key/server.key;
    
    	location / {
    	proxy_pass http://word;
    	include proxy_params;
    	}	
    }
    
    server {
    	listen 80;
    	server_name word.cheng.com;
    	return 302 https://$http_host$request_uri;
    }
    
    [root@web01 conf.d]# cat word.cheng.com.conf 
    server {
    	listen 80;
    	server_name word.cheng.com;
    	root /code/wordpress;
    	
    	location / {
    	index index.php;
    	}
    
    	location ~ .php$ {
    	fastcgi_param HTTPS on;
    	fastcgi_pass 127.0.0.1:9000;
    	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    	include fastcgi_params;
    	}
    }
    

    13.阿里云申请免费的证书------->真实

    s.chengyinwu.com
    

    14.SLB+ECS配置Https

    1.四层配置:TCP---->6666---->添加ECS---->端口22
    2.xshell连接负载公网ip 6666
    安装nginx 
    [root@lb01 conf.d]# cat sui.cheng.com.conf
    server {
    	listen 8080;
    	server_name sui.cheng.com;
    	root /code0;
    	
    	location / {
    	index index.html;
    	}
    }
    	
    
    server {
    	listen 8081;
    	server_name sui.cheng.com;
    	root /code1;
    	
    	location / {
    	index index.html;
    	}
    }
    [root@lb01 ~]# mkdir /code0
    [root@lb01 ~]# mkdir /code1
    [root@lb01 ~]# echo "web01..." > /code0/index.html
    [root@lb01 ~]# echo "web02..." > /code1/index.html
    在nginx主配置文件删除80端口
    监听配置向导----->http --80---->新建虚拟服务器组web--添加两台ECS,端口8080--8081---->检查域名---七层配置完毕
    云解析--添加记录--解析至公网ip
    通过浏览器访问真实域名
    监听配置--->HTTPS-->443--选择证书(部署到负载均衡)--->选择web服务器组--->检查域名---七层配置完毕
    监听---->删除80端口
    监听配置---->http--->80--->监听转发---》HTTPS:443
    通过HTTPS访问---显示web01 02实现轮询
    

    15.需求: 部分URL走https,部分不走https

    s.cheng.com/login ---> https

    [root@lb01 conf.d]# cat proxy_s.cheng.com.conf
    upstream webs {
    	server 172.16.1.7:80;
    	server 172.16.1.8:80;
    }
    
    server {
    	listen 443 ssl;
    	ssl_certificate ssl_key/server.crt;
    	ssl_certificate_key ssl_key/server.key;
    
    	server_name s.cheng.com;
    	location / {
    	proxy_pass http://webs;
    	include proxy_params;
    	}
    }
    
    server {
    	listen 80;
    	server_name s.cheng.com;
    
    	if ($request_uri ~* "^/login") { 
        	return 302 https://$http_host$request_uri;	
    	}
    
    	location / {
    	proxy_pass http://webs;
            include proxy_params;
    	}
    }
    

    16.需求: 当用户请求s.cheng.com/abc时走http,其他的所有都走https

    	s.oldxu.com/       ---> https
    	s.oldxu.com/abc    ---> http
    
    [root@lb01 conf.d]# cat proxy_s.oldxu.com.conf 
    upstream webs {
    	server 172.16.1.7:80;
    	server 172.16.1.8:80;
    }
    
    server {
    	listen 443 ssl;
    	ssl_certificate ssl_key/server.crt;
    	ssl_certificate_key ssl_key/server.key;
    
    	server_name s.oldxu.com;
    	location / {
    		proxy_pass http://webs;
    		include proxy_params;
    	}
    }
    server {
    	listen 80;
    	server_name s.oldxu.com;
    	
    	if ($request_uri !~* "^/abc") {
    		return 302 https://$http_host$request_uri;
    	}
    
    	location / {
    		proxy_pass http://webs;
    		include proxy_params;
    	}
    }
    

    17.https优化相关的参数?

    server {
        listen 443 ssl;
        server_name nginx.bjstack.com;
    	
        ssl_certificate ssl_key/1524377920931.pem;
        ssl_certificate_key ssl_key/1524377920931.key;
    	ssl_session_cache shared:SSL:10m; 	#在建立完ssl握手后如果断开连接,在session_timeout时间内再次连接,是不需要在次建立握手,可以复用之前的连接
        ssl_session_timeout 1440m;           #ssl连接断开后的超时时间
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用的TLS版本协议
        ssl_prefer_server_ciphers on;        #Nginx决定使用哪些协议与浏览器进行通讯
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #配置加密套间
    
    l	ocation / {
            root /soft/code;
            index index.html index.htm;
        }
    }
    server {
            listen 80;
            server_name nginx.bjstack.com;
            return 302 https://$server_name$request_uri;
    }
    

    18.总结

    1.https:
    2.https,模拟DNS劫持
    3.https证书颁发流程
    4.配置Https
    单台服务器 LNMP架构
    多台服务器 nginx+LNMP架构
    1.先实现http
    2.在来实现https
    5.阿里云申请真是的证书 --->捆绑对应的域名
    6.配置SLB+ECS集群--->http方式
    7.配置SLB+ECS集群--->https方式
    8.将http的所有请求--->https上
    9.SLB如何支持多个域名
    10.https在使用上一些场景,或者说一些个需求:
    1.指定某些url走https,其他都是http协议?
    2.所有资源都走https,某一个或某两个url不走https?
    11.https配置上优化相关一些个参数? session_cache session_timeout

  • 相关阅读:
    ssh.sh_for_ubuntu1604
    ssh.sh_for_ubuntu1404
    ssh.sh_for_ubuntu1204
    ssh.sh_for_centos
    raw,cow,qcow,qcow2镜像的比较
    Oz 创建Windows2008R2镜像
    Oz 创建Ubuntu镜像
    Oz 创建Debian8镜像
    Oz 创建CentOS7镜像
    Oz 创建CentOS6镜像
  • 原文地址:https://www.cnblogs.com/yinwu/p/11616452.html
Copyright © 2011-2022 走看看