|
|
ngin配置https协议
: t& Z: S; Y6 F2 C; C1.https简介" ?" J! \& g! l& q. y. Y5 z
HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据
) e" h6 O; j. }2.https协议原理
" n! s, G, q& I% J/ C( H& Y; J& i- W首先,客户端与服务器建立连接,各自生成私钥和公钥,是不同的。服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来
# `2 d5 h9 |' I( D" }1 X# Z) m! p8 i. L- |
TLS或传输层安全( transport layer security),它的前身是SSL(安全套接字层secure sockets layer),是Web协议用来包裹在一个受保护,加密封装正常通道。' a# y9 [/ q* T1 G( Z
采用这种技术,服务器和客户端之间可以安全地进行交互,而不用担心消息将被拦截和读取。证书系统帮助用户在核实它们与连接站点的身份。
5 A0 U. f, w; z
$ p, b- @- v# [$ d
5 }- |7 i8 C# C5 a9 e [2 aopenssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/nginx/conf/ssl/nginx.key -out /usr/local/nginx/conf/ssl/nginx.crt7 w* J+ S. s, \ O" N
, K2 f; \; l4 c$ s( l! x' ^( Y
s. T, s+ O6 E; U0 z3 eGenerating a 2048 bit RSA private key" c8 y8 `' O1 y8 D
...................+++' W& B' l0 S: ?& \- q
........+++
: Z; ~. c) W# l: w5 ^writing new private key to '/usr/local/nginx/conf/ssl/nginx.key'
7 M" T; x/ a6 J+ i, O# K-----) U" o+ R9 S# u5 \; E8 A6 T
You are about to be asked to enter information that will be incorporated
$ w, o" O" d& d$ ointo your certificate request.+ N/ T, C) ~7 c
What you are about to enter is what is called a Distinguished Name or a DN.4 X5 h' ^; Z2 W/ d h% P
There are quite a few fields but you can leave some blank5 r( x: w* o; Z7 M0 S) Q; B) d
For some fields there will be a default value,, M$ u7 } K7 n
If you enter '.', the field will be left blank.
$ q: |: u4 u) |, k2 l4 K-----) k8 N+ D% S( W) X0 r# y' K
+ O3 B! r( K4 W/ s8 M1 o. E0 rCountry Name (2 letter code) [XX]:BJ % a1 @. O2 T- v" C5 {1 y4 e# s
State or Province Name (full name) []:Beijing
* n1 T% _- x b& C! e6 H% j" N+ |5 gLocality Name (eg, city) [Default City]:Beijing
" z& X3 r; Q0 A" U# _" t! ?1 _Organization Name (eg, company) [Default Company Ltd]:Beijing
8 P& g! M; D; }; BOrganizational Unit Name (eg, section) []:
1 B# k, ?6 ^8 N) s$ HCommon Name (eg, your name or your server's hostname) []:
4 y1 r% q) o5 v WEmail Address []:
1 C2 r( r& }; L! ^
, h, B9 a; f% S4 o# v
9 \; X2 B: K" S" n6 b: t3 x, L步骤2: Configure Nginx to Use SSL首先配置HTTP请求重定向 首先配置HTTP请求重定向 server { 0 X$ F3 Z9 q/ n3 A4 z; g
$ C: D" U4 ~$ H, o( L8 s2 R; l' r listen 80;
- O& s1 e3 D* m, F( e6 n' m% c& O. J9 C9 D4 d7 ~0 n* m
server_name www.yourdomain.com; ! M( W, K0 z$ X* x
; h: ~9 \2 f3 L C! |+ `7 z
rewrite ^ https://$http_host$request_uri? permanent; # force redirect http to https #return 301 https://$http_host$request_uri; 5 D0 p% c+ l9 e2 T9 R
- O& r4 f9 _9 w; D }* M5 K" ~7 @1 _! z1 P
* I' i: O: n: a# l \/ a0 Z8 m# j$ l! fserver {
8 [9 |( ]! L) a5 x$ {! j; b- v7 u listen 443 ssl;
' M2 e& B; I1 y) ^* z$ S. s* t ssl_certificate /etc/nginx/ssl/nginx.crt;
8 A; W( ?* H0 n/ E6 W' \6 z+ ~& N9 `& ?/ p% m2 B5 ~* V
ssl_certificate_key /etc/nginx/ssl/nginx.key;
, t2 I f: I5 S$ E1 T/ x% r0 k keepalive_timeout 70;1 y0 t# _ a' w- V% Y/ Z) o1 ?
9 E9 _5 t% r! B' L. o7 A3 P server_name www.yourdomain.com; #禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击 & w# l2 R' j* z9 |* Y
7 G3 I1 X! r4 z& j2 }; i, X
server_tokens off; #如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访问 #add_header Strict-Transport-Security "max-age=31536000; includeSubdomains"; # ......
6 F. P% r3 m: R+ X' ~1 M9 J8 O7 `1 c- J' w2 W
fastcgi_param HTTPS on;/ M: w9 i) |- X. E
% d0 q1 x2 |) C/ e( `
fastcgi_param HTTP_SCHEME https;0 ^+ y6 \9 f; y
/ H: v2 R+ K" P1 ^7 }( [ access_log /usr/local/nginx/logs/wiki.xby1993.net.access.log;
) x) d+ b7 y1 X2 I4 r( f0 j2 M3 X9 L
error_log /usr/local/nginx/logs/wiki.xby1993.net.error.log;
: b$ @1 G+ m% _; Y( [! a" h' H
3 e+ ]% U4 g3 D* R$ q) Y r, f9 S" }}4 h U _) r: _* E; g# {- O& E
6 `9 U2 Q& C4 D d6 ~$ G
如果想同时启用HTTP和HTTPS ( t" c: x" k) ~6 \
; j) h* Y2 f. a
server { 7 }* T; q2 {. A: L: \% F* J
listen 80;
4 p7 t' x' _0 K# C! H$ J0 }- Y5 | ~6 b3 D! K- W' @
listen 443 ssl; ( Z( J: _+ L( b6 y& y8 C
$ }7 l) y: K# v) q% e7 W/ r" w/ O server_name www.example.com;
! y9 Z3 c5 b9 E7 n& P. p/ G$ `. y
ssl_certificate www.example.com.crt;
9 @6 b0 n4 ?" L8 N# q ssl_certificate_key www.example.com.key;
; o4 B$ a. E2 `1 \5 K" b# _* u3 [5 e5 N
# G, P2 h5 l& r+ r8 X6 b ...' h$ e: G+ ]; N1 q; b
}
! _% ^6 R, g- G0 a5 i9 r4 w- j' u% _# |( V1 T
|
|