Last active
August 29, 2015 13:55
-
-
Save kuntoaji/8728290 to your computer and use it in GitHub Desktop.
Nginx Load Balancer Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# default nginx configuration | |
user nginx; | |
worker_processes 1; | |
error_log /var/log/nginx/error.log warn; | |
pid /var/run/nginx.pid; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | |
'$status $body_bytes_sent "$http_referer" ' | |
'"$http_user_agent" "$http_x_forwarded_for"'; | |
access_log /var/log/nginx/access.log main; | |
sendfile on; | |
#tcp_nopush on; | |
keepalive_timeout 65; | |
#gzip on; | |
include /etc/nginx/conf.d/*.conf; | |
# mulai dari sini :) | |
upstream mybackend { | |
# Weight adalah bobot yang digunakan seberapa sering backend server tersebut dipilih untuk memproses request. | |
# Default weight adalah 1 | |
server 192.168.0.102:8080; | |
server mybackend2.example.com:8080 weight=4; | |
server mybackend3.example.com weight=2; | |
server unix:/tmp/backend4; | |
} | |
server { | |
listen 80; | |
server_name example.com www.example.com; | |
location / { | |
proxy_pass http://mybackend; | |
# header ini digunakan agar backend app mengenal request domain sebagai example.com atau www.example.com, bukan sebagai mybackend | |
proxy_set_header Host $http_host; | |
# header ini digunakan agar IP yang diproses aplikasi backend berasal dari IP client, bukan dari load balancer. | |
proxy_set_header X-Real-IP $remote_addr; | |
# beberapa aplikasi atau framework menggunakan X-Forwarded-For untuk mendapatkan alamat IP. | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment