nginx docker compose not up and running - nginx

I have below docker-compose for nginx service: however I don't see up and running for any service request to forward, is there something I'm doing wrong? any suggestion will be really helpful.
container_name: nginx
image: nginx
ports:
- 80:80
- 443:443
entrypoint: /bin/sh -c
command: >
cat << EOF > /etc/nginx/conf.d/myproxy.conf
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/my.com.cert;
ssl_certificate_key /etc/nginx/my.com.key;
server_name my.app.com;
location / {
proxy_pass http://service-name:443;
}
}
EOF
nginx -s reload;
nginx -g 'daemon off;'
volumes:
- ./nginx/my.com.key:/etc/nginx/my.com.key
- ./nginx/my.com.cert:/etc/nginx/my.com.cert
networks:
nginx should up and running, but I don't see any container running for Nginx.
Solved:
I solved the issue by creating a custom conf file with all the server details and mapped that custom conf file to override existing conf and also, the deleted entrypoint and command block.

First of all, get rid of entire command and also entrypoint section! :).
write your nginx config in a file beside docker-compose.yml or where ever you want (lets name it myproxy.conf). Then, add ./myproxy.conf:/etc/nginx/conf.d/myproxy.conf to valumes section.
Nginx docker images handles entrypoint it self and there is no need to interfere!
Your compose should be something like this:
nginx:
container_name: nginx
image: nginx
ports:
- 80:80
- 443:443
volumes:
- ./myproxy.conf:/etc/nginx/conf.d/myproxy.conf
- ./nginx/my.com.key:/etc/nginx/my.com.key
- ./nginx/my.com.cert:/etc/nginx/my.com.cert
networks:

Related

Nginx proxy server with docker-compose not working with SSL certificate

I've been scratching my head trying to figure out what I'm doing wrong. I have a project running on a DigitalOcean droplet - it has 3 containers: an Nginx server and a frontend and api server. It was all working fine, but I've now tried to add an SSL cert to the Nginx server and the browser just gives back 'this site can't be reached'.
My docker-compose is as follows:
services:
clarbeast-frontend:
container_name: clarbeast-frontend
build:
context: ./frontend
dockerfile: Dockerfile
image: clarbeast-frontend
clarbeast-api:
container_name: clarbeast-api
build:
context: ./api
dockerfile: Dockerfile
image: clarbeast-api
clarbeast-nginx:
container_name: clarbeast-nginx
build:
context: ./nginx
dockerfile: Dockerfile
image: clarbeast-nginx
ports:
- "80:80"
depends_on:
- clarbeast-frontend
- clarbeast-api
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- /root/nginx_bundle_5a52b057ab94.crt:/root/nginx_bundle_5a52b057ab94.crt
- /root/example.com.key:/root/example.com.key
and my nginx.conf is as follows:
events {}
http {
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /root/nginx_bundle_5a52b057ab94.crt;
ssl_certificate_key /root/example.com.key;
location / {
proxy_pass http://clarbeast-frontend:3000;
}
location /api {
proxy_pass http://clarbeast-api:9874;
}
}
}
my docker container for nginx is up and running fine according to the logs - when I've had the path to the certs wrong, it's complained. Can anyone see what the issue might be?
I've tried double checking all the paths
I got it working - there were a few issues I had to address.
I wasn't exposing port 443 - so I needed to change the docker-compose to
ports:
- "80:80"
I also needed to add proxy_ssl_server_name on to the location blocks for the frontend and api in the nginx.conf.

FastAPI served through NGINX with gunicorn and docker compose

I have a FastAPI API that I want to serve using gunicorn, nginx and docker compose.
I manage to make the FastApi and Gunicorn work with docker compose, now I add nginx. But I cannot manage to make it work. When I do curl http://localhost:80 I get this messsage: If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
So this is my docker compose file:
version: '3.8'
services:
web:
build:
dockerfile: Dockerfile.prod
context: .
command: gunicorn main:app --bind 0.0.0.0:8000 --worker-class uvicorn.workers.UvicornWorker
expose:
- 8000
env_file:
- ./.env.prod
nginx:
build:
dockerfile: Dockerfile.prod
context: ./nginx
ports:
- 1337:80
depends_on:
- web
On this one, if I set ports to 80:80 I get an error when the image is composed: Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use, which I don't know why.
If I put [some random number]:80 (e.g. 1337:80) then the docker build works, but I get the If you see this page, the nginx web server is successfully installed but... error message state before. I think 1337 is not where nginx is listening, and that's why.
This is my nginx conf file:
upstream platic_service {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://platic_service;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
I tried to change it to listen to 8080 but does not work.
What am I doing wrong?

docker compose nginx reverse proxy mount config

I'm trying to use nginx as a reverse proxy using the below docker-compose file
version: '3'
services:
nginx:
image: nginx
volumes:
- "nginx-conf:/etc/nginx/conf.d"
ports:
- 80:80
depends_on:
- nginxtest
nginxtest:
image: nginx
volumes:
nginx-conf:
Inside ${PWD}/nginx-conf I've the default.conf file like so
http {
server {
listen 80;
location / {
proxy_pass http://nginxtext;
}
}
}
nginx container doesn't load my reverse proxy config; instead it loads default config.
It depends what you are trying to achieve, as per documentation.
Those lines are of interest in this particular case:
# Path on the host, relative to the Compose file
- ./volume_name:/some/docker/path
# Named volume
- volume_name:/some/docker/path
If you are trying to mount folder from host, to nginx configuration folder, update the volumes part to the following:
volumes:
- "~/nginx-conf:/etc/nginx/conf.d"

Properly mount Flask app at location using Nginx reverse proxy

I have a Flask app, which runs inside a Docker container and should be exposed under a specific URL: myserver.com/mylocation. I want to use another container running Nginx as a reverse proxy to achieve the routing. I am following an awesome tutorial that got me quite far already.
My Nginx-config (the relevant part) reads:
server {
server_name myserver.com;
location /mylocation {
proxy_pass http://myapp:5000;
proxy_set_header Host $host;
rewrite ^/mylocation(.*)$ $1 break;
}
}
My docker-compose.yml reads:
version: '2'
services:
nginx:
image: nginx:latest
container_name: production_nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
- 443:443
myapp:
build: .
image: app_image
container_name: app_container
expose:
- "5000"
Now, when I run this, I successfully get my applications' index.html from myserver.com/mylocation, but subsequent requests (the CSS, JS etc) are being fired at myserver.com without the location part (/mylocation), and so Nginx does not route them to the container and they 404. The references to CSS, JS and such are all relative, they do not (and should not) contain the server name and location.
How can I achieve this? Am I missing something in my NGinx config that would let the app know it should run at /mylocation?

Dockerized Nginx upstream error serving separate Docker container with Flask/uWSGI app

I am experiencing the following error with my multi-container Docker setup after running docker-compose build && docker-compose up and attempting to hit my index page:
[error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.99.1, server: localhostz, request: "GET / HTTP/1.1", upstream: "uwsgi://172.17.0.39:8000", host: "192.168.99.100"
Here is my docker-compose.yml:
web:
restart: always
build: ./web-app
expose:
- "8000"
command: /usr/local/bin/uwsgi --ini sample-uwsgi.ini
nginx:
restart: always
build: ./nginx/
ports:
- "80:80"
links:
- web:web
nginx/Dockerfile
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
ADD sample-nginx.conf /etc/nginx/conf.d/
nginx/sample-nginx.conf
upstream flask {
server web:8000;
}
server {
listen 80;
server_name localhostz;
charset utf-8;
client_max_body_size 75M;
location / {
uwsgi_pass flask;
include uwsgi_params;
}
}
web-app/Dockerfile
FROM ansible/ubuntu14.04-ansible:stable
WORKDIR /root
ADD application.py application.py
ADD requirements.txt requirements.txt
ADD sample-uwsgi.ini sample-uwsgi.ini
ADD ansible /srv/ansible
WORKDIR /srv/ansible
RUN ansible-playbook container-bootstrap.yml -c local
web-app/sample-uswgi.ini
[uwsgi]
module = application
callable = app
master = true
processes = 5
socket = web:8000
chown-socket = www-data:www-data
vacuum = true
enable-threads=True
die-on-term = true
Please do not post suggestions regarding a single container setup. I am doing as an exercise in being able to scale Docker app containers served under a single nginx container.
Secret sauce was changing the socket line in sample-uwsgi.ini to:
socket = 0.0.0.0:8000

Resources