Docker:nginx-proxy 不将 GET 请求转发到 wordpress

Docker:nginx-proxy 不将 GET 请求转发到 wordpress

我正在尝试设置一台 Ubuntu 16.04 机器,以便 nginx-proxy 容器可以反向代理到 wordpress 容器。

我遇到的问题是,当尝试从外部世界访问该网站时,nginx 仅转发一些 GET 请求,即主页和网站图标的 HTML。它不会转发任何其他请求来加载页面的其他元素。

这是我的docker-compose.yml:

version: "2"
services:

 mysql:
  image: mysql:latest
  container_name: db
  restart: always
  environment:
   - MYSQL_ROOT_PASSWORD=SuperSecretMysqlPassword
  volumes:
   - /home/docker-stuff/var-lib-mysql:/var/lib/mysql

 nginx-proxy:
  image: jwilder/nginx-proxy:alpine
  container_name: nginx
  restart: always
  ports:
   - "443:443"
   - "80:80"
  volumes:
   - /var/run/docker.sock:/tmp/docker.sock:ro

 wordpress:
  image: wordpress:latest
  container_name: wp
  restart: always
  environment:
   - VIRTUAL_HOST=example.com
   - WORDPRESS_DB_HOST=mysql
   - WORDPRESS_DB_USER=wordpress
   - WORDPRESS_DB_PASSWORD=SuperSecretWordpressPassword
  volumes:
   - /home/wp/:/var/www/html/

docker-compose up这是我尝试访问该网站(在端口 80 上)时的输出:

nginx          | nginx.1    | 92.222.75.87 172.18.0.1 - - [23/Jun/2017:18:26:20 +0000] "POST /wp-cron.php?doing_wp_cron=1498242380.0386610031127929687500 HTTP/1.1" 503 213 "http://92.222.75.87/wp-cron.php?doing_wp_cron=1498242380.0386610031127929687500" "WordPress/4.8; https://92.222.75.87"
wp             | 172.18.0.3 - - [23/Jun/2017:18:26:19 +0000] "GET / HTTP/1.1" 200 17780 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | example.com 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET / HTTP/1.1" 200 17467 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/assets/images/header.jpg HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/style.css?ver=4.8 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-includes/js/jquery/jquery.js?ver=1.12.4 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/assets/js/skip-link-focus-fix.js?ver=1.0 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/assets/js/global.js?ver=1.0 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/assets/js/jquery.scrollTo.js?ver=2.1.2 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-includes/js/wp-embed.min.js?ver=4.8 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-includes/js/wp-emoji-release.min.js?ver=4.8 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/assets/images/header.jpg HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/assets/js/skip-link-focus-fix.js?ver=1.0 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/assets/js/global.js?ver=1.0 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-content/themes/twentyseventeen/assets/js/jquery.scrollTo.js?ver=2.1.2 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | 92.222.75.87 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /wp-includes/js/wp-embed.min.js?ver=4.8 HTTP/1.1" 503 213 "http://example.com/" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
wp             | 172.18.0.3 - - [23/Jun/2017:18:26:20 +0000] "GET /favicon.ico HTTP/1.1" 200 192 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
nginx          | nginx.1    | example.com 146.198.151.82 - - [23/Jun/2017:18:26:20 +0000] "GET /favicon.ico HTTP/1.1" 200 0 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"

所有图像都是从 docker hub 最新提取的。我尝试使用“jwilder/nginx-proxy:latest”而不是 alpine,结果相同...

我不太了解 nginx,所以我真的很感激这里的一些帮助......提前感谢!

答案1

经过大量的调试,我找到了这个问题的根本原因。我在这里详细说明一下,希望能对其他人有所帮助。

问题在于主页中的链接是根据主机的 IP 地址构建的,而不是主机名本身。这导致浏览器发出 GET 请求,其中“Host:”标头设置为 IP 地址而不是主机名,而 nginx 显然不知道如何处理它(然后请求由附加部分处理,其中servernginx.tmpl所有请求发出 503 响应)。

为什么 wordpress 主页的链接中有这些 IP 地址?这可能是因为我最初安装 wordpress 时直接使用 IP 地址访问服务器,而当时 DNS 尚未启动并运行。我删除了 wordpress 数据库,然后通过主机名访问服务器重新安装,然后就好了!一切都很顺利!

由于某种原因,wordpress 会保存您用于访问它的主机名并使用它来构建其 html...

相关内容