Docker nginx dynamic proxy_pass is not working - docker

I want to access docker container by name in nginx reverse proxy which is also a docker container.
nginx configuration is as follow
server {
server_name domain.com;
location / {
proxy_pass http://host.docker.internal:3000;
}
}
server {
server_name api-sub.domain.com;
resolver 127.0.0.11 valid=10s ipv6=off;
location / {
proxy_pass http://$arg_subdomain:3001;
}
}
server {
server_name ~^(?<subdomain>\w+).domain\.com$;
resolver 127.0.0.11 valid=10s ipv6=off;
location / {
proxy_pass http://$subdomain:3002;
}
}
The first application is running on host os and working fine. Apart from first all container running in same user defined docker network but they are ginving 502 Bad Gateway. So I think that the resolver not providing IP against docker container. I am running the container using the following command
-- nginx
docker run -p 80:80 -d --network="user-defined-network" --restart always --add-host=host.docker.internal:host-gateway image-nginx
-- other container
docker run -d --network user-defined-network image-solution
Can someone please help me.

Related

how to reach another two containers with cpprest from a dockerised nginx

i have 2 docker containers with cpprest runned in:
docker run -it -p 0.0.0.0:8081:8080
and
docker run -it -p 0.0.0.0:8082:8080
I have nginx in docker container runned in: docker run -it -p 0.0.0.0:8083:8080
my nginx.conf file is:
http {
upstream backend {
server 0.0.0.0:8081;
server 0.0.0.0:8082;
}
# This server accepts all traffic to port 80 and passes it to the upstream.
# Notice that the upstream name and the proxy_pass need to match.
server {
listen 80;
location / {
proxy_pass http://backend/;
}
}
}
When nginx isn't in a container all works fine, but when i try with dockerised nginx and open 0.0.0.0:8083 - it gives me :

Nginx Reverse Proxy To Docker Container Web Apps Giving 404

I just made a fresh Ubuntu desktop vm, threw docker on it, threw Nginx on it, and pulled and ran the container yeasy/simple-web:latest, and ran it twice with the commands
docker run --rm -it -p 8000:80 yeasy/simple-web:latest
docker run --rm -it -p 8001:80 yeasy/simple-web:latest
I went over to /etc/nginx/sites-available and created a new file localhost.conf with the contents
server {
listen 80;
location /chad {
proxy_pass http://127.0.0.1:8000/;
}
location /brock {
proxy_pass http://127.0.0.1:8081/;
}
}
I then created a symlink of the localhost.conf file at /etc/nginx/sites-enabled with the command
ln -s ../sites-available/localhost.conf .
This was all done as root.
When I curl localhost:8000 and localhost:8001 I get the correct webpage hosted in the docker container. When I curl localhost/chad or localhost/brock, I get an Nginx 404 error. I have not touched the default config for Nginx, and did not modify the Docker images
I am limited to using docker images and Nginx, so I cannot change technology stacks.
Not sure if you're already doing this but it's worth mentioning:
You need to reload or restart Nginx whenever you make changes to its configuration.
To reload Nginx, use one of the following commands:
sudo systemctl reload nginx
sudo service nginx reload
I ended up being able to host both my docker containers with Nginx on the host machine with the following config following the above instructions.
server {
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
listen 127.0.0.1;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location /chad {
proxy_pass http://127.0.0.1:8000/;
}
location /brock {
proxy_pass http://127.0.0.1:8001/;
}
}

Nginx reverse proxy not finding other internal Docker container using hostname

I have two docker containers. One runs Kestrel (172.17.0.3), The other runs Nginx (172.17.0.4) using a reverse proxy to connect to Kestrel. Nginx connects fine when I use internal Docker ip of Kestrel container but when I try to connect to Kestrel using container's hostname in nginx.conf (kestral) I get following error:
2020/06/30 00:23:03 [emerg] 58#58: host not found in upstream "kestrel" in /etc/nginx/nginx.conf:7
nginx: [emerg] host not found in upstream "kestrel" in /etc/nginx/nginx.conf:7
I launched containers with these two lines
docker run -d --name kestrel --restart always -h kestrel mykestrelimage
docker run -d --name nginx --restart always -p 80:80 -h nginx mynginximage
My nginx.conf file below.
http {
# I've tried with and without line below that I found on Stackoverflow
resolver 127.0.0.11 ipv6=off;
server {
listen 80;
location / {
# lines below don't work
# proxy_pass http//kestrel:80;
# proxy_pass http//kestrel
# proxy_pass http//kestrel:80/;
# proxy_pass http//kestrel/;
# when I put internal docker ip of Kestrel server works fine
proxy_pass http://172.17.0.3:80/;
}
}
}
events {
}
I figured out solution to my problem. There were two issues.
First problem: By default Docker uses default bridge network when creating containers. The default Docker bridge network does not resolve DNS though. You have to create a custom bridge network and then specify network when creating docker containers. The below allowed me to ping between containers using hostname
docker network create --driver=bridge mycustomnetwork
docker run -d --name=kestrel --restart=always -h kestrel.local --network=mycustomnetwork mykestrelimage
docker run -d --name=nginx --restart always -p 80:80 -h nginx.local --network=mycustomnetwork mynginximage
Second problem: Even though it was only one kestrel server for some reason Nginx required that I setup an upstream section in /etc/nginx/nginx.conf
http {
upstream backendservers {
server kestrel;
}
server {
listen 80;
location / {
proxy_pass http://backendservers/;
}
}
}
events {
}

nginx reverse proxy proxy_pass wildcard

I have an application running on port 4343. This is a single page app, so hitting http://myApp:4343 will dynamically redirect me to somewhere like http://myApp:4343/#/pageOne.
Both the nginx container and the myApp container are running on the same docker network so can resolve via container name.
I'm trying to proxy this via nginx with:
server {
listen 80;
server_name localhost;
location /myApp {
proxy_pass http://myApp:4343
}
}
How do I wildcard the rule?

jenkins behind nginx reverse proxy

I'm trying to keep a jenkins container(docker) behind nginx reverse proxy. It works fine with this path, https://example.com/ but it returns 502 Bad Gateway when I add parameter to the path, https://example.com/jenkins.
The docker container for jenkins is run like this
docker container run -d -p 127.0.0.1:8080:8080 jenkins/jenkins
Here is my code,
server {
listen 80;
root /var/www/html;
server_name schoolcloudy.com www.schoolcloudy.com;
location / {
proxy_pass http://localhost:8000;
}
}
# Virtual Host configuration for example.com
upstream jenkins {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name jenkins;
location /jenkins {
proxy_pass http://jenkins;
proxy_redirect 127.0.0.1:8080 https://schoolcloudy.com/jenkins;
}
}
Specify the Jenkins container's network with --network=host flag when you run the container. This way the container will be able to interact with host network or use the container's IP explicitly in the Nginx conf.
good practice in such questions is official documentation usage:
wiki.jenkins.io
I've configured Jenkins behind Nginx reverse proxy several time, wiki works fine for me each time.
P.S.: look like proxy_pass option value in your config should be changed to http://127.0.0.1:8080

Resources