使用nginx(https)为tomcat(http)做反向代理

使用nginx(https)为tomcat(http)做反向代理

 

   公司使用tomcat部署了一套服务,需要通过互联网对外提供服务。鉴于安全考虑,公司要求使用https对外提供服务,在配置nginx反向代理时,通过http为tomcat做反向代理时测试正常;通过https为tomcat做反向代理时,要么跳转不到tomcat站点,要么在tomcat站点的各个页面之间无法跳转。通过网上搜索,折腾一天后,终于找到了解决方法(如果tomcat也配置成https,nginx使用https也可以正常为tomcat做反向代理的),现在无需将tomcat配置为https,即可实现nginx(https)为tomcat(http)做反向代理。

配置情景如下:

    Tomcat的访问地址为:http://192.168.1.23:8080/yyt-ort

    Nginx的地址为:https://192.168.1.25:2443

    Nginx为tomcat做反向代理后的对外地址为:

  通过本地访问:https://192.168.1.25:2443/yyt-ort

  通过域名访问:https://www.xxxxx.com:1234/yyt-ort

1、nginx的配置如下:

    server {

        listen       2443 ssl;
        server_name  www.xxxxx.com:2443;

        ssl                  on;
        ssl_certificate         cert/cert.crt;
        ssl_certificate_key     cert/cert.key;
        ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        proxy_redirect http:// $scheme://;
        port_in_redirect on;
 
        location ~ ^/yst-orp {
               proxy_pass http://192.168.1.23:8081;
               proxy_set_header  Host $host:$server_port;
               proxy_set_header  Host             $host;
               proxy_set_header  X-Real-IP        $remote_addr;
               proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

2、nginx中的主要配置说明

Nginx(https)能够为tomcat(http)反向代理成功,主要是通过三个配置参数实现的

(1)proxy_redirect参数;

  这个参数主要用来改从被代理服务器传来的应答头中的"Location"和"Refresh"字段。

  配置语法为:proxy_redirect [ default|off|redirect replacement ]

  默认的值为:proxy_redirect default 

  可使用的标签:http,server,location

  在此处配置为proxy_redirect http:// $scheme://;的作用是将从tomcat中返回的http修改为https。

官方文档地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

(2)port_in_redirect参数;

  这个参数的作用是启用或禁用在nginx发布的绝对重定向中指定端口。

  配置语法:port_in_redirect [off|on]

  默认值:port_in_redirect on

  可使用此配置的标签:http,server,location

  此处配置为port_in_redirect on,作用是在nginx反向代理跳转到tomcat时将跳转到的端口替换为nginx监听的端口。

官方文档地址:http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect

(3)proxy_set_header参数

  这个参数的作用是允许重新定义或者添加发往后端服务器的请求头。value可以包含文本、变量或者它们的组合。 当且仅当当前配置级别中没有定义proxy_set_header指令时,会从上面的级别继承配置。 默认情况下,只有两个请求头会被重新定义:

proxy_set_header Host $proxy_host;

proxy_set_header Connection close;

配置语法格式:port_in_redirect field value

可使用的标签:http,server,location

  此处设置为proxy_set_header  Host $host:$server_port;的作用是将请求的报文的头部的客户端的ip更改为当前作为反向代理的nginx的监听的ip及端口,这样后端的tomcat处理完请求时返回给nginx,再由nginx代理将结果返回给客户端。不然,后端服务处理完请求会直接返回到客户端,这样会导致客户端请求的https跳转为http,以及请求超时。

官方文档地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

3 绑定域名,通过域名访问

  配置完成后,在本地通过nginx配置的https可以正常访问(配置了https,通过ip加端口的方式访问时提示安全性问题是正常的),但是通过对外提供的域名加端口不能正常访问。

  这是由于nginx配置中proxy_set_header参数的设置所引起的,proxy_set_header设置为Host $host:$server_port,这样导致将客户端请求报文头部的源地址修改为nginx本地监听地址加端口即:(192.168.1.25):2443,,在内外端口不一致时对外将请求结果返回给客户端时目标地址将变为:域名(www.xxxxx.com):2443,这样导致客户通过域名访问时输入的地址www.xxxxx.com:1234变为www.xxxxx.com:2443。解决这个问题只需更改配置(修改匹配值proxy_set_header  Host $host:$server_port;)为:

proxy_set_header  Host www.xxxxx.com:1234;

  这样设置的作用就是,服务端处理完请求,将处理结果原路返回,最终由www.xxxxx.com:1243代理服务端发送给客户端。这样设置后本地直接通过nginx主机访问时也会跳转为域名地址。

注意:在通过域名加端口的方式测试时,nginx对外映射的地址和自己测试的终端的出口地址不能为同一公网ip,否则。本地通过域名测试时无法访问。

猜你喜欢

转载自blog.csdn.net/dayi_123/article/details/79753018