Home  >  Article  >  Backend Development  >  Solve the problem of session loss from proxy_pass to tomcat in nginx

Solve the problem of session loss from proxy_pass to tomcat in nginx

WBOY
WBOYOriginal
2016-08-08 09:21:521266browse

Introduction

I have always used nginx1.5.7 as a web server and proxy server. One application has always corresponded to one Tomcat, which is one port, and only one domain name.
Today, I integrated Tomcat on the server. Five applications share one Tomcat.
The first problem that arises is that an exception occurs when the user waits to enter the background, and the logged-in user information cannot be found!
After debugging, it was found that there were multiple request requests, and the sessions were inconsistent. It felt like another browser was accessing, and we concluded that the session must be lost!

Cause

After careful analysis, we came to the conclusion: the problem lies in the configuration of Nginx!

<code>server_name  www<span>.</span>weixin4j<span>.</span>org;
charset utf<span>-</span><span>8</span>;
root   /opt/apache<span>-tomcat</span><span>-</span><span>7.0</span><span>.53</span>/webapps/weixin4j<span>/</span>;
location <span>/</span> {
    proxy_pass         http:<span>//127.0.0.1:8180/weixin4j/;</span>
    proxy_set_header   Host             <span>$host</span>;
    proxy_set_header   X<span>-Real</span><span>-IP</span><span>$remote_addr</span>;
    proxy_set_header   X<span>-Forwarded</span><span>-For</span><span>$proxy_add_x_forwarded_for</span>;
}</code>

This configuration will cause the cookie storage location is not based on "/", so the session will be re-created on the second visit, so the information in the session will be lost.

Solution

Modify the storage path of cookies

<code>server_name  www<span>.</span>weixin4j<span>.</span>org;
charset utf<span>-</span><span>8</span>;
root   /opt/apache<span>-tomcat</span><span>-</span><span>7.0</span><span>.53</span>/webapps/weixin4j<span>/</span>;
location <span>/</span> {
    proxy_pass         http:<span>//127.0.0.1:8180/weixin4j/;</span>
    proxy_set_header   Host             <span>$host</span>;
    proxy_set_header   X<span>-Real</span><span>-IP</span><span>$remote_addr</span>;
    proxy_set_header   X<span>-Forwarded</span><span>-For</span><span>$proxy_add_x_forwarded_for</span>;
    add_header From www<span>.</span>weixin4j<span>.</span>org;
    proxy_cookie_path /weixin4j<span>/</span><span>/</span>;
    proxy_set_header Cookie <span>$http_cookie</span>;
}</code>

Restart the service and test!
pass!

The above introduces how to solve the session loss problem from proxy_pass to tomcat in nginx, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn