本文旨在测试 nginx对 http服务 https 服务 的代理方式。
部署测试 http服务准备测试服务程序 gintest 并启动如下 [root@localhost ~]# sudo nohup ./gintest 9000 &[1] 4229[root@localhost ~]# nohup: 忽略输入并把输出追加到"nohup.out"[root@localhost ~]# [root@localhost ~]# curl http:localhost:9000curl: (6) Could not resolve host: http:localhost; 未知的错误[root@localhost ~]# curl http://localhost:9000{"message":"测试程序,服务器ip:192.168.90.9,端口:9080"}[
nginx反向代理 http服务安装 nginx 并修改配置文件 ,对 web服务进行代理如下 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://192.168.90.9:9000; root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } 启动 nginx 并访问,结果如下 代理成功 
nginx 通过 https反向代理 http服务使用 https 服务需要安装http_ssl_module 否则会报错 『ssl parameter requires ngx_http_ssl_module』 重新编译安装 nginx nginx源码根目录执行如下命令 ,重新编译 ,输出目录保持不变 ./configure --with-http_stub_status_module --with-http_ssl_module make && make install 安装 成功后可以看到我们的 nginx 目录下已经安装了新版本的 nginx 
配置 nginx 提前准备好一套证书和私钥, tls.crt tls.key nginx http 模块配置中,增加如下配置 server { listen 443 ssl; server_name www.mytest.com; ssl_certificate /root/tls.crt; ssl_certificate_key /root/tls.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_pass http://192.168.90.9:9000; root html; index index.html index.htm; } } 重新加载 配置 [root@localhost conf]# ../sbin/nginx -s reload 访问测试 通过https访问应用成功,如下: 
这种情况下需要将 应用的证书配置到nginx,如果 nginx代理了很多应用,每个应用证书不同,那么需要配置很多证书。 nginx提供了 stream 模块用于 tcp/udp 请求直接转发后台服务器处理,不用配置证书。如下
Nginx Stream模块负载均衡测试
准备测试用 https服务准备一个 java https 服务 部署 tomcat及 web示例应用 
生成tomcat 证书 [root@mysql tomcat]# keytool -genkey -v -alias tomcat -keyalg RSA -keystore /root/tomcat/certs/tomcat.keystore -validity 365输入密钥库口令: 再次输入新口令: 您的名字与姓氏是什么? [Unknown]: zhang您的组织单位名称是什么? [Unknown]: pcitc您的组织名称是什么? [Unknown]: ptitc您所在的城市或区域名称是什么? [Unknown]: bj您所在的省/市/自治区名称是什么? [Unknown]: bj该单位的双字母国家/地区代码是什么? [Unknown]: bjCN=zhang, OU=pcitc, O=ptitc, L=bj, ST=bj, C=bj是否正确? [否]: y正在为以下对象生成 2,048 位RSA密钥对和自签名证书 (SHA256withRSA) (有效期为 365 天): CN=zhang, OU=pcitc, O=ptitc, L=bj, ST=bj, C=bj输入 <tomcat> 的密钥口令 (如果和密钥库口令相同, 按回车): [正在存储/root/tomcat/certs/tomcat.keystore]Warning:JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore /root/tomcat/certs/tomcat.keystore -destkeystore /root/tomcat/certs/tomcat.keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。[root@mysql tomcat]# ll certs总用量 4-rw-r--r-- 1 root root 2201 3月 3 00:14 tomcat.keystore 配置 tomcat SSL 连接 tomcat 配置文件 server.xml中增加如下内容 : 
https服务访问测试 通过 https 访问web服务成功 https://192.168.90.20:8443/testWeb/index.jsp 
nginx stream 安装配置nginx 安装 stream 模块 重新编译安装 nginx ./configure --with-streammake && make install 配置 nginx stream nginx.conf配置文件中增加 stream配置(和 http 模块并列) 
访问测试 通过 nginx 443 端口可以在没有配置证书的情况下可以通过 https访问后台的应用。 
负载均衡策略该模块支持的负载均衡策略如下: Round Robin 轮询– 默认情况下,NGINX 使用 Round Robin 算法对流量进行负载平衡,将其按顺序定调用上游组中的服务器。因为是默认方法,所以没有 round 下载地址: 使用Docker部署打包发布springboot项目 Docker远程连接设置的实现示例 |