Nginx反向代理Tomcat


系統環境:CentOS

生產環境:WDLINUX

WEB引擎:nginx+apache

1.在tomcat中創建虛擬主機

修改tomcat\conf\server.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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM