Please assist
I'm trying to run both react.js and nest.js on http://localhost:3000 with docker-compose and Nginx, however
my react.js app isn't binding correctly. When I visit the link, I only see the nginx welcome page.
This is my docker-compose.yaml
version: "3.6"
services:
database:
image: postgres:13.1-alpine
env_file:
- ./database/.env
volumes:
- "db-data:/var/lib/postgresql/data"
networks:
- challenge
ports:
- "5432:5432"
backend:
build:
context: $PWD/../../backend
dockerfile: $PWD/backend/Dockerfile
volumes:
- ./backend/.env:/app/.env
- ../../backend/src:/app/src
- storage:/app/storage
ports:
- 3000
networks:
- challenge
depends_on:
- database
env_file:
- ./backend/.env
environment:
- FORCE_COLOR=1
frontend:
build:
context: $PWD/../../web
dockerfile: $PWD/frontend/Dockerfile
ports:
- 3001
networks:
- challenge
depends_on:
- backend
env_file:
- ./frontend/.env
volumes:
- ../../web/src:/app/frontend/src
nginx:
image: nginx
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- 3000:80
depends_on:
- backend
- frontend
networks:
- challenge
volumes:
db-data:
storage:
networks:
challenge:
And this is my Dockerfile for the React
FROM node:14.18.1-alpine3.14 as build
WORKDIR /app/frontend
COPY package.json /app/frontend/
COPY yarn.lock /app/frontend/
RUN yarn install --frozen-lockfile
COPY . /app/frontend
RUN yarn run build
FROM nginx:1.21.3-alpine
COPY --from=build /app/frontend/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Also, this is my nginx.conf
events {
worker_connections 1024;
}
http {
client_max_body_size 1000M;
server {
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
listen 80;
server_name localhost;
location /api/v1 {
proxy_pass http://backend:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
Please assist as I've tried changing the ports, and exposing different ports but that doesn't seem to work.
You have two nginx servers: one inside frontend service and another one inside nginx service. Is it intended?
I suppose you need to access the nginx service provided by frontend so in that case you need to go to:
http://localhost:3001
and map 3001:80 because 80 is the exposed port in that image.
Related
I was to access my css with nginx from public folder it works totally fine when i open
localhost:8080/css/main
but i want to access it from my express server running on 3000, 8080 is just web pack building this public folder and updating it on change so i want to access this folder with my express running on 3000 not on webpack on 8080
localhost:3000/css/main
i tried couple of things but didn't work so i didn't want to mess more with the file
upstream backend {
server dev-node:3000;
}
upstream webpack {
server dev-webpack:8080;
}
server {
listen 80;
server_name localhost;
root /app/public
#access_log /var/log/nginx/host.access.log main;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1d;
}
location ^~ /(audio|build|images|style) {
autoindex on;
root /app/public;
}
location /api {
proxy_pass http://backend;
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 / {
proxy_pass http://webpack;
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 ~ /\.ht {
deny all;
}
}
docker file
version: '3'
services:
dev-nginx:
build:
context: ./nginx
container_name: dev-nginx
links:
- dev-node
ports:
- "80:80"
- "443:443"
volumes:
- ./client/public:/usr/share/nginx/html
dev-webpack:
build:
context: ./client
container_name: dev-webpack
volumes:
- ./client:/app
- /app/node_modules
ports:
- "8080:8080"
command: "yarn dev"
dev-node:
build:
context: ./server
container_name: dev-node
environment:
- NODE_ENV=production
- PORT=3000
volumes:
- ./server:/app
- /app/node_modules
ports:
- "3000:3000"
command: "yarn dev"
Hey I am trying to build a root for a personal portfolio. All containers are running except Streamlit. Streamlit should be accessible via mydomain.com/streamlit. Locally everything works without problems. After calling the homepage and running containers I get a white screen.
Streamlit Config:
[server]
headless=true
port=8501
enableCORS=false
enableXsrfProtection=false
enableWebsocketCompression=false
[browser]
serverAddress="0.0.0.0"
serverPort=8501
NGINX Config :
server {
listen 80;
server_name domain.de;
server_tokens off;
client_max_body_size 20M;
include /etc/nginx/mime.types;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name domain.de;
ssl_certificate /etc/nginx/certs/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/certs/ss-bundle.key;
server_tokens off;
include /etc/nginx/mime.types;
client_max_body_size 20M;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /streamlit {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
proxy_pass http://streamlit:8501;
}
location /api {
try_files $uri #proxy_api;
}
location /admin {
try_files $uri #proxy_api;
}
location #proxy_api {
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://backend:8000;
}
location /django_static/ {
autoindex on;
alias /app/backend/django_static/;
}
}
The Docker-Compose file:
version: '2'
services:
nginx:
restart: unless-stopped
build:
context: .
dockerfile: ./docker/nginx/Dockerfile
ports:
- 80:80
- 443:443
volumes:
- static_volume:/app/backend/django_static
- ./docker/nginx/production:/etc/nginx/conf.d
- ./docker/nginx/certs:/etc/nginx/certs
depends_on:
- backend
environment:
- COMPOSE_HTTP_TIMEOUT=200
backend:
restart: unless-stopped
build:
context: .
dockerfile: ./docker/backend/Dockerfile
entrypoint: /app/docker/backend/wsgi-entrypoint.sh
volumes:
- static_volume:/app/backend/django_static
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- COMPOSE_HTTP_TIMEOUT=200
expose:
- 8000
depends_on:
- db
db:
image: postgres:13.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- 5432:5432
environment:
- POSTGRES_USER=hello_django
- POSTGRES_PASSWORD=hello_django
- POSTGRES_DB=hello_django
- DB_ENGINE=django.db.backends.postgresql
- DB_HOST=db
- DB_PORT=5432
- DEBUG=1
redis:
image: redis:6.2
command: redis-server
ports:
- 6379:6379
restart: unless-stopped
environment:
- COMPOSE_HTTP_TIMEOUT=200
streamlit:
build:
context: .
dockerfile: ./docker/streamlit/Dockerfile
ports:
- 8501:8501
environment:
- COMPOSE_HTTP_TIMEOUT=200
entrypoint: /app/docker/streamlit/entrypoint.sh
volumes:
static_volume: {}
postgres_data:
And the Dockerfile for Streamlit:
# docker/streamlit/Dockerfile
FROM python:3.10.5
WORKDIR /app
ADD ./streamlit/portfolio-streamlit-wrapper/requirements.txt /app/streamlit/
RUN pip install --upgrade pip
RUN pip install -r streamlit/requirements.txt
ADD ./docker /app/docker
ADD ./streamlit/portfolio-streamlit-wrapper/ /app/streamlit
EXPOSE 8501
RUN chmod +x /app/docker/streamlit/entrypoint.sh
The whole project builds up normally. I have access to the Django Admin Dashboard and all containers are running. The regular home page is also displayed. Does anyone have any idea what the reason could be that the homepage changes the HTML - title but the streamlit dashboard is not visible?
Edit:
Streamlit is started due an entryscript: Which I cant explain why I created it.
#!/bin/sh
until cd /app/streamlit/
do
echo "Waiting for server volume..."
done
streamlit run welcome.py
I followed the example: How to upload multiple files with django rest api? The solution work in localhost, but when i upload to nginx server running in a container docker, the nginx return a error 400, i can't more details because i'm not getting log, my nginx configuration it is like this:
upstream api {
server neworleansenglishcourse.com.br:8000;
}
server {
listen 80;
server_name neworleansenglishcourse.com.br www.neworleansenglishcourse.com.br;
location / {
client_max_body_size 8000M;
client_body_buffer_size 8000M;
client_body_timeout 120;
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
client_max_body_size 8000M;
client_body_buffer_size 8000M;
client_body_timeout 120;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://api;
}
location /complementos/ {
client_max_body_size 8000M;
client_body_buffer_size 8000M;
client_body_timeout 120;
}
}
My docker compose it is like this:
version: '3'
services:
backend:
build:
context: ./api
command: gunicorn plataformaingles.wsgi --bind 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- db
networks:
- production-network
frontend:
build:
context: ./front
volumes:
- front_build:/front/build
networks:
- production-network
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/nginx-setup.conf:/etc/nginx/conf.d/default.conf
- front_build:/usr/share/nginx/html
depends_on:
- backend
- frontend
networks:
- production-network
volumes:
front_build:
networks:
production-network:
driver: bridge
Is there a way to only upgrade SignalR WebSocket connections in your nginx.conf file without duplicating a whole section?
ASP.NET Core is rejecting my SignalR connections when they're not upgraded, however I'm running an API on that ASP.NET Core project that won't accept POST Request with bodies when the requests have the header "Connection: "upgrade"".
My current solution is just to duplicate the "location" section in my nginx.conf file as you see below. Is there a more elegant solution?
Thanks!
My nginx.conf file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
include /etc/nginx/mime.types;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location / {
proxy_pass http://ui:80;
rewrite ^/(.*)$ /$1 break;
}
location /shopvac/signalr {
proxy_pass http://shopvac:80;
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;
rewrite ^/shopvac/(.*)$ /$1 break;
}
location /shopvac {
proxy_pass http://shopvac:80;
rewrite ^/shopvac/(.*)$ /$1 break;
}
}
}
My docker-compose.yaml file:
version: "3"
networks:
proxy:
database:
volumes:
database-data:
services:
proxy:
image: nginx:latest
depends_on:
- ui
- shopvac
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./logs/nginx:/var/log/nginx
ports:
- "80:80"
- "443:443"
networks:
- proxy
database:
image: postgres:latest
volumes:
- database-data:/var/lib/postgresql/data
expose:
- "5432"
networks:
- database
environment:
- POSTGRES_DB=home-system
- POSTGRES_USER=home-system
ui:
build:
context: ./ui
dockerfile: Dockerfile
expose:
- "80"
networks:
- proxy
shopvac:
build:
context: ./shopvac
dockerfile: Dockerfile
depends_on:
- database
volumes:
- /app
expose:
- "8080"
networks:
- proxy
- database
Found a solution!
So you can edit the "proxy_set_header Connection" property in nginx.conf to pass the necessary value forward.
The important section of the nginx.conf is now as follows:
location /shopvac {
proxy_pass http://shopvac:80;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
rewrite ^/shopvac/(.*)$ /$1 break;
}
I'm having problems with nginx reverse proxying as a docker container. My question is about how to correctly proxy pass nginx in a default docker network?
Here's my docker-compose.yml (unnecessary details omitted for brevity)
version: '3'
networks:
nginx_default:
external: true
services:
db:
image: postgres:10.2
ports:
- "5432:5432"
environment: ...
postgrest:
image: postgrest/postgrest
ports:
- "3000:3000"
environment: ...
nginx:
restart: always
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/sites-enabled/ruler
command: [nginx-debug, '-g', 'daemon off;']
webapp:
build:
context: "./"
dockerfile: Dockerfile.dev
volumes: ...
ports:
- "3001:3001"
environment: ...
Here's my nginx.conf
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name _;
gzip on;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/x-javascript;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
location / {
try_files $uri #node;
}
location /api/ {
try_files $uri #postgrest;
}
location #node {
proxy_pass http://webapp:3001;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_cache_control;
add_header X-Proxy-Cache $upstream_cache_status;
}
location #postgrest {
proxy_pass http://postgrest:3000;
proxy_http_version 1.1;
default_type application/json;
proxy_set_header Connection "";
proxy_hide_header Content-Location;
add_header Content-Location /api/$upstream_http_content_location;
}
}
And my Dockerfile.dev
FROM node:8.9
WORKDIR /client
CMD npm run dev -- -p 3001
When I do $ docker-compose up -d everything starts without an error. After that I can successfully do $ curl http://127.0.0.1:3001/ (webapp) and $ curl http://127.0.0.1:3000 (postgrest).
But when I try $ curl http://127.0.0.1:8080/ (nginx should handle here the proxying) I get default nginx welcome page. Also $ curl http://127.0.0.1:8080/api/ is not hitting the API :/
What may be the cause? Using $ docker inspect I see that every container is in the same default network.
Edit: Using $ docker-compose logs seems like the default network is not used at all O_o
docker-compose logs
WARNING: Some networks were defined but are not used by any service: nginx_default
Attaching to ruler_webapp_1, ruler_nginx_1
webapp_1 |
webapp_1 | > ruler# dev /client
webapp_1 | > next "-p" "3001"
webapp_1 |
webapp_1 | > Using external babel configuration
webapp_1 | > Location: "/client/.babelrc"
webapp_1 | DONE Compiled successfully in 1741ms09:04:49
webapp_1 |
My guess is you mapped your local nginx configuration file to the wrong file on the container side. The default configuration file for the nginx image is located at /etc/nginx/conf.d/default.conf so the volume of the nginx container should be:
./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
You can check your configuration file is used correctly by executing:
docker-compose exec nginx nginx -T
Side notes:
Never use the latest tag, because in some time you may face broken compatibility issues. Use fixed version tag 1, 1.13 etc. instead
You don't need to publish ports everywhere, eg. 3000:3000, 3001:3001. Those ports will be accessible internally by containers
Your config is a partial config and not a complete nginx config. So it needs to go inside conf.d inside the container and not on nginx.conf or sites-enabled. So change
volumes:
- ./nginx/nginx.conf:/etc/nginx/sites-enabled/ruler
to
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
And now it should start working