docker - Nginx 找不到具体位置
问题描述
我正在尝试通过使用运行 django + gunicorn + nginx docker-compose
。
django 和 gunicorn 现在可以正常工作并响应请求,但是,当我尝试访问 nginx(端口 80)中的项目时,它找不到我的项目位置。
以下是 nginxDockerfile
和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 端口并响应请求。如果我去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
解决方案
我不是很熟悉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>
推荐阅读
- c# - 正则表达式替换为 C# 中的括号变量
- javascript - 在 html 页面加载之前运行 google 脚本
- java - 为什么 Maven 看不到 JavaFX 图形模块(而 Eclipse 有)
- mongodb - 如何使用工作副本机器同步 MongoDB 故障转移?
- php - 为什么我的输入表单在我的表格中找不到与我的搜索匹配的用户?
- java - 如何从 Java 中的 KeyStore 检索实际密钥?
- kubernetes - 是否可以仅为命名空间设置资源配额?[Kubernetes]
- ios - 无法与添加到 Window 的 UIView 交互
- r - R:如果值在n个元素后重复,则更改数据框中的行
- powershell - 我们如何识别文件是否包含标题或不使用 powershell / azure 数据工厂