2024年河北省网络建设与运维-省赛-nginx 和tomcat 服务服务步骤

题目:

5.nginx 和tomcat 服务

任务描述:利用系统自带tomcat,搭建 Tomcat网站。

(1)配置 linux2 为 nginx 服务器,网站目录为/www/nginx,默认文档 index.html 的内容为“HelloNginx”;仅允许使用域名访问,http 访问自动跳转到 https。

(2)配置 linux3 和 linux4 为 tomcat 服务 器,网站目录均为/www/tomcat,网站默认首页内容分别为“tomcatA” 和“tomcatB”,使用http默认80端口。

(3)利用 nginx 反向代理,实现 linux3 和 linux4 的 tomcat 负载均衡, 通过 https://tomcat.China_skills.cn 加密访问 Tomcat服务 器,http 访问通过301 自动跳转到https 。

步骤:

第一步配置 Linux2 为 Nginx 服务器:

1.安装 Nginx 并配置网站目录

yum install nginx* 
mkdir -p /www/nginx
echo "HelloNginx" | sudo tee /www/nginx/index.html

2. 配置 Nginx 以仅允许使用域名访问,并将 HTTP 请求自动重定向到 HTTPS:

编辑 Nginx 默认站点配置文件:/etc/nginx/sites-available/default

3.添加以下配置:

server {
    listen 80;
    server_name linux2.China_skills.cn;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name linux2.China_skills.cn;

    root /www/nginx;
    index index.html;

    ssl_certificate /path/to/your/ssl_certificate.crt;
    ssl_certificate_key /path/to/your/ssl_certificate_key.key;

    location / {
        try_files $uri $uri/ =404;
    }
}

替换 your_domain.com 为你的域名,/path/to/your/ssl_certificate.crt/path/to/your/ssl_certificate_key.key 分别为你的 SSL 证书和私钥的路径。

第二步配置 Linux3 和 Linux4 为 Tomcat 服务器:

1.安装 Tomcat

yum install -y tomcat* 

2.网站目录设置为 /www/tomcat,并修改默认首页内容为 "tomcatA" 和 "tomcatB"。

echo tomcatA > /www/tomcat/index.jsp

echo tomcatB > /www/tomcat/index.jsp

第三步配置 Nginx 实现 Tomcat 负载均衡和 HTTPS 访问:

编辑 Nginx 默认站点配置文件 /etc/nginx/sites-available/default

upstream tomcat_servers {
    server linux3.China_skills.cn:443;
    server linux4.China_skills.cn:443;
}

server {
    listen 80;
    server_name tomcat.China_skills.cn;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name tomcat.China_skills.cn;

    ssl_certificate /path/to/your/ssl_certificate.crt;
    ssl_certificate_key /path/to/your/ssl_certificate_key.key;

    location / {
        proxy_pass http://tomcat_servers;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
 

替换 /path/to/your/ssl_certificate.crt/path/to/your/ssl_certificate_key.key 分别为你的 SSL 证书和私钥的路径。

第四步重启 Nginx 服务以使更改生效:

systemctl restart nginx
systemctl enable --now nginx

测试

访问 http://linux2.China_skills.cn 应该自动跳转到 HTTPS,并显示 "HelloNginx"。 访问 https://tomcat.China_skills.cn 应该能够访问到 Tomcat 服务器,并实现负载均衡。

结果应为:

curl https://linux2.China_skills.cn

HelloNginx

curl https://tomcat.China_skills.cn

tomcatA

curl https://tomcat.China_skills.cn

tomcatB

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 07:26:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 07:26:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 07:26:03       82 阅读
  4. Python语言-面向对象

    2024-04-09 07:26:03       91 阅读

热门阅读

  1. 详解Oracle数据库的检查点(Checkpoint)

    2024-04-09 07:26:03       36 阅读
  2. 设计模式之迭代器模式

    2024-04-09 07:26:03       33 阅读
  3. verilog 和 system verilog 有什么区别?

    2024-04-09 07:26:03       38 阅读
  4. 一些考研经验

    2024-04-09 07:26:03       40 阅读
  5. Rust 实战练习 - 9. 文本编码,URL编码,加密解密

    2024-04-09 07:26:03       36 阅读
  6. 10.枚举

    10.枚举

    2024-04-09 07:26:03      38 阅读
  7. uniapp 检查更新

    2024-04-09 07:26:03       37 阅读
  8. 新型基础设施建设(新基建)

    2024-04-09 07:26:03       32 阅读
  9. golang 使用 cipher、aes 实现 oauth2 验证

    2024-04-09 07:26:03       36 阅读
  10. MySQL视图及如何导入导出

    2024-04-09 07:26:03       35 阅读
  11. 【IP层的校验和与UDP的校验和】+【FPGA实现】

    2024-04-09 07:26:03       27 阅读
  12. Prime Ring Problem(UVA 524)

    2024-04-09 07:26:03       37 阅读