HTTPS正常工作,但HTTP网址显示NGINX欢迎页面且不重定向

我最近在一台 Ubuntu Focal Fossa VPS 上安装了 Discourse,运行效果很好,但在处理一些 URL 特性时遇到了困难。

以下是我在 Safari 中观察到的现象(我已向域名注册商设置了 @、WWW 和 * 的 A 记录):

example.com [显示“欢迎使用 nginx!”页面] 失败
www.example.com [重定向到 https://example.com] 成功
http://example.com [显示“欢迎使用 nginx!”页面] 失败
http://www.example.com [重定向到 https://example.com 并正常工作] 成功
https://example.com [按预期工作,未重定向] 成功
https://www.example.com [报错“此连接不是私密连接”] 失败

我希望将安装部署在根域名(apex)上,因此在设置时输入了 example.com

非常感谢您的任何建议!

你在 Discourse 服务器上运行了外部的 nginx 吗?为什么?

Discourse 自带了一个预配置的 nginx,只要你让它监听 80 和 443 端口,它就能处理这些请求。

@Falco,感谢您的回复。据我所知,没有。VPS 提供商提供的镜像仅名为"Focal Fossa Clean OS",我推测这意味着它不包含任何第三方工具。

这是直接使用发布的"30 分钟”安装流程进行的开箱即用部署。

sudo su

wget -qO- https://get.docker.com/ | sh

git clone https://github.com/discourse/discourse_docker.git /var/discourse

cd /var/discourse

./discourse-setup

哦,那确实是非常奇怪的行为!

你能在这里分享一下你的 app.yml 吗(请移除密码等敏感数据)。该文件位于 /var/discourse/containers/app.yml

另外,请提供 docker ps -a 的输出结果。

没问题,谢谢。我的域名使用奇怪的顶级域名(TLD)会导致任何问题吗?(它是 .community)。

## 这是 Discourse Docker 容器的全功能独立模板
##
## 修改此文件后,您必须重新构建
## /var/discourse/launcher rebuild app
##
## 编辑时请*非常*小心!
## YAML 文件对空白字符或对齐中的错误超级敏感!
## 访问 http://www.yamllint.com/ 根据需要验证此文件

templates:
  - "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  - "templates/web.ratelimited.template.yml"
## 如果您想添加 Lets Encrypt (https),请取消注释这两行
  - "templates/web.ssl.template.yml"
  - "templates/web.letsencrypt.ssl.template.yml"

## 此容器应暴露哪些 TCP/IP 端口?
## 如果您希望 Discourse 与另一个 Web 服务器(如 Apache 或 nginx)共享端口,
## 请参阅 https://meta.discourse.org/t/17247 了解详情
expose:
  - "80:80"   # http
  - "443:443" # https

params:
  db_default_text_search_config: "pg_catalog.english"

  ## 将 db_shared_buffers 设置为总内存的最大 25%。
  ## 将根据检测到的 RAM 由 bootstrap 自动设置,或者您可以覆盖
  db_shared_buffers: "768MB"

  ## 可以提高排序性能,但会增加每个连接的内存使用量
  #db_work_mem: "40MB"

  ## 此容器应使用哪个 Git 修订版?(默认:tests-passed)
  #version: tests-passed

env:
  LANG: en_US.UTF-8
  # DISCOURSE_DEFAULT_LOCALE: en

  ## 支持多少个并发 Web 请求?取决于内存和 CPU 核心数。
  ## 将根据检测到的 CPU 由 bootstrap 自动设置,或者您可以覆盖
  UNICORN_WORKERS: 8

  ## TODO: 此 Discourse 实例将响应的域名
  ## 必需。Discourse 无法在纯 IP 地址上工作。
  DISCOURSE_HOSTNAME: example.com

  ## 如果您希望容器以与上面指定的相同的主机名(-h 选项)启动,请取消注释
  ## (默认值为 "$hostname-$config")
  #DOCKER_USE_HOSTNAME: true

  ## TODO: 初始注册时将设为管理员和开发人员的逗号分隔电子邮件列表
  ## 示例 'user1@example.com,user2@example.com'
  DISCOURSE_DEVELOPER_EMAILS: 'email@gmail.com'

  ## TODO: 用于验证新账户和发送通知的 SMTP 邮件服务器
  # 需要 SMTP 地址、用户名和密码
  # 警告:SMTP 密码中的字符 '#' 可能会引起问题!
  DISCOURSE_SMTP_ADDRESS: smtp.postmarkapp.com
  DISCOURSE_SMTP_PORT: 587
  DISCOURSE_SMTP_USER_NAME: username
  DISCOURSE_SMTP_PASSWORD: "password"
  #DISCOURSE_SMTP_ENABLE_START_TLS: true           # (可选,默认为 true)
  #DISCOURSE_SMTP_DOMAIN: discourse.example.com    # (某些提供商必需)

  ## 如果您添加了 Lets Encrypt 模板,请取消注释以下行以获取免费 SSL 证书
  LETSENCRYPT_ACCOUNT_EMAIL: email@gmail.com


  ## 此 Discourse 实例的 http 或 https CDN 地址(配置为拉取)
  ## 请参阅 https://meta.discourse.org/t/14857 了解详情
  #DISCOURSE_CDN_URL: https://discourse-cdn.example.com

  ## Maxmind 地理位置 IP 地址查找的许可证密钥
  ## 请参阅 https://meta.discourse.org/t/-/137387/23 了解详情
  #DISCOURSE_MAXMIND_LICENSE_KEY: 1234567890123456

## Docker 容器是无状态的;所有数据都存储在 /shared 中
volumes:
  - volume:
      host: /var/discourse/shared/standalone
      guest: /shared
  - volume:
      host: /var/discourse/shared/standalone/log/var-log
      guest: /var/log

## 插件放在这里
## 请参阅 https://meta.discourse.org/t/19157 了解详情
hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - git clone https://github.com/discourse/docker_manager.git

## 构建后运行的任何自定义命令
run:
  - exec: echo "开始自定义命令"
  ## 如果您想为首次注册设置“发件人”电子邮件地址,请取消注释并更改:
  ## 收到首次注册邮件后,重新注释该行。它只需要运行一次。
  #- exec: rails r "SiteSetting.notification_email='info@unconfigured.discourse.org'"
  - exec: echo "自定义命令结束"

以及

CONTAINER ID   IMAGE                 COMMAND        CREATED          STATUS          PORTS                                      NAMES
465fbf1c3fb8   local_discourse/app   "/sbin/boot"   47 分钟前        运行中 46 分钟    0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   app

也许你提供商提供的这个发行版预装了 Nginx?

curl -I localhost 会输出什么?

HTTP/1.1 301 永久移动
服务器 : nginx/1.18.0
日期 : 2021 年 1 月 25 日 周一 20:18:00 GMT
内容类型 : text/html
内容长度 : 169
连接 : keep-alive
位置 : https://example.com

这是否意味着它已经安装了?(抱歉,我是这方面的新手)

这正是它正常运行时的预期响应。那么,http://example.com 是否仍然显示 Nginx 欢迎页面?

https://example.com 运行正常,但 http://example.com(HTTP)却跳转到了 Welcome to nginx! 页面::sob:

您能分享实际的域名吗?

所以,这完全没给我显示 nginx 的欢迎页面。

你能在另一个设备(比如你的手机)上试试吗?

curl -I example.com -L
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0
Date: Mon, 25 Jan 2021 20:35:21 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://example.com/

HTTP/2 200 
server: nginx
date: Mon, 25 Jan 2021 20:35:22 GMT
content-type: text/html; charset=utf-8
vary: Accept-Encoding
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-download-options: noopen
x-permitted-cross-domain-policies: none
referrer-policy: strict-origin-when-cross-origin
x-discourse-route: finish_installation/index
cache-control: no-cache, no-store
content-security-policy: base-uri 'none'; object-src 'none'; script-src https://example.com/logs/ https://example.com/sidekiq/ https://example.com/mini-profiler-resources/ https://example.com/assets/ https://example.com/brotli_asset/ https://example.com/extra-locales/ https://example.com/highlight-js/ https://example.com/javascripts/ https://example.com/plugins/ https://example.com/theme-javascripts/ https://example.com/svg-sprite/; worker-src 'self' https://example.com/assets/ https://example.com/brotli_asset/ https://example.com/javascripts/ https://example.com/plugins/
x-request-id: 8755d4fa-387f-4509-8709-b6075f274d09
x-runtime: 0.026020
strict-transport-security: max-age=31536000

好吧,我显然是个傻瓜。我在手机上通过 4G 网络尝试(以检查本地 DNS),所有那些 URL 都正常工作。重新连接到 Wi-Fi 后,一切也都正常。于是,我清除了桌面版 Safari 的缓存,结果——所有问题都解决了。

这一定是因为我最初测试主机时留下的缓存,之后一直没有刷新。非常感谢您帮忙排查问题,也抱歉占用了您的时间。

顺便说一句,我今天在 Ubuntu 22.04 上遇到了同样的问题。清除 Safari 缓存并刷新也解决了这个问题。