nginx反向代理+tomcat(nginx转发规则和实现负载均衡)

来源:互联网 发布:历史停车数据分析 编辑:程序博客网 时间:2024/06/05 17:58

有时候想通过nginx实现反向代理,分别根据不同的url映射到不同的tomcat服务器,已达到在一台服务器挂多个网站和应用的目的。


1)最简单的就是修改下nginx安装目录的nginx.conf文件:

原来的文件一部分是:

 server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            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;        #}    }
修改后的为:

 server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location ^~ /Express/ {            proxy_pass   http://127.0.0.1:8080/;            proxy_redirect off;            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;        #}    }
其实就是修改了server节点中的location节点的内容。

上面实现了把有关Express的所有url映射到 http://127.0.0.1:8080了。也就是给tomcat了,成功实现了功能。

上面的server可以有多个(多个server域)。如果有server_name就需要指定了。默认的是localhost,可以是 server_name  www.test.com;或者  server_name  xxx.test.com;也就是二级域名的时候

2)

假设本地布置了3台tomcat。ulr访问分别为/tomcat/6/、/tomcat/7/、/tomcat/8/。那么通过nginx实现负载均衡转发就应该这样写:

 location ^~ /tomcat/6/ {        proxy_pass   http://127.0.0.1:8686/;        proxy_redirect off;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    }    location ^~ /tomcat/7/ {        proxy_pass   http://127.0.0.1:8787/;        proxy_redirect off;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    }    location ^~ /tomcat/8/ {        proxy_pass   http://127.0.0.1:8888/;        proxy_redirect off;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    }
这样,通过访问http://xxx.xxx.xxx.xxx/tomcat/6/ 就可以直接转发到127.0.0.1的8686端口了,方便做测试。
另外^~ /tomcat/6/代表匹配URL以/tomcat/6/开头,需要计算大小写。


3)

反向代理适用于很多场合,负载均衡是最普遍的用法。

nginx 作为目前最流行的web服务器之一,可以很方便地实现反向代理。

nginx 反向代理官方文档: NGINX REVERSE PROXY

当在一台主机上部署了多个不同的web服务器,并且需要能在80端口同时访问这些web服务器时,可以使用 nginx 的反向代理功能: 用 nginx 在80端口监听所有请求,并依据转发规则(比较常见的是以 URI 来转发)转发到对应的web服务器上。

例如有 webmail , webcom 以及 webdefault 三个服务器分别运行在 portmail , portcom , portdefault 端口,要实现从80端口同时访问这三个web服务器,则可以在80端口运行 nginx, 然后将 /mail 下的请求转发到 webmail 服务器, 将 /com下的请求转发到 webcom 服务器, 将其他所有请求转发到 webdefault 服务器。

假设服务器域名为example.com,则对应的 nginx http配置如下:

http {    server {            server_name example.com;            location /mail/ {                    proxy_pass http://example.com:protmail/;            }            location /com/ {                    proxy_pass http://example.com:portcom/main/;            }            location / {                    proxy_pass http://example.com:portdefault;            }    }}

以上的配置会按以下规则转发请求( GET 和 POST 请求都会转发):

  • 将 http://example.com/mail/ 下的请求转发到 http://example.com:portmail/
  • 将 http://example.com/com/ 下的请求转发到 http://example.com:portcom/main/
  • 将其它所有请求转发到 http://example.com:portdefault/

需要注意的是,在以上的配置中,webdefault 的代理服务器设置是没有指定URI的,而 webmail 和 webcom 的代理服务器设置是指定了URI的(分别为 / 和 /main/)。 
如果代理服务器地址中是带有URI的,此URI会替换掉 location 所匹配的URI部分。 
而如果代理服务器地址中是不带有URI的,则会用完整的请求URL来转发到代理服务器。

官方文档描述:

If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. 
If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

以上配置的转发示例:

  • http://example.com/mail/index.html -> http://example.com:portmail/index.html
  • http://example.com/com/index.html -> http://example.com:portcom/main/index.html
  • http://example.com/mail/static/a.jpg -> http://example.com:portmail/static/a.jpg
  • http://example.com/com/static/b.css -> http://example.com:portcom/main/static/b.css
  • http://example.com/other/index.htm -> http://example.com:portdefault/other/index.htm

参考连接:http://blog.bbzhh.com/index.php/aboutme.html

http://blog.csdn.net/tobacco5648/article/details/51099426




0 0
原创粉丝点击