How to set up subdomains correctly in docker nginx? - docker

I am trying to set up subdomains to separate the back-end and front-end sides:
api.domain.com (back-end)
app.domain.com (front-end)
This is my docker-compose file:
version: "3.8"
services:
gateway:
container_name: ws-gateway
build:
context: gateway/nginx
dockerfile: Dockerfile
ports:
- "8082:8082"
depends_on:
- api
api:
container_name: ws-api
build:
context: api/nginx
dockerfile: Dockerfile
volumes:
- ./api:/app
This is my gateway Dockerfile:
FROM nginx:1.21.3-alpine
COPY ./conf.d/api.conf /etc/nginx/conf.d
WORKDIR /app
Configuration file "api.conf":
server {
listen 8082;
listen [::]:8082;
server_name api.mydomainname.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://api;
proxy_ssl_session_reuse off;
proxy_redirect off;
}
}
I specified to listen on port 8082, specified the server name, but that doesn't work.
When I try to go to the api.mydomainname.com the server gives an error "This site can’t be reached". But if I specify the port mydomainname.com:8082, then it works:

Related

How to access node server from react front end? (MERN/NGINX)

I want to call my api point in my react application. Here when I access the localhost then my react app opens up but all the api points doest work when I hit "localhost/apipoint". this shows me 404 not found or 502 bad request.
Here is my docker-compose file
version: "3.8"
services:
nodeserver:
build:
context: ./Backend
networks:
- app-network
ports:
- "8082:8082"
frontend:
build:
context: ./Frontend
networks:
- app-network
ports:
- "80:80"
networks:
app-network:
my nginx default.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location /apipoint {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8082;
}
location / {
# root /usr/share/nginx/html;
root /var/www/html;
}
}

Containerized Keycloak behind Nginx not working (502 Bad Gateway)

I need to serve containerized keycloak behind Nginx. Keycloak runs without any problem at 'localhost:8080' but when I try to access it through the reverse proxy at 'localhost/auth' I get '502 Bad Gateway'.
Here's the details of the error taken from Nginx logs:
[error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.80.1, server: , request: "GET /auth/ HTTP/1.1", upstream: "http://127.0.0.1:8080/auth/", host: "localhost"
Please find below my docker-compose file (I haven't pasted the other containers):
version: '3'
services:
keycloak:
image: jboss/keycloak
container_name: keycloak
ports:
- "8080:8080"
environment:
KEYCLOAK_USER: ${KEYCLOAK_USER}
KEYCLOAK_PASSWORD: ${KEYCLOAK_PASSWORD}
KEYCLOAK_DB_VENDOR: ${KEYCLOAK_DB_VENDOR}
KEYCLOAK_DB_ADDR: ${KEYCLOAK_DB_ADDR}
KEYCLOAK_DB_DATABASE: ${KEYCLOAK_DB_DATABASE}
KEYCLOAK_DB_USER: ${KEYCLOAK_DB_USER}
KEYCLOAK_DB_PASSWORD: ${KEYCLOAK_DB_PASSWORD}
PROXY_ADDRESS_FORWARDING: 'true'
depends_on:
- keycloak-db
keycloak-db:
image: postgres
container_name: keycloak-db
restart: unless-stopped
environment:
POSTGRES_USER: ${POSTGRES_USERNAME}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: keycloak
volumes:
- ./keycloak/data:/var/lib/postgresql/data/
networks:
- my-app
nginx:
image: nginx:1.15-alpine
container_name: nginx
build:
context: ./nginx
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./nginx/conf:/etc/nginx/conf.d
networks:
- my-app
networks:
my-app:
This is the Nginx upstream.conf:
# path: /etc/nginx/conf.d/upstream.conf
# Keycloak server
upstream keycloak {
server localhost:8080;
}
Nginx default.conf:
server {
listen 80;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
# keycloak
location /auth {
proxy_pass http://keycloak/auth;
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-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /auth/admin {
proxy_pass http://keycloak/auth/admin;
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-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
Does anyone have any idea what is wrong in my configuration?
Obvious problem:
upstream keycloak {
server localhost:8080;
}
Each container has own "localhost", so you are connecting to nginx's localhost != keycloak's localhost != host's localhost. Use service name there, e.g.:
upstream keycloak {
server keycloak:8080;
}

Docker Network Wildcard Subdomain routing of internal requests to nginx service

I'm building an e2e test suite inside a docker container for CI.
It has 3 services (admin, platform, dashboard) which all connect to a postgres instance.
I use nginx as a reverse proxy to direct the traffic to the correct service based on the subdomain.
Dashboard (dashboard.localtest.me) and Admin (admin.localtest.me) each have their own subdomain and everything else goes to Platform (ex. accounts.localtest.me, public.localtest.me) and there are hundreds of subdomains. The services are on their own ports as well as part of how the product is designed.
Currently I can bring all this up in docker-compose, make requests to <foo>.localtest.me:8080 from my browser and nginx will direct all the traffic to the correct endpoints. Works great.
But when the Cypress service tests start making requests (from inside the docker host) they don't resolve. My assumption is that since the hostname isn't resolving then the request isn't going to the nginx service, which means it can't get routed to the correct product service (admin, platform, or dashboard)
If I can route everything to nginx with a dns wildcard (*.localest.me) I think that would work but I can't figure out what modules or tools can allow for that. Everything I've found is for handing a reverse proxy connecting to the docker host, not containers making url requests internally.
TL;DR
How can I allow cypress container to make wildcard GET requests (*.localtest.me) to my nginx reverse_proxy container
This is roughly what my docker compose looks like, I've removed any of the internal env vars that aren't relevant
services:
postgres:
build:
context: ./
dockerfile: postgres.dockerfile
ports:
- 5432:5432
dashboard:
build:
context: ./
dockerfile: web.dockerfile
ports:
- 9001:9000
platform:
build:
context: ./
dockerfile: web.dockerfile
ports:
- 8001:8000
admin:
build:
context: ./
dockerfile: web.dockerfile
ports:
- 7001:7000
nginx:
restart: always
image: nginx
build:
context: ./
dockerfile: nginx.dockerfile
ports:
- 8080:80
- 443:443
- 8000:8000
- 9000:9000
- 7000:7000
cypress:
image: cypress-testing
build:
context: ./
dockerfile: cypress-tests.dockerfile
ports:
- 6000:6000
Here is my nginx.conf
server {
listen 80;
listen 9000;
server_name dashboard.localtest.me;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_pass http://dashboard:9000/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_buffering off;
chunked_transfer_encoding off;
}
}
server {
listen 80;
listen 7000;
server_name admin.localtest.me;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header Connection "";
proxy_pass http://admin:7000/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_buffering off;
chunked_transfer_encoding off;
}
}
server {
listen 80;
listen 8000;
server_name ~(^|\.)localtest\.me$;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_pass http://platform:8000/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_buffering off;
chunked_transfer_encoding off;
}
}

custom port assign to PgAdmin4 by NGINX configuration in docker container cluster

I am trying to do reverse proxy in my Docker container. I am trying to use PgAdmin4 in port 8080, not 80. For security reasons, I am not using Docker port forwarding. I am trying to use Nginx to reverse proxy pgAdmin's port 80 to 8080. my Nginx code :
server {
listen 8080;
location / {
proxy_pass http://pgadmin_test:80/;
proxy_redirect off;
proxy_set_header Host $host
}
}
This code is not working.
But if I use port 80 in Nginx, it works properly
server {
listen 80;
location / {
proxy_pass http://pgadmin_test:80/;
proxy_redirect off;
proxy_set_header Host $host
}
}
The git hub link is: https://github.com/subhadipsjsc/docker_nginx_pgadmin
nginx_test:
build: ./nginx
container_name: nginx_test
restart: always
ports:
- "3000:3000"
pgadmin_test:
image: dpage/pgadmin4
restart: always
depends_on:
- postgres_test
ports:
- 80:80
server {
listen 3000;
location / {
proxy_pass http://pgadmin_test:80/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Here is the official documentation: https://www.pgadmin.org/docs/pgadmin4/latest/container_deployment.html#http-via-nginx
For me this was helpful because I wanted pgadmin mounted in a subroute /pgadmin

NGINX Proxy does not work

I have two containers running on my DigitalOcean droplet. The first container is a REST api container running on port 9090. The second container is a ReactJS multistage container with Nginx running on port 4500. The docker-compose file is this:
version: '3'
services:
ubgrillmysql:
image: mysql:5.6
container_name: ubgrillmysql
environment:
- MYSQL_ROOT_PASSWORD=finder
- MYSQL_DATABASE=ubgrillData
- MYSQL_USER=ubgadmin
- MYSQL_PASSWORD=win(der90)Admin
volumes:
- /home/justicar/workspace/applications/data
networks:
- ubgrill_network
web:
image: ubgrill/web
container_name: web
depends_on:
- ubgrillmysql
environment:
- DATABASE_HOST=ubgrillmysql
- DATABASE_USER=ubgadmin
- DATABASE_PASSWORD=win(der90)Admin
- DATABASE_NAME=ubgrillData
- DATABASE_PORT=3306
ports:
- 9090:9090
networks:
- ubgrill_network
frontend:
image: ubgrill/frontend
container_name: frontend
build: ./src/main/ubgrill
depends_on:
- web
ports:
- 4500:80
networks:
- ubgrill_network
networks:
ubgrill_network:
external:
I have installed nginx on my droplet as I intend to transfer my blog there in the future. I created a server block using mu droplet's domain name and stored in /etc/nginx/sites-available with the name <domain>.com. The file is as follows:
upstream ubgrillapi{
server localhost:9090;
}
upstream ubgrillapp{
server localhost:4500;
}
server {
listen 80;
listen [::]:80;
root /var/www/<domain>.com/html;
index index.html index.htm;
server_name <domain>.com www.<domain>.com;
location / {
try_files $uri $uri/ =404;
}
location /ubgrill/api/ {
proxy_pass http://ubgrillapi;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /ubgrill/app/ {
proxy_pass http://ubgrillapp;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
When I try to access, http://<domain>.com/ubgrill/app, I get a 404(Not Found) error. The same goes for http://<domain>.com/ubgrill/api. I can access the app at http://<domain>.com:4500 and the api at http://<domain>.com:9090/ubgrill/api (The API actually starts with /ubgrill/api). I am a complete noob in nginx proxy and the tutorials online all deal with nginx in a separate container which is not my configuration. What is the problem with my configuration and can you please point me to a more comprehensive tutorial on nginx? Thanks.
In nginx conf :
proxy_pass http://ubgrillapp;
http://nginx.org/en/docs/http/ngx_http_proxy_module.html
Nginx proxy_pass shows the location
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
(http://example.com:9090/ubgrill/api)
In your case you i guess you should set this to:
proxy_pass localhost:9090/ubgrill/api

Resources