nginx轮询负载均衡演示demo

2024-10-09 08:46:17 浏览数 (1)

1.nginx /conf/nginx.conf配置文件

代码语言:javascript复制
#user  nobody;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
	use epoll;
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
	upstream yes.oktokeep.com {
		
		server 127.0.0.1:8882;
		server 127.0.0.1:8883;
		server 127.0.0.1:8881;
		server 127.0.0.1:8884;
	}
	
    server {
        listen       80;
        server_name  yes.oktokeep.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://yes.oktokeep.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;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

2.修改 /etc/hosts文件

增加映射  127.0.0.1 yes.oktokeep.com

3.创建nodejs文件,模拟4个应用,文件内容一样,仅仅端口号不同

8881.js(同理创建8882.js,8883.js,8884.js文件)

代码语言:javascript复制
var http = require("http");
http.createServer(function(request,response){
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("Hello Nignx,I am warking on port 8881.n");
    response.end();
    
}).listen(8881);

console.log("nodejs start listen 8881 port!n");
代码语言:javascript复制
var http = require("http");
http.createServer(function(request,response){
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("Hello Nignx,I am warking on port 8882.n");
    response.end();
    
}).listen(8882);

console.log("nodejs start listen 8882 port!n");
代码语言:javascript复制
var http = require("http");
http.createServer(function(request,response){
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("Hello Nignx,I am warking on port 8883.n");
    response.end();
    
}).listen(8883);

console.log("nodejs start listen 8883 port!n");
代码语言:javascript复制
var http = require("http");
http.createServer(function(request,response){
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("Hello Nignx,I am warking on port 8884.n");
    response.end();
    
}).listen(8884);

console.log("nodejs start listen 8884 port!n");

4.启动nginx, node 8881.js &

5.模拟请求

上传3份nodejs文件 node /usr/workspace/nodejs/8881.js & node /usr/workspace/nodejs/8882.js & node /usr/workspace/nodejs/8883.js & node /usr/workspace/nodejs/8884.js &

##使用轮询的方式来访问 [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8883. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8884. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8882. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8881. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8883. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8884. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8882. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8881. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8883. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8884. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8882. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8881. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8883. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8884. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8882. [root@VM-4-3-centos /]# curl http://yes.oktokeep.com Hello Nignx,I am warking on port 8881.

0 人点赞