首页 > 解决方案 > Nginx 找不到具体位置

问题描述

我正在尝试通过使用运行 django + gunicorn + nginx docker-compose
django 和 gunicorn 现在可以正常工作并响应请求,但是,当我尝试访问 nginx(端口 80)中的项目时,它找不到我的项目位置。
以下是 nginxDockerfilenginx.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 端口并响应请求。如果我去127.0.0.1,我会看到 nginx 默认页面。但是如果我去127.0.0.1/backend/,nginx 会显示 404 页面。

在 docker-compose 日志中,它向我显示以下行:

[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

标签: dockernginxdocker-composedocker-networking

解决方案


我不是很熟悉gunicorn,但你的配置看起来基本正确。

什么nginx时候proxy_pass,path /backend也被proxy_pass添加到您的backend-service 中。
我假设,您gunicorn不知道如何处理/backend-path?

如果您将所有内容重写为/

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>

推荐阅读