linux下nginx 配置反向代理及负载均衡,linuxnginx


1.1. 启动nginx

启动 :进入sbin目录

[root@localhost sbin]# ./nginx   或者启动的时候加载指定conf: ./nginx -c /usr/local/nginx/conf/nginx.conf

关闭nginx:

[root@localhost sbin]# ./nginx -s stop

推荐使用:

[root@localhost sbin]# ./nginx -s quit

重启nginx:

1、先关闭后启动:先执行上面的关闭,再启动开启。

2、刷新配置文件: [root@localhost sbin]#  ./nginx -s reload

1.2. 访问nginx

默认是80端口。

2.Nginx反向代理

上传tomcat到服务器并解压:

复制tomcat生成tomcat01、tomcat02

修改tomcat02端口号:

vim tomcat02/conf/server.xml

修改port为8086:

端口为8081:

8009为8010:

保存退出。

启动tomcat01,tomcat01

访问两个tomcat是否OK:

修改tomcat01,02的欢迎页 好做区分:

vim tomcat01/webapps/ROOT/index.jsp

添加端口号在页面:

vim tomcat02/webapps/ROOT/index.jsp

再次访问两个tomcat:

编辑nginx的conf文件添加以下内容:

vim nginx/conf/nginx.conf

	 upstream tomcat1{
		server 192.168.1.118:8080;
	 }
	 
	 upstream tomcat2{
		server 192.168.1.118:8081;
	 }
	
	 server {
        listen       80;
        server_name  www.mytest1.com;

        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	 server {
        listen       80;
        server_name  www.mytest2.com;

        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

保存

添加ip域名映射,这里添加主机的就好 一般在 C:\Windows\System32\drivers\etc.hosts 文件

测试访问:

到这里反向代理是完成了

3、负载均衡

3.1. 轮询实现:

编辑nginx.conf 修改 upstream tomcat1 添加8081

启动nginx访问 tomcat01页面:

可以看到 访问www.mytest1.com  刷新页面,访问tomcat01 tomcat02 两个项目以此出现,轮询默认采用1:1的比例。

3.2.权重:

修改nginx.conf 在每个tomcat地址后面添加权重比: 

重启nginx: ./nginx -s reload

重新访问www.mytest1.com

可以看到8081出现2次 8080出现一次  按2:1的比率访问

相关内容

    暂无相关文章