upstream to loadhst nginx code example
Example 1: nginx load balancer
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
Example 2: nginx config multiple upstream
sudo yum install httpd-tools [RHEL/CentOS]
sudo apt install apache2-utils [Debian/Ubuntu]
create password files with below commands
htpasswd -c /home/osboxes/nginx1-auth.htpasswd admin
htpasswd -c /home/osboxes/nginx2-auth.htpasswd sysadmin
htpasswd -c /home/osboxes/glances-auth.htpasswd devopsadmin
events {}
http {
upstream backend_nginx1 {
server nginx1;
}
upstream backend_nginx2 {
server nginx2;
}
upstream backend_glances {
server glances:61208;
}
server {
listen 7070;
server_name _;
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/nginx1-auth.htpasswd;
proxy_pass http://backend_nginx1;
}
}
server {
listen 8080;
server_name _;
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/nginx2-auth.htpasswd;
proxy_pass http://backend_nginx2;
}
}
server {
listen 9090;
server_name _;
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/glances-auth.htpasswd;
proxy_pass http://backend_glances;
}
}
}
sudo docker run -d --name nginx1 nginx
sudo docker run -d --name nginx2 nginx
sudo docker run -d --restart="always" -e GLANCES_OPT="-w" -v /var/run/docker.sock:/var/run/docker.sock:ro --pid host --name glances docker.io/nicolargo/glances
sudo docker run -d --name nginx-reverseProxy -p 7070:7070 -p 8080:8080 -p 9090:9090 --link nginx1:nginx1 --link nginx2:nginx2 --link glances:glances -v /home/osboxes/nginx.conf:/etc/nginx/nginx.conf -v /home/osboxes/nginx1-auth.htpasswd:/etc/nginx/nginx1-auth.htpasswd -v /home/osboxes/nginx2-auth.htpasswd:/etc/nginx/nginx2-auth.htpasswd -v /home/osboxes/glances-auth.htpasswd:/etc/nginx/glances-auth.htpasswd nginx
events {}
stream {
upstream postgres {
server pdb2:5432;
}
server {
listen 5432;
proxy_pass postgres;
}
}