nginx配置ssl证书,实现https安全访问.

前置条件:

|-----------|---------------|----------------|
| 名称 | ip地址 | 端口号 |
| nginx服务器 | 192.168.59.30 | 80/443 |
| server服务器 | 190.168.59.31 | 8080/8081/8082 |

安装nginx服务:

参见: 编译安装nginx-CSDN博客

启动后端web服务器192.168.59.31:

(#后端要被代理的web服务器要有docker服务并且配置相关的加速服务)

拉取tomcat容器镜像:

Go 复制代码
#web1机器拉取镜像
[root@web1 ~]$docker pull registry.cn-beijing.aliyuncs.com/scorpio/tomcat:lates
#检查镜像是否成功拉取
[root@web1 ~]$docker images
REPOSITORY                                        TAG       IMAGE ID       CREATED       SIZE
registry.cn-beijing.aliyuncs.com/scorpio/tomcat   latest    df50c9d355cf   6 years ago   463MB

启动容器镜像:

Go 复制代码
#下载好的容器镜像启动tomcat服务
[root@web1 ~]$docker run -d -p 8080:8080 --name tomcat registry.cn-beijing.aliyuncs.com/scorpio/tomcat
#
[root@web1 ~]$docker run -d -p 8081:8080 --name tomcat2 registry.cn-beijing.aliyuncs.com/scorpio/tomcat
#
[root@web1 ~]$docker run -d -p 8082:8080 --name tomcat3 registry.cn-beijing.aliyuncs.com/scorpio/tomcat

检查容器镜像创建情况

Go 复制代码
#容器创建成功
[root@web1 ~]$docker ps
CONTAINER ID   IMAGE                                             COMMAND             CREATED        STATUS        PORTS                                       NAMES
3ecd40461c2c   registry.cn-beijing.aliyuncs.com/scorpio/tomcat   "catalina.sh run"   35 hours ago   Up 35 hours   0.0.0.0:8082->8080/tcp, :::8082->8080/tcp   tomcat3
b207aebf5c26   registry.cn-beijing.aliyuncs.com/scorpio/tomcat   "catalina.sh run"   35 hours ago   Up 35 hours   0.0.0.0:8081->8080/tcp, :::8081->8080/tcp   tomcat2
f59eba49fb2d   registry.cn-beijing.aliyuncs.com/scorpio/tomcat   "catalina.sh run"   37 hours ago   Up 37 hours   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   tomcat
检查宿主机映射端口
Go 复制代码
#端口启动成功
[root@web1 ~]$netstat -lntup
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:8080            0.0.0.0:*               LISTEN      12269/docker-proxy
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      12472/docker-proxy
tcp        0      0 0.0.0.0:8082            0.0.0.0:*               LISTEN      12608/docker-proxy
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1013/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1138/master
tcp6       0      0 :::8080                 :::*                    LISTEN      12275/docker-proxy
tcp6       0      0 :::8081                 :::*                    LISTEN      12477/docker-proxy
tcp6       0      0 :::8082                 :::*                    LISTEN      12613/docker-proxy
tcp6       0      0 :::22                   :::*                    LISTEN      1013/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1138/master

查看服务页面是否启动成功

配置nginx服务器 192.168.59.30

(这里配置的域名以及证书需要自己从购买和申请.域名可以买个几块钱的,证书可以申请免费的)

Go 复制代码
[root@ansible-leader /usr/local/nginx/conf]$vim nginx.conf
# 不指定运行Nginx的工作进程用户,默认使用安装时设定的用户
#user  nobody;
worker_processes  1; # 设置工作进程的数量为1

events {
    worker_connections  1024; # 每个工作进程允许的最大连接数为1024
}

http { # HTTP服务的相关配置
    include       mime.types; # 包含MIME类型定义文件
    default_type  application/octet-stream; # 默认的MIME类型

    sendfile        on; # 开启高效文件传输模式
    keepalive_timeout  65; # 客户端连接保持活动状态的超时时间设置为65秒

    upstream httpds { # 定义一个名为httpds的上游服务器组
        server 192.168.59.31:8080 weight=6; # 第一台后端服务器,权重为6
        server 192.168.59.31:8081 weight=2; # 第二台后端服务器,权重为2
        server 192.168.59.31:8082 weight=2; # 第三台后端服务器,权重为2
    }

    server { # 配置一个虚拟主机
        listen       80; # 监听80端口
        #server_name  localhost; # 注释掉了默认的服务器名
        server_name  www.mfzz.xyz; # 设置服务器名为www.mfzz.xyz
        return 301 https://$host$request_uri; # 将所有HTTP请求重定向到HTTPS

        error_page   500 502 503 504  /50x.html; # 自定义错误页面位置
        location = /50x.html { # 当出现500、502、503或504错误时显示此页面
            root   html; # 错误页面位于html目录下
        }
    }

    server { # HTTPS服务配置
        listen 443 ssl; # 监听443端口,并启用SSL
        server_name www.mfzz.xyz; # 设置服务器名为www.mfzz.xyz

        ssl_certificate /etc/ssl/nginx_certs/mfzz.xyz.pem; # SSL证书路径
        ssl_certificate_key /etc/ssl/nginx_certs/mfzz.xyz.key; # SSL证书密钥路径

        # SSL安全配置
        ssl_protocols TLSv1.2 TLSv1.3; # 使用的协议版本
        ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256'; # 加密套件
        ssl_prefer_server_ciphers on; # 优先使用服务器指定的加密套件

        location / { # 处理所有请求
            proxy_pass http://httpds; # 反向代理至httpds上游服务器组
            proxy_set_header Host $host; # 设置请求头中的Host字段
            proxy_set_header X-Real-IP $remote_addr; # 设置真实的客户端IP地址
            proxy_set_header X-Forwarded-Proto https; # 设置转发协议为https
        }
    }
}

检查nginx配置文件语法并重新加载配置

Go 复制代码
[root@ansible-leader /usr/local/nginx/conf]$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@ansible-leader /usr/local/nginx/conf]$nginx -s reload
查看页面证书加载情况

证书加载成功.

相关推荐
bingbingyihao14 小时前
接口请求控制工具
java·nginx·负载均衡
Danileaf_Guo20 小时前
常用VPN性能对比测试(IPsec、L2TP VPN、SSL VPN、L2TP over IPsec等)
网络·网络协议·ssl
HEX9CF21 小时前
【Docker】快速部署 Certbot 并为 Nginx 服务器配置 SSL/TLS 证书
服务器·nginx·docker
游戏开发爱好者81 天前
使用克魔助手查看iOS 应用程序使用历史记录和耗能历史记录
websocket·网络协议·tcp/ip·http·网络安全·https·udp
教练、我想打篮球1 天前
14 nginx 的 dns 缓存的流程
nginx·dns·nslookup
日月星辰Ace1 天前
HTTP 和 HTTPS 请求流程
网络协议·http·https
小萌新上大分1 天前
nginx入门,部署静态资源,反向代理,负载均衡使用
nginx·spring cloud·nginx配置·nginx部署前端项目·nginx负载均衡策略·nginx反向代理配置
pingxiaozhao1 天前
在Ubuntu内网环境中为Gogs配置HTTPS访问(通过Apache反向代理使用IP地址)
ubuntu·https·apache
三天不学习2 天前
NginxWebUI:可视化 Nginx 配置管理工具,告别繁琐命令行!
运维·nginx
小徐Chao努力2 天前
【安全】加密算法原理与实战
安全·https·des·ssl·加密·aes·rsa