首页 > 解决方案 > 将 Traefik 与 Nginx 一起使用时出现 404 错误

问题描述

我的设置是在 Rancher/Docker 上,我正在尝试通过使用 Traefik 让我的 InvoiceNinja 堆栈在线。

我目前有另外两个可以公开的容器,但之前不必尝试通过 nginx 容器进行连接。

访问https://invoiceninja.example.com时,我只是收到 404 错误。我可以使用主机和容器的 IP 和端口在本地访问它。

码头工人-compose.yml:

version: '2'
services:
  app:
    image: invoiceninja/invoiceninja:latest
    dns:
      - 1.1.1.1
      - 1.0.0.1
    environment:
      APP_ENV: production
      APP_DEBUG: false
      APP_URL: https://${TRAEFIK_HOST}
      APP_CIPHER: AES-256-CBC
      APP_KEY: ${APP_KEY}
      DB_STRICT: false
      DB_HOST: mysql
      DB_DATABASE: ninja_db
      DB_USERNAME: ninja
      DB_PASSWORD: ${DB_USER_PASS}
      {{- if (.Values.MOBILE_APP_SECRET)}}
      API_SECRET: ${MOBILE_APP_SECRET}
      {{- end}}
      {{- if (.Values.GMAPS_KEY)}}
      GOOGLE_MAPS_API_KEY: ${GMAPS_KEY}
      {{- end}}
    labels:
      io.rancher.container.pull_image: always
      {{- if (.Values.HOST_LABEL)}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
      io.rancher.sidekicks: cron, web
    links:
      - mysql
    restart: on-failure
    volumes:
      - Logo:/var/www/app/public/logo
      - Storage:/var/www/app/storage
  cron:
    image: invoiceninja/invoiceninja:latest
    dns:
      - 1.1.1.1
      - 1.0.0.1
    entrypoint: |
      bash -c 'bash -s <<EOF
      trap "break;exit" SIGHUP SIGINT SIGTERM
      sleep 300s
      while /bin/true; do
        ./artisan ninja:send-invoices
        ./artisan ninja:send-reminders
        sleep 1h
      done
      EOF'
    environment:
      APP_ENV: production
      APP_DEBUG: false
      APP_URL: https://${TRAEFIK_HOST}
      APP_CIPHER: AES-256-CBC
      APP_KEY: ${APP_KEY}
      DB_STRICT: false
      DB_HOST: mysql
      DB_DATABASE: ninja_db
      DB_USERNAME: ninja
      DB_PASSWORD: ${DB_USER_PASS}
      {{- if .Values.MOBILE_APP_SECRET}}
      API_SECRET: ${MOBILE_APP_SECRET}
      {{- end}}
      {{- if .Values.GMAPS_KEY}}
      GOOGLE_MAPS_API_KEY: ${GMAPS_KEY}
      {{- end}}
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
    links:
      - mysql
    restart: on-failure
    volumes_from:
      - app
  mysql:
    image: mysql:5
    dns:
      - 1.1.1.1
      - 1.0.0.1
    environment:
      MYSQL_DATABASE: ninja_db
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
      MYSQL_USER: ninja
      MYSQL_PASSWORD: ${DB_USER_PASS}
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
    restart: on-failure
    volumes:
      - Database:/var/lib/mysql
  web:
    image: nginx
    dns:
      - 1.1.1.1
      - 1.0.0.1
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
      {{- if .Values.TRAEFIK_HOST}}
      traefik.enable: true
      traefik.frontend.rule: Host:${TRAEFIK_HOST}
      traefik.port: "80"
      traefik.frontend.entryPoints: https
      traefik.frontend.passHostHeader: true
      {{- else}}
      traefik.enable: false
      {{- end}}
    links:
      - app
    networks:
      - public-proxy
    ports:
      - "${WEB_PORT}:80"
    restart: on-failure
    volumes:
      - /RancherConfig/${DATA_DIR}/nginx.conf:/etc/nginx/nginx.conf:ro
    volumes_from:
      - app
networks:
  public-proxy:
    external: true

nginx.conf:

user www-data;

events {
  worker_connections 768;
}

http {
    upstream backend {
        server app:9000;
    }
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    gzip on;
    gzip_disable "msie6";

    server {
        listen      80 default;
        server_name invoiceninja.example.com;

        root /var/www/app/public;

        index index.php;

        charset utf-8;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        sendfile off;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass backend;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors off;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
        }

        location ~ /\.ht {
            deny all;
        }
    }
}

我尝试(并且仍然)使用“traefik.frontend.passHostHeader:true”标签作为类似问题,但似乎没有什么不同。

任何帮助是极大的赞赏!谢谢!

标签: dockernginxtraefik

解决方案


正常工作的固定 docker-compose.yml:

version: '2'
services:
  invoiceninja:
    image: invoiceninja/invoiceninja:latest
    dns:
      - 1.1.1.1
      - 1.0.0.1
    labels:
      io.rancher.container.pull_image: always
      {{- if (.Values.HOST_LABEL)}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
    links:
      - mysql
    restart: on-failure
    volumes:
      - /etc/localtime:/etc/localtime:ro # Syncronize time of container with the host system
      - /etc/timezone:/etc/timezone:ro # Syncronize timezone of container with the host system
      - /RancherCattle/${DATA_DIR}/Configuration/.env:/var/www/app/.env
      - /RancherCattle/${DATA_DIR}/Logo:/var/www/app/public/logo
      - /RancherCattle/${DATA_DIR}/Storage:/var/www/app/storage
  cron:
    image: invoiceninja/invoiceninja:latest
    dns:
      - 1.1.1.1
      - 1.0.0.1
    entrypoint: |
      bash -c 'bash -s <<EOF
      trap "break;exit" SIGHUP SIGINT SIGTERM
      sleep 300s
      while /bin/true; do
        ./artisan ninja:send-invoices
        ./artisan ninja:send-reminders
        sleep 1d
      done
      EOF'
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
    links:
      - mysql
    restart: on-failure
    volumes_from:
      - invoiceninja
  mysql:
    image: mysql:5
    dns:
      - 1.1.1.1
      - 1.0.0.1
    environment:
      MYSQL_DATABASE: ninja_db
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
      MYSQL_USER: ninja_user
      MYSQL_PASSWORD: ${DB_USER_PASS}
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
      traefik.enable: false
    networks:
      - db-admin
    restart: on-failure
    volumes:
      - /etc/localtime:/etc/localtime:ro # Syncronize time of container with the host system
      - /etc/timezone:/etc/timezone:ro # Syncronize timezone of container with the host system
      - /RancherCattle/${DATA_DIR}/Database:/var/lib/mysql
  nginx:
    image: nginx
    dns:
      - 1.1.1.1
      - 1.0.0.1
    labels:
      io.rancher.container.pull_image: always
      {{- if .Values.HOST_LABEL}}
      io.rancher.scheduler.affinity:host_label: ${HOST_LABEL}
      {{- end}}
      {{- if .Values.TRAEFIK_HOST}}
      traefik.enable: true
      traefik.frontend.rule: Host:${TRAEFIK_HOST}
      traefik.frontend.entryPoints: http,https
      traefik.frontend.headers.forceSTSHeader: true
      traefik.frontend.headers.SSLRedirect: true
      traefik.frontend.headers.STSPreload: true
      traefik.frontend.headers.STSSeconds: 15552000
      traefik.port: "80"
      {{- else}}
      traefik.enable: false
      {{- end}}
      io.rancher.sidekicks: invoiceninja,cron
    links:
      - invoiceninja
    networks:
      - public-proxy
    ports:
      - "${WEB_PORT}:80"
    restart: on-failure
    volumes:
      - /RancherCattle/${DATA_DIR}/Configuration/nginx.conf:/etc/nginx/nginx.conf:ro
    volumes_from:
      - invoiceninja

networks:
  db-admin:
    external: true
  public-proxy:
    external: true

nginx.conf:

user www-data;

events {
  worker_connections 768;
}

http {
    upstream backend {
        server invoiceninja:9000;
    }
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    gzip on;
    gzip_disable "msie6";

    server {
        listen      80 default;
        server_name _;

        root /var/www/app/public;

        index index.php;

        charset utf-8;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        sendfile off;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass backend;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors off;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
            fastcgi_param HTTPS 1;
        }

        location ~ /\.ht {
            deny all;
        }
    }
}

我不久前又回到了这个问题,所以我可能不完全记得所有问题是什么。

据我所知:

  • 容器命名不一致,使用了错误的主机名。
  • 在 docker-compose.yml 文件中使用环境变量会导致一些问题,以后无法更改它们。删除容器时,我经常没有完全删除持久数据卷,所以这也导致了问题,以前设置的环境变量,并且在新的部署中不容易更改。

推荐阅读