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

Source: Internet
Author: User
: This article mainly introduces how to solve the problem of session loss from proxy_pass to tomcat in nginx. if you are interested in PHP tutorials, refer to it. Introduction

I have always used nginx1.5.7 as the web server and proxy server. it has always been an application that corresponds to one Tomcat, that is, one port, and only one domain name.
Today, Tomcat on the server is integrated. five applications share one Tomcat.
The first problem occurred when a user enters the background. the user information cannot be found!
After debugging, it was found that the request was changed to multiple requests, and the session was inconsistent. it felt like another browser was accessing the request. The conclusion was that the session was definitely lost!

Cause

After careful analysis, we can conclude that the problem lies in the Nginx configuration!

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

This configuration will cause the cookie storage location to be not based on "/", so the session will be created again during the second access, so the information in the session is lost.

Solution

Modify the cookie storage path

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

Restart the service and test it!
Pass!

The above describes how to solve the problem of session loss from proxy_pass to tomcat in nginx, including some content, and hope to help friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.