nginxでサーバ負荷分散を設定するサンプルコード
1.負荷分散
①http://www.arkgame.com
負荷分散:192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.168.1.5:804台サーバ
②http://blog.arkgame.com
負荷分散:192.168.1.7の8080、8081、8082ポート。
2.設定ファイル
nginx.conf:
3.設定内容:
user www www;
worker_processes 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#最大ファイルディスクリプタ
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include conf/mime.types;
default_type application/octet-stream;
keepalive_timeout 120;
tcp_nodelay on;
upstream www.arkgame.com {
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
}
upstream blog.arkgame.com {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}
server
{
listen 80;
server_name www.arkgame.com;
location / {
proxy_pass http://www.arkgame.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format www_startnews24_com '$remote_addr – $remote_user [$time_local] $request '
'"$status" $body_bytes_sent “$http_referer" '
'"$http_user_agent" “$http_x_forwarded_for"';
access_log /data1/logs/www.log www_startnews24_com;
}
server
{
listen 80;
server_name blog.arkgame.com;
location / {
proxy_pass http://blog.arkgame.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format blog_s135_com '$remote_addr – $remote_user [$time_local] $request '
'"$status" $body_bytes_sent “$http_referer" '
'"$http_user_agent" “$http_x_forwarded_for"';
access_log /data1/logs/blog.log blog_s135_com;
}
}