winds配置nginx反向代理和负载均衡

1.首先去管网去下载nginx

下载地址:http://nginx.org/en/download.html

解压并打开截图如下(放的位置不要有中文要不然会启动出错):

2.启动一个tomcat服务器,端口为8080

 启动成功后并访问:http://localhost:80880/tets/index.html

3.配置本地hosts

配置好之后,访问www.123.com:8080/test/index.html

 此时得访问还需加上端口。

4.配置nginx.conf

配置反向代理

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;
    server {
        listen       80; #监听得端口80
        server_name  www.123.com;  #系统服务名称,即hosts所配置得名称

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_pass http://127.0.0.1:8080; #配置反向代理得地址
            index  index.html index.htm;
        }

        #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;
        #}
    }

 5.启动nginx,点击nginx.exe,并查看后台进程是否启动成功。

如果启动失败请查看log日志查找原因。

访问地址http://www.123.com/test/index.html,是没有端口得。

6.上面得nginx反向代理已配置成功,如果出现域名一样,端口不一样 则需要配置负载均衡,nginx.conf配置如下:

#负载均衡得地址
upstream server {
		server 127.0.0.1:8080;
		server 127.0.0.1:8081;
		server 127.0.0.1:8082;
	}
    server {
        listen       80; #监听得端口
        server_name  www.123.com;#系统服务名称,hosts配置得名称

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_pass   http://server; #反向代理
            index  index.html index.htm;
        }

        #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;
        #}
    }

7.重新启动nginx,输入命令:nginx.exe -s reload

访问地址http://www.123.com/test/index.html

轮询法

从上图可以看出每次访问得内容都不一样,8080/8081/8082轮询得去访问,nginx 默认得负载均衡算法是轮询法,你一次我一次。

加权轮询法

负载均衡地址后面的 weight=1,weight=2 是表示权重的意思,数字越大,权重越高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值