1 简介
Nginx 是一个非常强大和流行的高性能 web 服务器. 本文讲解 Nginx 如何整合 https 并将 http 重定向到 https .
(1) Springboot 整合 https 原来这么简单
(2) HTTPS 之密钥知识与密钥工具 Keytool 和 Keystore-Explorer
(3) Springboot 以 Tomcat 为容器实现 http 重定向到 https 的两种方式
(4) Springboot 以 Jetty 为容器实现 http 重定向到 https
Nginx 的特点:
(1) 热启动: 例如当修改配置文件后, 不需要停止与启动就可以让配置生效, 命令如下:
nginx -s reload
(2) 高并发连接: 顶住 10 万以上连接是没有问题的.
(3) 低内存消耗: 在高性能的同时, 保持很低的内存消耗;
(4) 响应请求快;
(5) 高可靠性.
Nginx 可以做哪些事呢? 最常用的功能为下面三个:
(1) 静态 HTTP 服务器, 实现动静态分离
(2) 反向代理
(3) 负载均衡
2 安装与使用
CentOS 使用下面命令进行安装与使用:
- # 添加 Nginx 源
- rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- # 安装 Nginx
- yum install -y nginx
- # 启动 Nginx
- systemctl start nginx.service
- # 停止 Nginx
- systemctl stop nginx.service
- # 设置开机自启 Nginx
- systemctl enable nginx.service
- # 重新加载
- nginx -s reload
Mac 使用下面命令进行安装和使用:
- # 查看是否有安装
- brew info nginx
- # 安装
- brew install nginx
- # 启动, 默认端口为 8080
- nginx
- # 停止
- nginx -s stop
- # 重新加载
- nginx -s reload
安装完会有提供说明:
- Docroot is: /usr/local/var/www
- nginx will load all files in /usr/local/etc/nginx/servers/
就知道该在哪放网站资源和配置文件了.
3 整合 https
3.1 生成密钥文件
先通过 keytool 生成 PKCS12 格式的密钥, 然后通过 openssl 取出 cert 和 key , 具体命令如下:
- # 生成 PKCS12 格式的密钥文件
- keytool -genkey -alias localhost -keyalg RSA -keysize 2048 -sigalg SHA256withRSA -storetype PKCS12 -keystore localhost.p12 -dname CN=localhost,OU=Test,O=pkslow,L=Guangzhou,C=CN -validity 731 -storepass changeit -keypass changeit
- # 导出 pem(certificate)
- openssl pkcs12 -nokeys -in ./localhost.p12 -out localhost.pem
- # 导出 key
- openssl pkcs12 -nocerts -nodes -in ./localhost.p12 -out localhost.key
3.2 配置 nginx.conf
新建一个 nginx.conf 文件, 把它放在配置加载目录上. 要把密钥文件路径配置上去, 具体配置如下:
- server {
- listen 443 ssl;
- server_name localhost;
- ssl_certificate /key-path/localhost.pem;
- ssl_certificate_key /key-path/localhost.key;
- ssl_session_timeout 5m;
- ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
- ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
- ssl_prefer_server_ciphers on;
- location / {
- proxy_set_header HOST $host;
- proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://127.0.0.1:8000/;
- }
- }
记得要替换 key-path 为具体的密钥文件的路径.
ssl_certificate : 这个配置的是 cert 文件.
ssl_certificate_key : 这个配置的是 private key 文件.
proxy_pass http://127.0.0.1:8000/ : 这个作用是把请求反向代理到这个地址上.
4 开启 http 并重定向到 https
4.1 开启 http
开启 http 很简单, 直接把 listen 80; 加到 listen 443 ssl; 上面去就可以了. 或者新加一个 server 配置, 如下:
- server {
- listen 443 ssl;
- server_name localhost;
- ssl_certificate /key-path/localhost.pem;
- ssl_certificate_key /key-path/localhost.key;
- ssl_session_timeout 5m;
- ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
- ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
- ssl_prefer_server_ciphers on;
- location / {
- proxy_set_header HOST $host;
- proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://127.0.0.1:8000/;
- }
- }
- server {
- listen 80;
- server_name localhost;
- location / {
- proxy_set_header HOST $host;
- proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://127.0.0.1:8000/;
- }
- }
4.2 重定向到 https 的两种方式
要把 http 重定向到 https 也很简单, 具体可以使用两种配置来实现.
第一种方式使用 return 301 如下:
- server {
- listen 80;
- server_name localhost;
- return 301 https://127.0.0.1$request_uri;
- }
第二种方式使用 rewrite 如下:
- server {
- listen 80;
- server_name localhost;
- rewrite ^(.*)$ https://$host$1 permanent;
- }
对于 return 和 rewrite 的区别, 可以阅读这篇文章: Creating NGINX Rewrite Rules
5 总结
最后, 介绍一个工具, 可以快速方便获得 nginx 的配置: Nginx Config https://www.digitalocean.com/community/tools/nginx .
来源: http://www.tuicool.com/articles/iauAFvf