玛卡巴卡的卡巴卡玛 发表于 2025-4-9 01:35:12

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

前置条件:
名称 ip地点端标语nginx服务器192.168.59.3080/443server服务器190.168.59.318080/8081/8082 安装nginx服务:
参见: 编译安装nginx-CSDN博客
启动后端web服务器192.168.59.31:

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

拉取tomcat容器镜像:

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

启动容器镜像:

#下载好的容器镜像启动tomcat服务
$docker run -d -p 8080:8080 --name tomcat registry.cn-beijing.aliyuncs.com/scorpio/tomcat
#
$docker run -d -p 8081:8080 --name tomcat2 registry.cn-beijing.aliyuncs.com/scorpio/tomcat
#
$docker run -d -p 8082:8080 --name tomcat3 registry.cn-beijing.aliyuncs.com/scorpio/tomcat 查抄容器镜像创建情况

#容器创建成功
$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 查抄宿主机映射端口

#端口启动成功
$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 检察服务页面是否启动成功

https://i-blog.csdnimg.cn/direct/262d601383cc4495aadc32bd732c2257.png
配置nginx服务器 192.168.59.30

(这里配置的域名以及证书需要本身从购买和申请.域名可以买个几块钱的,证书可以申请免费的)
$vim nginx.conf
# 不指定运行Nginx的工作进程用户,默认使用安装时设定的用户
#usernobody;
worker_processes1; # 设置工作进程的数量为1

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

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

    sendfile      on; # 开启高效文件传输模式
    keepalive_timeout65; # 客户端连接保持活动状态的超时时间设置为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_namelocalhost; # 注释掉了默认的服务器名
      server_namewww.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配置文件语法并重新加载配置

$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
#
加载配置
$nginx -s reload
检察页面证书加载情况

https://i-blog.csdnimg.cn/direct/fba1ada9a7944558b981979de4bb86fe.png

证书加载成功.



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: nginx配置ssl证书,实现https安全访问.