Scenario:

I have a docker container on a Ubuntu server. The Ubuntu server itself has nginx running and setup properly. How do I redirect this nginx to the container?

Reply to this note

Please Login to reply.

Discussion

If your container opens a port to the outside, then you can use an Nginx proxy to serve this port from 127.0.0.1 on a domain.

In this example here, a Haven relay is running on port 3355 in a Docker container. The port is exposed to the outside. My Nginx server then serves this port with SSL and a domain.

location / {

proxy_pass http://127.0.0.1:3355;

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_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

}

Let me get a full example.....

Full example (with Letsencypt SSL cert) of my hosted Haven relay:

server {

http2 on;

listen 443 ssl;

listen [::]:443 ssl;

server_name h.codingarena.top;

server_tokens off;

root /home/user/h.codingarena.top;

ssl_certificate /etc/nginx/ssl/h.codingarena.top/xxx/server.crt;

ssl_certificate_key /etc/nginx/ssl/h.codingarena.top/xxx/server.key;

ssl_protocols TLSv1.2 TLSv1.3;

ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

ssl_prefer_server_ciphers off;

ssl_dhparam /etc/nginx/dhparams.pem;

index index.html index.htm index.php;

charset utf-8;

location / {

proxy_pass http://127.0.0.1:3355;

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_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

}

location = /favicon.ico { access_log off; log_not_found off; }

location = /robots.txt { access_log off; log_not_found off; }

access_log off;

error_log /var/log/nginx/h.codingarena.top-error.log error;

error_page 404 /index.php;

location ~ /\.(?!well-known).* {

deny all;

}

}

Oh. Damn. It was just an "SSL" in the default server config that was wrong. Works now. Thank you!

Sorry for asking again. Last time I setup an nginx server was 2015 and I don't know if docker was even a thing back then.

I made a new host in the default config. With you example.

The fact that my (not dockerised) nip-05 server is still working but the other page is just showing it as well tells me that I am on the right path and did not screw up anything major.

The "http2" parameter does not work for me though.

As it makes more and more fun to type words into a command line I guess I will figure it out today. Nginx manual is my lecture today.

Not sure what you're trying to do exactly but I use a Caddy server to reverse proxy to my containers ip:port

Very convenient and it gives whatever container auto ssl. Adding a new container is adding the dns record and adding the line in Caddy and boom it's done

I want to be able to have multiple docker containers being available via the same nginx server.