背景

服务器部署多网站,存在一个项目独占了80/443端口,该项目部署在docker中,其中创建了nginx的docker实例。现需后续添加的网站也可以通过域名正常访问,而不是通过域名:端口的形式访问,采用宿主机nignx方式进行管理,无论是http的80还是https的443均先经过该nginx再路由到相应的应用。

步骤:

1.申请域名泛解析的证书(这样多个二级域名可以共用同一个证书)

为该泛解析域名(*.amebob.cn)申请证书:acme.sh --issue --dns dns_cf -d amebob.cn -d "*.amebob.cn"

注意:之前已经安装了acme.sh,并配置过了基本信息,下面提供CloudFire的方式

export CF_Key=""
export CF_Email=""

acme.sh --issue --dns dns_cf -d test.amebob.cn

2.放置证书到指定文件夹下,并确定有权限访问(方便统一管理)

安装证书并放置到/etc/nginx/ssl目录下

acme.sh --install-cert -d amebob.cn \
--key-file       /etc/nginx/ssl/transfer.key  \
--fullchain-file /etc/nginx/ssl/transfer.crt \
--reloadcmd     "systemctl force-reload nginx"

chmod -R 755 /etc/nginx/ssl授予权限

3.各个网站/项目 配置单独的Nignx server文件,再软链接到生效目录中

各项目的Nignx server文件单独写在/etc/nginx/sites-available#下,配置样例如下:

root@bob:# cat /etc/nginx/sites-available/sense-eat
server {
    listen 80;
    server_name sense-eat.amebob.cn;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name sense-eat.amebob.cn;

    ssl_certificate     /etc/nginx/ssl/transfer.crt; 
    ssl_certificate_key /etc/nginx/ssl/transfer.key;

    # 建议的 SSL 安全优化
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://127.0.0.1:8080; # 对应 Java App 容器暴露的端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

以Debian系统为例

通过ln -s /etc/nginx/sites-available/sense-eat /etc/nginx/sites-enabled/进行软链接,将配置文件sense-eat链接到/etc/nginx/sites-enabled/目录下,这样nignx在扫描时就可以得到。

使用ls -l /etc/nginx/sites-enabled/便可查看该/etc/nginx/sites-enabled/目录下的软链接。

注意⚠️:若是centos系统,则应给于/etc/nginx/conf.d/目录下创建网站的nignx配置即可,不用软链接,nignx扫描时默认扫描该目录下的所有网站nignx配置。这是因为centos中的nignx配置默认包含include /etc/nginx/conf.d/*.conf;,而debian中则是include /etc/nginx/sites-enabled/*;

重启nignx前使用·nginx -t进行Nignx server文件配置检测,再使用systemctl reload nginx重启nignx

4.更新原程序的docker-compose.yml,删除其中的nignx配置,并将其它配置的端口附加本地地址127.0.1。

出现问题:原本的sense-eat程序无法公网访问

验证逻辑(定位问题所在)

通过curl -I http://127.0.0.1:8080 确认后端活着

通过 docker logs -f senseeat-app查看docker日志,得到了后端打印的数据库查询信息。

说明应用后端没有问题。认为问题出在Ngnix这里。

查看防火墙端口放行情况,端口已放行。

root@bob:~# ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 20262/tcp                  ALLOW IN    Anywhere                  
[ 2] 80/tcp                     ALLOW IN    Anywhere                  
[ 3] 443/tcp                    ALLOW IN    Anywhere   

查看nignx日志tail -f /var/log/nginx/access.log /var/log/nginx/error.log

得到大量301信息

ai分析:

为什么 Flexible 会导致“301 重定向过多”?

根本原因:

  1. 用户输入 https://sense-eat.amebob.cn

  2. Cloudflare 接收请求,由于设为 Flexible,CF 认为你的服务器“比较老旧,不支持加密”,于是它通过 HTTP (80端口) 去访问你的服务器。

  3. 你的 Nginx 接到请求,发现是 HTTP,触发了你写的配置:return 301 https://$host$request_uri;

  4. Nginx 告诉 Cloudflare:“嘿,请用加密的 HTTPS 访问我!”

  5. Cloudflare 收到这个 301 信号,又重新发起请求,但因为它还是 Flexible 模式,它依然固执地使用 HTTP 去请求你。

  6. 死循环形成:浏览器最终报错 ERR_TOO_MANY_REDIRECTS

Cloudflare中设置SSL/TLS为Full (Strict),解决问题。

常见的local test(测试docker实例是否正确返回数据)

这里一般在服务器shell中使用curl命令,进行get,post等请求,查看返回数据是否正常。

常用的curl参数

写法含义常见用途示例
-H设置请求头指定 Content-Type、Authorization 等-H ‘Content-Type: application/json’
-d发送请求体数据提交 JSON、表单数据;常用于 POST-d ‘[“北京烤鸭”,“炸酱面”]’
-i显示响应头和响应体同时看状态码、响应头、返回内容curl -i https://sense-eat.amebob.cn/local-dish/BJ
-I只获取响应头测试状态码、重定向、证书响应,不看响应体curl -I https://sense-eat.amebob.cn
| jq把 JSON 输出交给 jq 格式化/查询美化 JSON,提取字段curl -s https://sense-eat.amebob.cn/local-dish/BJ | jq
-s静默模式不显示进度条,适合配合 jqcurl -s … | jq
-X POST指定 HTTP 方法为 POST测试 POST、PUT、DELETE 接口
[root@iZgw0ee0yux4vr71p4i7xgZ conf.d]# curl -s -X POST https://sense-eat.amebob.cn/model1/dishInfo \
>   -H 'Content-Type: application/json' \
>   -d '["北京烤鸭","炸酱面"]' | jq
{
  "code": 200,
  "message": "操作成功",
  "data": [
    {
      "id": 1,
      "nameCn": "北京烤鸭",
      "nameEn": "Peking Roast Duck",
      "descriptionCn": "选用优质填鸭,经挂炉明火烤制,皮酥脆、肉嫩香,配荷叶饼、甜面酱、葱丝、黄瓜条同食。",
      "descriptionEn": "Made with specially fattened ducks roasted in a closed oven, featuring crisp skin and tender meat, served with thin pancakes, sweet bean sauce, scallions, and cucumber.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/72add3b313724dbebdd4043ce6a1d603.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": "db"
    },
    {
      "id": 2,
      "nameCn": "炸酱面",
      "nameEn": "Zhajiangmian (Noodles with Fried Soybean Sauce)",
      "descriptionCn": "手擀面配黄酱与肉末慢炒制成的浓香炸酱,辅以黄瓜、萝卜、豆芽等‘面码’,咸鲜醇厚,老北京家常主食。",
      "descriptionEn": "Hand-rolled noodles topped with savory fried sauce made from fermented soybean paste and minced pork, served with fresh vegetable garnishes like cucumber and radish.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/65a947df51f94d619037c634a7736990.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": "db"
    }
  ],
  "timestamp": 1773547795173
}
[root@iZgw0ee0yux4vr71p4i7xgZ conf.d]# curl -s https://sense-eat.amebob.cn/local-dish/BJ | jq
{
  "code": 200,
  "message": "操作成功",
  "data": [
    {
      "id": 1,
      "nameCn": "北京烤鸭",
      "nameEn": "Peking Roast Duck",
      "descriptionCn": "选用优质填鸭,经挂炉明火烤制,皮酥脆、肉嫩香,配荷叶饼、甜面酱、葱丝、黄瓜条同食。",
      "descriptionEn": "Made with specially fattened ducks roasted in a closed oven, featuring crisp skin and tender meat, served with thin pancakes, sweet bean sauce, scallions, and cucumber.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/72add3b313724dbebdd4043ce6a1d603.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 2,
      "nameCn": "炸酱面",
      "nameEn": "Zhajiangmian (Noodles with Fried Soybean Sauce)",
      "descriptionCn": "手擀面配黄酱与肉末慢炒制成的浓香炸酱,辅以黄瓜、萝卜、豆芽等‘面码’,咸鲜醇厚,老北京家常主食。",
      "descriptionEn": "Hand-rolled noodles topped with savory fried sauce made from fermented soybean paste and minced pork, served with fresh vegetable garnishes like cucumber and radish.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/65a947df51f94d619037c634a7736990.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 3,
      "nameCn": "涮羊肉",
      "nameEn": "Hot Pot Mutton Slices",
      "descriptionCn": "铜锅炭火清汤涮鲜切羊后腿肉,蘸麻酱、腐乳、韭菜花等调制的秘制蘸料,鲜嫩暖胃,冬日经典。",
      "descriptionEn": "Thin-sliced mutton cooked in boiling clear broth over charcoal-fired copper pot, dipped in sesame paste-based sauce—rich, warming, and quintessentially Beijing winter fare.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/21196593dfa64e74b65c73bc3845e6be.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 4,
      "nameCn": "豆汁儿",
      "nameEn": "Douchi Fermented Mung Bean Broth",
      "descriptionCn": "绿豆发酵制成的微酸灰绿色液体,气味独特,配焦圈、辣咸菜食用,是考验地道老北京味觉的标志性小吃。",
      "descriptionEn": "A pungent, sour fermented mung bean soup with a distinctive aroma; traditionally paired with crispy deep-fried dough rings and pickled vegetables.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/667aaa8ee85e405ca8e82cfee046de4c.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 5,
      "nameCn": "焦圈",
      "nameEn": "Jiaoquan (Deep-Fried Dough Rings)",
      "descriptionCn": "用面粉、明矾、碱面制成环状面坯,油炸至金黄酥脆,常与豆汁儿搭配,口感酥松微咸。",
      "descriptionEn": "Crispy golden ring-shaped dough fritters made with flour, alum, and alkali, often eaten with douchi—light, airy, and slightly salty.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/c4595b0138284af5a4363ce1a51b3a77.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 6,
      "nameCn": "驴打滚",
      "nameEn": "Lüdagun (Glutinous Rice Rolls with Soybean Powder)",
      "descriptionCn": "糯米卷裹豆沙馅,外滚黄豆粉,软糯香甜不粘牙,形似驴在沙土打滚而得名。",
      "descriptionEn": "Soft glutinous rice rolls filled with sweet red bean paste and coated in roasted soybean powder—named for its resemblance to a donkey rolling in dust.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/bf490519a4764b658a1e59afabb8e79e.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 7,
      "nameCn": "艾窝窝",
      "nameEn": "Aiwowo (Steamed Glutinous Rice Balls)",
      "descriptionCn": "糯米蒸熟捣制为皮,包入山楂、核桃、芝麻、白糖等馅料,洁白软糯,清凉香甜,春日节令点心。",
      "descriptionEn": "White, soft steamed glutinous rice balls filled with sweet fillings like hawthorn, walnuts, sesame, and sugar—traditionally enjoyed in spring.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/4d4a046826734c7dbe26de592f21035a.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 8,
      "nameCn": "门钉肉饼",
      "nameEn": "Mending Roubing (Pan-Fried Spiced Meat Patties)",
      "descriptionCn": "直径约5厘米圆饼,厚实多汁,牛肉大葱馅煎至两面金黄微焦,形似城门铜钉,外酥里嫩。",
      "descriptionEn": "Thick, pan-fried beef-and-scallion patties with juicy filling, golden-brown crust, named for their resemblance to ancient city gate rivets.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/238202fc20a94857ace0e76d2cb476ae.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 9,
      "nameCn": "炒肝",
      "nameEn": "Chao Gan (Stewed Pork Liver and Intestines)",
      "descriptionCn": "猪肝与肥肠切丁,以淀粉勾芡熬成浓稠卤汁,蒜香浓郁,配烧饼食用,旧时早点代表。",
      "descriptionEn": "A thick, savory stew of pork liver and intestines, flavored with garlic and starch-thickened broth—served with sesame-crusted buns as classic breakfast.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/5cbd38ba1ea843028153c315188b0aad.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 10,
      "nameCn": "卤煮火烧",
      "nameEn": "Lu Zhu Huo Shao (Braised Offal with Deep-Fried Dough)",
      "descriptionCn": "猪肠、肺头、豆腐块等卤炖入味,配油炸面饼(火烧)同煮,浓香醇厚,市井烟火气十足。",
      "descriptionEn": "Braised pork offal (intestines, lungs) and tofu simmered in aromatic soy-based broth, served with deep-fried wheat cakes—robust, hearty, and deeply local.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/6182dfcd8b604aca9ee43ad1a4860080.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 11,
      "nameCn": "糖葫芦",
      "nameEn": "Tanghulu (Candied Hawthorn on a Stick)",
      "descriptionCn": "山楂串竹签裹冰糖浆,凝结成琥珀脆壳,酸甜开胃,冬日街头最具辨识度的传统零食。",
      "descriptionEn": "Hawthorn berries skewered and coated in hardened sugar syrup—tangy, sweet, crunchy, and iconic street snack during cold months.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/0434f01751d5495aa7ba2ccf63d7a69e.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 12,
      "nameCn": "豌豆黄",
      "nameEn": "Wandouhuang (Yellow Pea Cake)",
      "descriptionCn": "去皮豌豆煮烂过筛,加糖熬制凝冻成型,色泽嫩黄,细腻清凉,宫廷传入的夏季消暑甜品。",
      "descriptionEn": "A delicate, jellied dessert made from strained yellow peas, sugar, and agar—pale yellow, smooth, refreshing, and originally imperial cuisine.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/d8d7d01b70214da687b89c4898b9b25a.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 13,
      "nameCn": "奶酪",
      "nameEn": "Nailuo (Traditional Beijing Milk Curd)",
      "descriptionCn": "牛奶加米酒酿制凝结而成的柔滑奶冻,微酸清甜,入口即化,老北京传统乳制甜点。",
      "descriptionEn": "A silky, mildly tangy milk curd set with fermented rice wine—creamy, delicate, and a centuries-old Beijing dairy delicacy.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/1fb4c3551630400d897dff3108abbe76.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 14,
      "nameCn": "爆肚",
      "nameEn": "Bao Du (Quick-Boiled Tripe)",
      "descriptionCn": "鲜牛/羊百叶或肚仁沸水焯烫10–20秒,脆嫩弹牙,佐麻酱、醋、辣椒油等蘸食,讲究火候。",
      "descriptionEn": "Sliced beef or lamb tripe blanched in boiling water for seconds—crisp, tender, and served with sesame sauce, vinegar, and chili oil.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/7f12b2ca262a405d9663413bab9f2b29.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 15,
      "nameCn": "咸奶油蛋糕",
      "nameEn": "Xian Nai You Cake (Beijing-Style Salted Cream Cake)",
      "descriptionCn": "融合京味创新的网红甜点,海盐与奶油交融,口感轻盈微咸回甘,体现当代北京烘焙特色。",
      "descriptionEn": "A modern Beijing fusion dessert blending sea salt and rich cream in a fluffy sponge—subtly savory-sweet, emblematic of contemporary local pastry trends.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/e8049cf682f1499c82b73cbf5b60c245.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 16,
      "nameCn": "三鲜烧卖",
      "nameEn": "Sanxian Shaomai (Steamed Dumplings with Three Delicacies)",
      "descriptionCn": "薄皮烧卖内包虾仁、猪肉、海参丁,顶部蓬松如石榴,鲜香多汁,老北京茶馆经典点心。",
      "descriptionEn": "Open-topped steamed dumplings with a delicate wrapper, filled with shrimp, pork, and sea cucumber—juicy, aromatic, and a teahouse staple.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/7473e24ae14c468e88411444f11fb4e9.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 17,
      "nameCn": "糟熘鱼片",
      "nameEn": "Zaoliu Yu Pian (Fish Fillets in Fermented Rice Sauce)",
      "descriptionCn": "鳜鱼或鲈鱼片滑炒,以绍兴酒糟、高汤、蛋清调汁,色泽乳白,酒香清雅,咸鲜微甜。",
      "descriptionEn": "Tender fish fillets stir-fried in a fragrant, creamy sauce made from fermented rice lees, stock, and egg white—savory, subtly sweet, and delicately boozy.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/630361c26c164de1b9670ce292cef4e7.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 18,
      "nameCn": "它似蜜",
      "nameEn": "Ta Si Mi (Sweet-Sauced Lamb)",
      "descriptionCn": "清真名菜,羊里脊切片滑炒,以蜂蜜、桂花、糖等熬成琥珀色浓汁,甜香软嫩,形似蜜糖。",
      "descriptionEn": "A Hui Muslim specialty: tender lamb strips stir-fried in glossy, amber-colored sauce of honey, osmanthus, and sugar—sweet, fragrant, and melt-in-mouth.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/a61b9e07e68441a19806864173e7363b.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 19,
      "nameCn": "芥末墩儿",
      "nameEn": "Jie Mo Dun Er (Preserved Chinese Cabbage with Mustard)",
      "descriptionCn": "大白菜心腌渍后码叠,淋黄芥末与米醋调汁,冷藏入味,辛香冲鼻,解腻爽口,冬季家常小菜。",
      "descriptionEn": "Layered Napa cabbage hearts preserved with mustard and rice vinegar—pungent, refreshing, and a traditional winter side dish to cut richness.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/512fa7f78ac94cbd897013e38c618270.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    },
    {
      "id": 20,
      "nameCn": "攒丝汤",
      "nameEn": "Cuansi Tang (Shredded Vegetable and Egg Drop Soup)",
      "descriptionCn": "清汤中飘浮细如发丝的豆腐、胡萝卜、蛋皮丝,清淡鲜美,体现京菜‘清汤见底’的烹饪哲学。",
      "descriptionEn": "A light, crystal-clear broth with ultra-thin shreds of tofu, carrot, and egg—elegant, delicate, and embodying Beijing cuisine’s emphasis on purity and clarity.",
      "imageUrl": "https://springboot-demo1-bob.oss-cn-beijing.aliyuncs.com/dishes/BJ/f22576395453460586720275327d6f31.png",
      "provinceCode": null,
      "provinceNameCn": "北京市",
      "provinceNameEn": "Beijing",
      "source": null
    }
  ],
  "timestamp": 1773547871434
}