Nginx не могу найти указанное c местоположение - PullRequest
0 голосов
/ 03 апреля 2020

Я пытаюсь запустить django + gunicorn + nginx, используя docker-compose.
django и gunicorn теперь работают правильно и отвечают на запрос, но, когда я пытаюсь получить доступ к проекту в nginx (порт 80), он не может найти местоположение моего проекта.
следующие nginx Dockerfile и nginx.conf:

FROM nginx:1.17.4-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/

и nginx.conf:

upstream back {
    # in docker-compose.yml file, (django+gunicorn) service name is `backend` and is listening on port 8000.
    server backend:8000;    
}

server {

    listen 80;
    location /backend {
        proxy_pass http://back;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

}

Теперь Gunicorn слушает на порт 8000 и ответы на запросы. если я go до 127.0.0.1, я вижу nginx страницу по умолчанию. Но если я go до 127.0.0.1/backend/, nginx покажет мне 404 страницы.

в docker -составлении журналов он показывает мне следующую строку:

[error] 6#6: *1 open() "/usr/share/nginx/html/backend" failed (2: No such file or directory), client: 192.168.176.1, server: localhost, request: "GET /backend HTTP/1.1", host: "127.0.0.1" <br>

Кажется, nginx ищет /backend в своих папках в usr/share и не делает передать запрос на порт 8000. Как я могу решить эту проблему?

ОБНОВЛЕНИЕ
это docker-compose.yml файл:

version: '3'

services:
  db:
    ...

  redis:
    ...

  backend:
    hostname: backend
    depends_on:
      - db
      - redis

    volumes:
      - ./backend/app/:/opt/

    build:
      context: .
      dockerfile: ./backend/Dockerfile

    working_dir: /opt/app/project
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
    env_file:
      - ./backend/.env

    ports:
     - 8000:8000

  nginx:
    image: nginx
    build:
      context: .
      dockerfile: ./nginx/Dockerfile
    ports:
      - 80:80
    depends_on:
      - backend

1 Ответ

0 голосов
/ 03 апреля 2020

Я не очень знаком с gunicorn, но ваша конфигурация выглядит в основном правильно.

Когда nginx делает proxy_pass, path /backend также proxy_pass ' к вашему backend -сервису.
Я предполагаю, что ваш gunicorn не знает, как обрабатывать /backend -путь?

Может быть, это поможет, если вы переписываете все на /:

nginx.conf:

upstream back {
    # in docker-compose.yml file, (django+gunicorn) service name is `backend` and is listening on port 8000.
    server backend:8000;    
}

server {

    listen 80;
    location /backend {
        # # # this rewrites all requests to '/backend' to '/' ...
        # # # ... before passing them to your backend-service
        rewrite /backend.* / break;
        # # #
        proxy_pass http://back;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

}

Я настроил ваш docker-compose.yml для удобства тестирования:

docker-compose.yml:

version: '3'

services:
  backend:
    image: nginx:alpine
    hostname: backend
    expose:
     - 80

  nginx:
    image: nginx:alpine
    ports:
      - 8080:80
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf

default.conf:

upstream back {
    # in docker-compose.yml file, (django+gunicorn) service name is `backend` and is listening on port 8000.
    server backend:80;
}

server {

    listen 80;
    location /backend {
        rewrite /backend.* / break;
        proxy_pass http://back;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

}

docker-compose up:

╰─❯ docker-compose up
Starting test_backend_1 ... done
Starting test_nginx_1   ... done
Attaching to test_backend_1, test_nginx_1
backend_1  | 172.21.0.3 - - [03/Apr/2020:11:53:52 +0000] "GET / HTTP/1.0" 200 612 "-" "curl/7.64.1" "172.21.0.1"
nginx_1    | 172.21.0.1 - - [03/Apr/2020:11:53:52 +0000] "GET /backend HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"

curl:

╰─❯ curl localhost:8080/backend

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
...