nginx and docker hide port from url - docker

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

Related

Docker nginx reverse proxy not load static file

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

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?

Dockercompose, Nginx, Resolver not working

I use an nginx container with this config:
set $ui http://ui:9000/backend;
resolver 127.0.0.11 valid=5m;
proxy_pass $ui;
This is needed, because the "ui" container wont necessarly be up when nginx starts. This avoids the "host not found in upstream..." error.
But now I get a 404 even when the ui-container is up and running (they are both in the same network defined in the docker-compose.yml). When I proxy pass without the variable, without the resolver and start the ui container first, everything works.
Now I am looking for why docker is failing to resolve it. Could I maybe manually add a fake route to http://ui which gets replaced when the ui-container starts? Where would that be? Or can I fix the resolver?
The answer is like in this post:
https://stackoverflow.com/a/52319161/3093499
Only change is putting the resolver and set variable into the server-body instead of the location.
First you need to make sure that you have the port in the ui backend Dockerfile with EXPOSE 9000. Then you're going to want to have this as your config:
http {
upstream ui {
server ui:9000;
}
server {
# whatever port your nginx reverse proxy is listening on.
listen 80;
location / {
proxy_pass http://ui/backend;
}
}
http
{
server {
ssl_certificate /etc/tls/tls.crt;
ssl_certificate_key /etc/tls/tls.key;
resolver 127.0.0.11;
resolver_timeout 10s;
access_log /var/log/nginx/access_log.log;
location / {
set $upstream_app homer;
set $upstream_port 8080;
set $upstream_proto http;
proxy_pass http://localhost:7001;
}
}
}
i worked too

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

Nginx URI path port redirect unexpected behaviour with Docker

I deployed a Docker container on port 8080.
I configured my nginx server to point to port 8080 in nginx.config like so:
server {
...
location / {
proxy_pass http://127.0.0.1:8080;
}
...
This worked fine and the calls to server were redirected to docker container.
I now needed to point different URI paths to different containers.
For example I needed /service1 path to point to my original docker container. So i did this:
server {
...
location / {
}
location /service1 {
proxy_pass http://127.0.0.1:8080;
}
I am following this guide here: https://gist.github.com/soheilhy/8b94347ff8336d971ad0#step-5----add-blog-and-mail
I did nginx -s reload and restart but I get a Whitelabel error when I navigate to {IP_ADDRESS}/service1. Canńot understand this.
Does it has something to do with the location / {} being emtpy?
EDIT
As suggested I redepolyed my container on 8090 and changed the nginx.config file to this:
server {
...
location / {
proxy_pass http://127.0.0.1:8080;
}
location /service1 {
proxy_pass http://127.0.0.1:8090;
}
Put when I HTTP to [IP_ADDRESS]/service1 I still get 404.
EDIT2
As suggested I changed the nginx.conf file to avoid passing the "service1" to container but the error response suggests that it still receives it.
nginx.conf:
location / {
}
location = /service1 {
return 302 /service1/;
}
location /service1/ {
proxy_pass http://127.0.0.1:8090;
}
Error:
{
"timestamp": "2018-04-07T09:56:44.600+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/service1/image"
}
Just to be clear, this error is generated by Docker not Ngnix so the redirect is working?
The issue is likely that the /service1 path is being passed to your container which then responds with a 404 because it doesn't know what to do with it.
Try the following which should strip /service1 from the path:
location = /service1 {
return 302 /service1/;
}
location /service1/ {
proxy_pass http://127.0.0.1:8090/;
}
Note that the trailing slashes are significant and must be respected.

Resources