这篇文章的前提是已经配置好了NGINX,而且tomcat已经配置好了,而且能能够访问了。
然后使用nginx反向代理
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://ip:18080/;
index index.html index.htm;
}
location /my{
proxy_pass http://ip:28080/;
}
如上配置时没有问题的访问(http://ip:80),你可以通过nginx访问两个的tomcat,但是如果你想访问(http://ip:80)根据负载均衡跳转,则要使用upstream,我卡在这里很久了,因为我一配置upstream就404的错,而且配置也没有错
经过多次百度,一般都是这样配的,不过我在本地这样配置使用也是可以用的,但是在linux上就不行了,没改配置文件能访问nginx首页,以及直接访问一个tomcat是可以的,但是就是不能负载均衡,但是我就是跳转不到tomcat,后来无意中增加了一个proxy_set_header Host $host;
就能跳转了,虽然不知道原理,但是还是要记录一下这个问题,以便以后自己查看
upstream tomcats{
ip_hash;
server ip:18080 weight=2 max_fails=3 fail_timeout=3;
server ip:28080 weight=1 max_fails=3 fail_timeout=3;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcats;
proxy_set_header Host $host;
index index.html index.htm;
}