Docker nginx reverse proxy not load static file - docker

I am working on a project with AWS LIghtsail container and I am facing with the following problem.
I created 3 docker containers:
-NGINX port 80
-NodeJs port 5000
-Cadvisor port 8080
Lightsail container service allows you to have only one port open from which you can reach your resources, for this reason I used nginx proxy pass to make all my containers reachable on port 80.
My problem is that when I try to reach $host/containers it does not load static resources (CSS, JS, images....).
These are my nginx configs
events {}
http {
upstream node {
server ${NODE_HOST}:${NODE_PORT};
}
upstream cad {
server ${CAD_HOST}:${CAD_PORT};
}
server {
listen 80;
#proxy pass nodejs works
location / {
proxy_pass http://node;
}
#proxy pass cadvisor port8080 not work
location /containers/ {
proxy_pass http://cad;
}
}
}
#EV VAR
NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx
NODE_HOST=node
NODE_PORT=5000
CAD_HOST=cad
CAD_PORT=8080
I was following this AWS official guide (step 5): https://aws.amazon.com/it/getting-started/hands-on/setup-an-nginx-reverse-proxy/
Screenshot

Try:
http {
upstream node {
server ${NODE_HOST}:${NODE_PORT};
}
upstream cad {
server ${CAD_HOST}:${CAD_PORT};
}
server {
listen 80;
location / {
proxy_pass http://node;
}
location #cad {
proxy_pass http://cad;
}
location /docker/ {
alias /cadvisor/docker/;
}
location /containers/ {
alias /cadvisor/containers/;
try_files $uri $uri/ #cad;
}
}
}
and check your logs after

Related

nginx and docker hide port from url

I have a droplet with nginx and docker container that works on port 4001.
I managed to implement only redirect from the main URL to my working container
events {}
http {
server {
listen 80;
server_name 1.1.1.1;
port_in_redirect off;
location / {
return 200 'This is nginx stroke';
}
location ~ /cv {
rewrite ^/cv(.*) http://1.1.1.1:4001;
}
}
}
And when url is http://1.1.1.1/cv me redirects to http://1.1.1.1:4001. How to fix it without port :4001
If i use
location /cv {
proxy_pass http://localhost:4001;
}
In inspect manager i see an empty react project, without my components:
But head is relevant

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?

Nginx Reverse Proxy with Dynamic Containers

I have a reverse proxy with nginx set up using docker compose. It is fully working when I run all services together with docker-compose up. However, I want to be able to run individual containers, and start (docker-compose up service1) and stop them independently from the proxy container. Here is a snippet from my current nginx config:
server {
listen 80;
location /service1/ {
proxy_pass http://service1/;
}
location /service2/ {
proxy_pass http://service2/;
}
}
Right now if I run service1, service2, and the proxy together all is well. However, if I run the proxy and only service2, for example, I get the following error: host not found in upstream "service1" in /etc/nginx/conf.d/default.conf:13. The behavior I want here is to just throw some HTTP error, and when that service does come up to route to it appropriately.
Is there any way to get this behavior?
Your issue is with nginx. It will fail to start if it cannot resolve one of the upstream hostnames.
In your case the docker service name will be unresolvable if the service is not up.
Try one of the solutions here, such as resolving at the location level.
(edit) The below example works for me:
events {
worker_connections 4096;
}
http {
server {
location /service1 {
resolver 127.0.0.11;
set $upstream http://service1:80;
proxy_pass $upstream;
}
location /service2 {
resolver 127.0.0.11;
set $upstream2 http://service2:80;
proxy_pass $upstream2;
}
}
}
Sounds like you need to use load balancing. I believe with load balancing it will attempt to share the load across servers/services. If one goes down, it should automatically use the others.
Example
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
Docs: http://nginx.org/en/docs/http/load_balancing.html

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