[英]Nginx can't find specific location
I'm trying to run django + gunicorn + nginx by using docker-compose
.我正在尝试使用 docker-compose 运行 django + gunicorn + docker-compose
。
django and gunicorn now are working correctly and response to request, but, when I try to access to project within nginx (port 80), it can't find my project location. django 和 gunicorn 现在工作正常并响应请求,但是,当我尝试访问 nginx(端口 80)中的项目时,它找不到我的项目位置。
following are nginx Dockerfile
and nginx.conf
:以下是 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/
and nginx.conf
:和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;
}
}
Now, gunicorn is listening on port 8000 and responses to requestes.现在,gunicorn 正在监听 8000 端口并响应请求。 if I go to 127.0.0.1
, I see nginx default page.如果我 go 到127.0.0.1
,我看到 nginx 默认页面。 But if I go to 127.0.0.1/backend/
, nginx shows me 404 page.但如果我 go 到127.0.0.1/backend/
, nginx 显示我 404 页。
in docker-compose logs it shows me following line:在 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>
It seems, nginx are searching for /backend
in his folders in usr/share
and does not pass request to port 8000. How can I solve this problem?看来,nginx 正在他的usr/share
文件夹中搜索/backend
并且没有将请求传递到端口 8000。我该如何解决这个问题?
UPDATE更新
this is docker-compose.yml
file:这是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
I'm not very familiar with gunicorn
, but your configuration looks basically correct.我对gunicorn
不是很熟悉,但是您的配置看起来基本正确。
When nginx
does the proxy_pass
, the path
/backend
is also proxy_pass
'ed to your backend
-service.当nginx
执行proxy_pass
时, path
/backend
也是proxy_pass
到您的backend
服务的。
I assume, that your gunicorn
does not know how to handle the /backend
-path?我假设,您的gunicorn
不知道如何处理/backend
-path?
Maybe it would help, if you rewrite everything to /
:如果您将所有内容重写为/
,也许会有所帮助:
nginx.conf
: 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;
}
}
I've adapted your docker-compose.yml
for easy testing purposes:为了便于测试,我已经调整了您的docker-compose.yml
:
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
: 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
:
╰─❯ 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
:
╰─❯ 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.