使用 NGINX 创建反向代理。我错过了什么?

Creating a reverse proxy with NGINX. What am I missing?

帮助调试

我一直在尝试用 NGINX 创建一个反向代理。现在我只是想让它重定向我本地网络上的流量。我想我很接近,但我被困住了。任何建议表示赞赏!

预期的行为是请求 http://api.dev.tagnoo.com 将流量路由到一个容器,而 http://app.dev.tagnoo.com 将流量路由到另一个容器。

实际情况是,尽管我的容器 运行 和 Nginx 似乎在工作,但我无法访问任何内容。我不知道如何调试它。

重现我的痛苦

我使用以下命令启动容器:

docker-compose pull --include-deps $@

docker-compose up -d --remove-orphans --build $@

我的 docker-compose.yaml 文件看起来像这样

services:
  lb:
    image: nginx:1.19.7-alpine
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./src/nginx.conf:/etc/nginx/nginx.conf
      - ./src/fullchain.pem:/etc/ssl/private/fullchain.pem
      - ./src/privkey.pem:/etc/ssl/private/privkey.pem
    networks:
      default:
        aliases:
          - api.dev.tagnoo.com
          - app.dev.tagnoo.com
          - dev.tagnoo.com

  postgres:
    image: postgres:13.3-alpine
    environment:
      POSTGRES_PASSWORD: postgres
    ports:
      - 5432:5432
    volumes:
      - pg-data:/var/lib/postgresql/data
  
  api: &api
    image: 410ventures/tagnoo-api:latest
    environment: &api_environment
      TEST_VAR: 'test123'
      VERSION: development
      WATCH: 1
  
  api-test:
    <<: *api
    command: ["true"]
    environment:
      <<: *api_environment
      TAGNOO_API_URL: http://localhost
      POSTGRES_URL: pg://postgres:postgres@postgres/tagnooTest
      REDIS_KEY_PREFIX: 'api-test:'

  api-app-test:
    <<: *api
    command: ["true"]
    environment:
      <<: *api_environment
      TAGNOO_API_URL: http://api-app-test
      POSTGRES_URL: pg://postgres:postgres@postgres/tagnooAppTest
      WATCH: 1

  app: &app
    image: 410ventures/tagnoo-app:latest
    environment: &app_environment
      VERSION: development
      WATCH: 1
  
  app-build:
    <<: *app
    command: ["true"]

  app-livereload:
    <<: *app
    command: ["true"]

  app-test:
    <<: *app
    command: ["true"]
    environment:
      <<: *app_environment
      TAGNOO_APP_URL: http://localhost
      TAGNOO_API_URL: http://api-app-test

volumes:
  pg-data:

这按预期工作: container info

我的 nginx.conf 文件看起来像这样

events {}

http {   server_tokens off;

  map $http_upgrade $connection_upgrade {
    '' close;
    default upgrade;   }

  proxy_http_version 1.1;   proxy_set_header Connection $connection_upgrade;   proxy_set_header Host $host;   proxy_set_header Upgrade $http_upgrade;   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   proxy_set_header X-Forwarded-Proto $scheme;   proxy_set_header X-Real-IP $remote_addr;

  ssl_certificate /etc/ssl/private/fullchain.pem;   ssl_certificate_key /etc/ssl/private/privkey.pem;

  proxy_read_timeout 24h;

  # proxy_pass directives are passed hosts via a $proxy_pass_host variable to   # allow nginx to start up before the hosts are actually available. Putting the   # hosts directly in the proxy_pass directive will fail start up unless all   # hosts are available. A resolver is required to use variables in proxy_pass   # directives, so use the docker internal DNS IP here.   resolver 127.0.0.11;

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

  server {
    listen 443 ssl http2;
    server_name app.dev.tagnoo.com;

    location /livereload {
      set $proxy_pass_host app-livereload:35729;
      proxy_pass http://$proxy_pass_host;
    }

    location / {
      set $proxy_pass_host app;
      proxy_pass http://$proxy_pass_host;
    }   }

  server {
    listen 443 ssl http2;
    server_name api.dev.tagnoo.com;

    location / {
      set $proxy_pass_host api;
      proxy_pass http://$proxy_pass_host;
    }   } }

我的根目录中有 privkey.pemfullchain.pem 文件。我只是使用 openssl 对它们进行了自签名,但我认为如果我忽略 ssl,它仍然适用于我的本地网络。

我试过的

我试过通过以下方式访问容器(无济于事):

这是我收到的回复日志(排名不分先后) docker compose logs for lb

我还没有为我的域设置任何 DNS 记录,tagnoo.com,但我认为这不重要,因为目前这只是一个本地环境。但我不确定这是不是真的。

目前我找不到有关调试 NGINX 的更多信息。

总结

我主要担心我的 NGINX 配置文件没有按预期执行。我也不确定我的 docker-compose 文件是否为反向代理正确设置。

我的容器是 运行,但它们的反向代理不知何故损坏了。每当我尝试访问时,请求都会失败并显示 302301 getaddrinfo ENOTFOUND api.dev.tagnoo.com

以下是我对此事的一些疑问:

任何 advice/tips 将不胜感激!

问题是我还没有设置 A 和 AAAA 记录来将 *.dev 子域解析到本地主机。我添加了这些并为主机创建了一个有效(非自签名)证书。