Nginx反向代理Tomcat

系统环境:CentOS

生产环境:WDLINUX

WEB引擎:nginx+apache

1.在tomcat中创建虚拟主机

修改tomcatconfserver.xml,在<Engine>标签中加入<host>标签,如下所示:

<Host name="www.abc.com" appBase="/www/webapps" unpackWARS="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
    <Context docBase="/www/webapps/abc" path=""reloadable="true" sessionCookiePath="/" />
</Host>

2.在nginx中开启反向代理

2.1.打开wdlinux/nginx-xxx/conf/nginx.conf文件,在http花括号内的最后一行加入include vhost/*.conf。

2.2.在nginx-xxx/conf文件夹内创建proxy.conf,并在文件中加入以下内容:

 1 proxy_connect_timeout 30s;
 2 proxy_send_timeout   90;
 3 proxy_read_timeout   90;
 4 proxy_buffer_size    32k;
 5 proxy_buffers     4 32k;
 6 proxy_busy_buffers_size 64k;
 7 #proxy_redirect     off;
 8 proxy_hide_header  Vary;
 9 proxy_set_header   Accept-Encoding '';
10 proxy_set_header   Host   $host;
11 proxy_set_header   Referer $http_referer;
12 proxy_set_header   Cookie $http_cookie;
13 proxy_set_header   X-Real-IP  $remote_addr;
14 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

2.3.在nginx-xxx/conf/vhost文件夹内创建abc_com.conf,并在文件中加入以下内容:

 1     server {
 2         listen       80;
 3         server_name  www.abc.com;
 4         root /www/webapps/abc;
 5         index index.html index.htm;
 6 
 7         location / {
 8                   proxy_pass http://localhost:81;
 9                   proxy_cookie_path /abc/ /;
10                   include proxy.conf;
11         }
12         location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
13                 expires      30d;
14         }
15 
16         location ~ .*.(js|css)?$ {
17                 expires      12h;
18         }
19     }

配置完成后,重启服务,即可通过www.abc.com访问abc项目。

PS:必须使用命令行方式重启nginx服务才有效果,即:nginx-xxx/sbin/nginx -s reload。

原文地址:https://www.cnblogs.com/fanelephant/p/3817602.html