12.17 Nginx负载均衡
12.18 ssl原理
12.19 生成ssl密钥对
12.20 Nginx配置ssl
扩展
针对请求的uri来代理 http://ask.apelearn.com/question/1049
根据访问的目录来区分后端的web http://ask.apelearn.com/question/920
nginx长链接 http://www.apelearn.com/bbs/thread-6545-1-1.html
nginx算法分析 http://blog.sina.com.cn/s/blog_72995dcc01016msi.htmlphp
upstream qq_com { ip_hash; server 61.135.157.156:80; server 125.39.240.113:80; } server { listen 80; server_name www.qq.com; location / { proxy_pass http://qq_com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
[root@yong-01 vhost]# yum install bind-utils -y [root@yong-01 vhost]# dig qq.com ; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> qq.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55322 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 512 ;; QUESTION SECTION: ;qq.com. IN A ;; ANSWER SECTION: qq.com. 353 IN A 111.161.64.48 qq.com. 353 IN A 111.161.64.40 ;; Query time: 27 msec ;; SERVER: 8.8.8.8#53(8.8.8.8) ;; WHEN: 二 6月 12 21:38:12 CST 2018 ;; MSG SIZE rcvd: 67
[root@yong-01 vhost]# vim load.conf 写入如下内容 upstream qq_com //upstream后的名称自定义 { ip_hash; //目的是为了让同一个用户始终保持在同一个机器上 server 111.161.64.40:80; //若是域名解析端口是80,这段配置上的指定端口80是能够省略的 server 111.161.64.48:80; } server { listen 80; //定义监听端口 server_name www.qq.com; //域名 location / { proxy_pass http://qq_com; //这里填写的是upstream 的名字 即“http://upstream”,由于做为一个模块,代理访问的是经过解析后的IP访问; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
[root@yong-01 vhost]# curl -x127.0.0.1:80 www.qq.com This is a test default site.
[root@yong-01 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 www.qq.com
在本身的虚拟机生成ssl 须要用到openssl工具html
[root@yong-01 ~]# cd /usr/local/nginx/conf/
[root@yong-01 conf]# rpm -qf `which openssl` openssl-1.0.2k-8.el7.x86_64
[root@yong-01 conf]# openssl genrsa -des3 -out tmp.key 2048 Generating RSA private key, 2048 bit long modulus .......+++ ......................................................................+++ e is 65537 (0x10001) Enter pass phrase for tmp.key: //输入密码 123456 Verifying - Enter pass phrase for tmp.key: //再次输入密码 123456
[root@yong-01 conf]# openssl rsa -in tmp.key -out yyl.key Enter pass phrase for tmp.key: //输入tmp.key的密码 123456 writing RSA key
[root@yong-01 conf]# rm -f tmp.key
[root@yong-01 conf]# openssl req -new -key yyl.key -out yyl.csr 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 //国家,2个字母 State or Province Name (full name) []:guangdong //省或州 Locality Name (eg, city) [Default City]:guangdong //城市 Organization Name (eg, company) [Default Company Ltd]:li //公司 Organizational Unit Name (eg, section) []:li //组织 Common Name (eg, your name or your server’s hostname) []:yueyong //您的主机名 Email Address []:yyli2008@163.com //邮箱 Please enter the following ‘extra’ attributes to be sent with your certificate request A challenge password []:yueyong //设置密码 An optional company name []:li //一个可选的公司名称 用请求证书文件和私钥文件,生成一个公钥
[root@yong-01 conf]# openssl x509 -req -days 365 -in yyl.csr -signkey yyl.key -out yyl.crt Signature ok subject=/C=cn/ST=guangdong/L=shenzhen/O=li/OU=li/CN=yueyong/emailAddress=yyli2008@163.com Getting Private key
server { listen 443; server_name aming.com; index index.html index.php; root /data/wwwroot/aming.com; ssl on; ssl_certificate aminglinux.crt; ssl_certificate_key aminglinux.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; }
[root@yong-01 conf]# vim /usr/local/nginx/conf/vhost/ssl.conf 添加如下内容 server { listen 443; //监听端口为443 server_name yongge.com; //主机名 index index.html index.php; root /data/wwwroot/yongge.com; //root 目录 ssl on; //开启ssl ssl_certificate yyl.crt; //指定公钥 ssl_certificate_key yyl.key; //指定私钥 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //ssl 的协议 }
[root@yong-01 vhost]# mkdir /data/wwwroot/yongge.com
[root@yong-01 vhost]# /usr/local/nginx/sbin/nginx -t nginx: [emerg] unknown directive "erver" in /usr/local/nginx/conf/vhost/ssl.conf:2 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@yong-01 vhost]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.4.7 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) configure arguments: --prefix=/usr/local/nginx
[root@yong-01 vhost]# cd /usr/local/src/nginx-1.4.7/ [root@yong-01 nginx-1.4.7]# ./configure --help |grep -i ssl --with-http_ssl_module enable ngx_http_ssl_module --with-mail_ssl_module enable ngx_mail_ssl_module --with-openssl=DIR set path to OpenSSL library sources --with-openssl-opt=OPTIONS set additional build options for OpenSSL
[root@yong-01 nginx-1.4.7]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@yong-01 nginx-1.4.7]# make install
[root@yong-01 nginx-1.4.7]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.4.7 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
[root@yong-01 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@yong-01 vhost]# service nginx restart Restarting nginx (via systemctl): [ 肯定 ]
[root@yong-01 vhost]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4311/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1114/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1470/master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 4311/nginx: master tcp6 0 0 :::22 :::* LISTEN 1114/sshd tcp6 0 0 ::1:25 :::* LISTEN 1470/master tcp6 0 0 :::3306 :::* LISTEN 1426/mysqld
[root@yong-01 vhost]# cd /data/wwwroot/yongge.com/ [root@yong-01 yongge.com]# ls [root@yong-01 yongge.com]# vim index.html This is a ssl.
[root@yong-01 yongge.com]# curl -x127.0.0.1:443 https://yongge.com/ curl: (56) Received HTTP code 400 from proxy after CONNECT
[root@yong-01 yongge.com]# vim /etc/hosts 加入如下内容 127.0.0.1 yongge.com
[root@yong-01 yongge.com]# curl https://yongge.com/ curl: (60) Peer's certificate issuer has been marked as not trusted by the user. More details here: http://curl.haxx.se/docs/sslcerts.html curl performs SSL certificate verification by default, using a "bundle" of Certificate Authority (CA) public keys (CA certs). If the default bundle file isn't adequate, you can specify an alternate file using the --cacert option. If this HTTPS server uses a certificate signed by a CA represented in the bundle, the certificate verification probably failed due to a problem with the certificate (it might be expired, or the name might not match the domain name in the URL). If you'd like to turn off curl's verification of the certificate, use the -k (or --insecure) option.
192.168.180.134 yongge.com
浏览器访问yongge.com,会看到加载超时mysql
这时查看虚拟机防火墙iptables -nvL,如果防火墙存在,能够直接ipbables -F清空全部规则,若不想清空全部规则能够增长443端口的规则 iptables -I INPUT -p tcp --dport 443 -j ACCEPTlinux