Reputation: 2041
I've a backend and a frontend that run in two containers in Docker, and both are accessible from my browser with their ports:
localhost:8081 # frontend
localhost:8082 # backend
Now, I want to use an Nginx server that receives all the traffic (port 80) and redirects:
localhost -> to frontend
localhost/api -> to the backend
After trying almost everything in nginx.conf
file (nothing worked), I found in an SO Question that the file I must modify is:
/etc/nginx/sites-enabled/default
So I try to modify it, and now nginx runs. And with runs, I mean that at least when I access localhost
, nginx welcome page is shown. However, I still can't make my nginx route traffic (proxy way, not redirect).
Right now my docker-compose
file has the following look (snippet for nginx plus those two services):
nginx:
image: nginx:latest
ports:
- 80:80
volumes:
- ./Dockerfiles/nginx/default:/etc/nginx/sites-enabled/default
links:
- frontend
- backend
depends_on:
- frontend
- backend
frontend:
build: ./Dockerfiles/frontend
volumes:
- ./Dockerfiles/frontend/www/src:/usr/src/app
- ./logs/frontend/httpd:/var/logs/httpd
ports:
- "8081:3000"
links:
- backend
backend:
build: ./Dockerfiles/backend
volumes:
- ./Dockerfiles/backend/www:/var/www/html
- ./logs/backend/httpd:/var/logs/httpd
ports:
- "8082:80"
links:
- "database"
- "redis"
depends_on:
- database
- redis
My default
file as said before, for nginx config is:
server {
listen 80 default_server;
# Add index.php to the list if you are using PHP
index index.html index.htm index.php index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_pass http://frontend:8081/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api {
proxy_pass http://backend:8082/public/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
And when I try to access my /api
url for example, the error that nginx is telling my is:
nginx_1 | 2018/12/17 21:44:09 [error] 6#6: *1 open() "/usr/share/nginx/html/api" failed (2: No such file or directory), client: 172.20.0.1, server: localhost, request: "GET /api HTTP/1.1", host: "localhost"
Which seems like it's trying to retrieve a file from local filesystem (something that I can't understand, given the conf. files).
Any lead, idea or tip?
Upvotes: 0
Views: 4227
Reputation: 36763
Put your server config in: /etc/nginx/conf.d/default.conf
, not in sites-enabled.
Docs: https://hub.docker.com/_/nginx/
Also, you will face a different problem later. Change http://frontend:8081/;
to http://frontend:3000/;
. Because nginx is contacting the frontend container directly, not throughout the host.
Upvotes: 2