LoginSignup
6
9

More than 5 years have passed since last update.

nginxでLoadBalancer

Posted at

参考

CentOS6にインストール

centos6
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install -y nginx
service nginx start

インストール直後の設定

  • 長くなるのでコメントは削除してます
/etc/nginx/nginx.conf
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;
}
/etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
  • /usr/share/nginx/html/index.html が 最初に使われるindex.html

ロードバランサ設定

/etc/nginx/nginx.conf
http {
    upstream backend {
        server yahoo.co.jp;
        server google.co.jp;
        server 192.168.0.4;
    }
    server {
        location / {
            proxy_pass http://backend;
        }
    }
}

nginxに何度かアクセスすると、yahoo, google, 192.168.0.4 のWEBページが表示されることを確認。

さらなる情報はマニュアル参照

6
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9