将Traefik与Nginx一起使用时出现404错误

时间:2018-08-29 18:45:05

标签: docker nginx traefik

我的设置是通过Rancher / Docker进行的,我正在尝试通过使用Traefik使我的InvoiceNinja堆栈联机。

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

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

docker-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”标签作为类似问题,但似乎没有什么不同。

任何帮助将不胜感激! 谢谢!

1 个答案:

答案 0 :(得分:0)

可正常运行的固定docker-compose.yml:

<div id="options">
  <input>
  <input>
  <input>
  <input>
  <input>

</div>

nginx.conf:

def func(n, k, *args):
  if n:
    for i in range(k):
      func(n - 1, k, *args, i)
  else:
    print(*args)

func(3, 10)

我不久前会回到这个话题,所以我可能不完全记得所有的问题。

据我所记得:

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