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