How to setup Nginix Docker Reverse proxy - azure

I am trying to use Nginix reverse proxy container for my web application(another docker container) which runs on non standard port ,
unfortunately I cannot edit my web application container as its developed by some vendor , so I have a plain request that I need to setup nginx as frontend with 80/443 and forward all requests to 10.0.0.0:10101(web app container).
I had tried jwilder/nginx proxy and default docker nginx container not able to get the right configurtaion .any lead would be great.
At the moment I haven't shared any conf files , I can share it on demand. here is the environment details
OS - Ubuntu
Azure

Use proxy_pass nginx feature
Assuming you have both containers linked and the web app container's name is webapp use this configuration on nginx container
upstream backend {
server webapp:10101;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
NOTE: please note that I am skipping some configurations as this is just an example
Put the configuration in nginx.conf file and then deploy the container like this
docker run -d -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf -p 80:80 nginx
Then you'll be able to access your webapp on http://locahost

Related

Nginx Load Balancer High availability running in Docker

I am running, nginx load balancer container for my backend web server,
Since I have only one container running as nginx load balancer, In case of container die/crash, clients cannot reach webservers,
Below is the nginx.conf
events {}
http {
upstream backend {
server 1.18.0.2;
server 1.18.0.3;
}
# 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;
}
}
}
Below is the Dockerfile:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
I am running nginx container like below
docker run -d -it -p 8082:80 nginx-ls
I am accessing above server with http://serve-ip:8082
Container may crash or die any time, in that case my application is not reachable, So I tried to run another container like below and I am getting below error since I am using same port and it is oblivious we cannot repurpose same port in same host.
docker run -d -it -p 8082:80 nginx-ls
06a20239bd303fab2bbe161255fadffd5b468098424d652d1292974b1bcc71f8
docker: Error response from daemon: driver failed programming external connectivity on endpoint
suspicious_darwin (58eeb43d88510e4f67f618aaa2ba06ceaaa44db3ccfb0f7335a739206e12a366): Bind for
0.0.0.0:8082 failed: port is already allocated.
So I ran in different port, It works fine
docker run -d -it -p 8083:80 nginx-ls
But How do we tell/configure clients use port 8083 container when container 8082 is down
or is there any other best method to achieve nginx load balancer with high availability?
Note: For some reasons, I cannot use docker-compose

Deploy nodejs app on ecs

I'm try launch Nodejs app on alibabacloud ecs with using nginx.
my app is in /var/www/html/ar_exp folder
When I runnode app then app listen localhost:8080
my nginx default config:
`
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://localhost:8080;
}
}
`
When I check from my ecs server via curl mydomain.com or via wget mydomain.com response is correct from nodejs app. But when i check it in browser then: ERR_connection_timed_out.
For all files in nodejs app folder () i set the following settings:
chown -R www-data:www-data /var/www/
and 755 for all files
what else would I check?
Website inaccessibility from the ECS can be due to following reasons.
OS firewall: Make sure that the application OS firewall(ufw) settings are properly configured to allow communication to the application port.
VPC Security Group: Make sure that the security group setting is applied and required communication port is configured.Details to configure security group

how to config nginx docker container to recognize localhost application in different port?

Setup:
All following are running on my Mac OS:
localhost:8089 a nodejs REST api runing in my local, OUTSIDE of the nginx container, stand alone!
locahost:80 nginx docker container
I was able to serve static file inside the nginx docker container, but when I set the config for the nginx as:
http {
server {
location / {
root /usr/share/nginx/html;
}
location /api/ {
proxy_pass http://localhost:8089;
}
}
}
for some reason, any localhost:80/api call that suppose to direct to http://localhost:8089; call is returning 404 not found page
404 Not Found
nginx/1.13.6
Any idea where is the config I made wrong? I feel like maybe I shouldn't use localhost:8089 inside nginx? But then what should I be using?
An example can be found here
https://github.com/adamchenwei/docker-nginx-playground
Containers have their own networking namespace / networking stack. So localhost inside the container is the localhost of the container itself.
If you are running Docker for Mac, there is a special hostname that's available inside the container that allows you to connect to services that are running on the host itself. Refer to this answer for more information; https://stackoverflow.com/a/43541732/1811501
If you are running Docker on Linux, this answer (on the same question) allows you to find the IP-address to connect to; https://stackoverflow.com/a/31328031/1811501

NGINX reverse proxy to docker container running web app

On the host I have the docker container on port 4012, and in the docker container the webapp runs on port 3000 ( 0.0.0.0:4012->3000/tcp )
So to access the webapp i simply go to http://hostname:4012 and the webpage shows fine.
I want to be able to go to http://hostname/metrics to run the same webpage from my browser.
While I got this to work by simply adding a location to the nginx.conf on the host:
location /metrics {
proxy_pass http://localhost:4012;
}
All that loads is the index.html (I see the same html source code at http://localhost:4012 and http://hostname/metrics)
but the http://hostname/metrics does not load the javascript assets needed to run the webapp.
From developer tools I see the non proxied site loads assets like so:
http://hostname:4012/assets/styles.css
While the proxied version that goes to /metrics tries to load like this:
http://hostname/assets/styles.css
It doesn't append the /metrics to the assets like it does to get the index.html...
What am I missing here?
If it means anything, the webapp is running on a nodejs express server listening to port 3000 on the docker container.
You need to make the app work that way
location /metrics/ {
proxy_pass http://localhost:4012/;
sub_filter_once off;
sub_filter 'http://localhost:4012/' '$scheme://$host/metrics/';
sub_filter '<head>' '<head>\n<base href="hostname:4012">';
}
So adding a <base> tag to the html helps change /css to /metrics/css. Then absolute urls are also changed using sub_filter.

How deploy PHP Application in WebApp for containers

I have a containerised PHP application (Symfony) in a PHP-FPM container. Currently we expose this website with a Nginx Server as reverse proxy and connecting via fastcgi to this PHP-FPM container.
Do someone how I can bring this PHP-FPM container to Web App for Containers Azure Service?
Do I need to included an WebServer to my container to publish the website? (something like Apache+mod_php?)
I believe should be someway to connect the Azure WebServer to my container.
Thanks,
You're only allowed 1 image, so you'll have to add a webserver to your existing PHP-FPM container. You've already got all of the nginx configs I assume, so try installing nginx in your PHP-FPM container.

Resources