nginx reverse proxy proxy_pass wildcard - docker

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?

Related

Can't configure nginx reverse proxy

I'm running php+nginx api inside docker container. It is available on port 8080. I trying to add nginx reverse proxy to open api on address api.versite.online and frontend project on versite.online.
I installed nginx on server, added /etc/nginx/sites-available/api.versite.online config (also added symlink to sites-enabled directory), tested config with nginx -t, restarted nginx service with systemctl reload nginx, but it had no effect. api.versite.online:8080 and versite.online:8080 makes request to docker container, looks like top level nginx are ignored.
Nginx access log is empty.
/etc/nginx/sites-available/api.versite.online config
server {
listen 80;
server_name api.versite.online;
access_log /var/log/nginx/api.versite.access.log;
location / {
proxy_pass http://localhost:8080;
}
}
It seems that i forgot to add a firewall rule with sudo ufw allow 'Nginx HTTP'

Docker nginx dynamic proxy_pass is not working

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.

nginx server block + docker, how does back and front communicate

My application is divided into a backend and frontend docker container which are running in digital ocean server. I purchased a domain and inserted the routes provided from digital ocean into my namecheap DNS. I am using nginx server block to route my frontend to the server and would like it to communicate to my backend docker container. I am currently watching this tutorial from faraday.
My frontend container is running on localhost:3000 and my backend is running on localhost:5000;
And i've set the ports to run when the location is server_name/ . How will my nginx server block know whether it's loading the frontend or backend to the domain since both are expected to run proxy_pass at location /?
I want to display the front onto server_name provided but still able to access my backend
server{
server_name newlife.life;
access_log /var/log/nginx/st-access.log
error_log /var/log/nginx/st-error.log debug
location / {
proxy_pass http://localhost:3000;
}
location / {
proxy_pass http://localhost:5000;
}
}
Your frontend can make the backend calls on a different subpath. These requests will arrive at nginx and then nginx can proxy them to backend by rewriting the URL using the http_rewrite module.
See https://nginx.org/en/docs/http/ngx_http_rewrite_module.html
Example:
location /backend {
proxy_pass localhost:5000;
rewrite ^/backend/(.*) /$1 break;
}

spring boot application behind nginx with https

I have two docker containers:
One container runs my spring boot application which listens on port 8080:
This container exposes 8080 port to other docker containers.
Container ip in the docker network is 172.17.0.2.
The other container runs nginx which publishes port 80.
I can successfully put my spring boot app behind nginx with the following conf in my nginx container:
server {
server_name <my-ip>;
listen 80;
location / {
proxy_pass http://172.17.0.2:8080/;
}
}
Doing a GET request to my REST API (http://my-ip/context-url) works fine.
I am trying now to put my application behind nginx with https. My nginx conf is as follows:
server {
server_name <my-ip>;
listen 80;
return 301 https://$server_name$request_uri;
}
server {
server_name <my-ip>;
listen 443;
ssl on;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
location / {
proxy_pass http://172.17.0.2:8080/;
}
}
However I cannot access my application now either through http or https.
http redirects to https and result is ERR_CONNECTION_REFUSED
Problem was that I was not publishing 443 port when running nginx container but only port 80.The nginx configuration is right.

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