Solution to Nginx session loss problem

Solution to Nginx session loss problem

In the path of using nginx as a reverse proxy tomcat, session loss may occur. Each time a request is sent, the JESSIONID will change, indicating that the last session is lost and a new session is created.

First case:

 server{
    listen 80;
    server_name www.jiahemdata.com www.jiahemdata.cn;
    charset utf-8;
    location /{
      proxy_redirect off;
      proxy_pass http://127.0.0.1:8093;
      proxy_set_header Host $host;
      proxy_set_header Referer $http_referer;
      proxy_set_header X-Real-Ip $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    access_log logs/tomcat_access.log;
  }

Since the current nginx only listens to one port and does not set the path, the problem of session loss generally does not occur.

Second case:

 server{
    listen 80;
    server_name www.jiahemdata.com www.jiahemdata.cn;
    root /opt/tomcat-jhyx/webapps/jhyx/;
    charset utf-8;
    location /{
      proxy_pass http://127.0.0.1:8093/jhyx/;
      proxy_set_header Host $host;
      proxy_set_header Referer $http_referer;
      proxy_set_header X-Real-Ip $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    access_log logs/tomcat_access.log;
  }

In this case, the tomcat folder is specified, not just a port listening, which will cause changes in each request and result in session loss.

The third case:

 server{
    listen 80;
    server_name www.jiahemdata.com www.jiahemdata.cn;
    root /opt/tomcat-jhyx/webapps/jhyx/;
    charset utf-8;
    location /{
      proxy_redirect off;
      proxy_pass http://127.0.0.1:8093/jhyx/;
      proxy_cookie_path /jhyx/ /; //Set the cookie path so that it does not change every time a request occurs.
      proxy_cookie_path /jhyx/;
      proxy_set_header Host $host;
      proxy_set_header Referer $http_referer;
      proxy_set_header X-Real-Ip $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    access_log logs/tomcat_access.log;
  }

At this time, you find that your problem is still not solved. At this time, you are thinking, I have clearly set the cookie path, why doesn’t it work? That’s because you didn’t send the cookie when you requested.

The fourth case:

 server{
    listen 80;
    server_name www.jiahemdata.com www.jiahemdata.cn;
    root /opt/tomcat-jhyx/webapps/jhyx/;
    charset utf-8;
    location /{
      proxy_redirect off;
      proxy_pass http://127.0.0.1:8093/jhyx/;
      proxy_cookie_path /jhyx/ /;
      proxy_cookie_path /jhyx/;
      proxy_set_header Host $host;
      proxy_set_header Referer $http_referer;
      proxy_set_header Cookie $http_cookie; //Carry cookie information when sending request proxy_set_header X-Real-Ip $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    access_log logs/tomcat_access.log;
  }

I hope you can find a correct solution on the Internet.

This is the end of this article about how to solve the Nginx session loss problem. For more information about Nginx session loss, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Springsession nginx reverse proxy integration process
  • Analysis of the solution to Nginx Session sharing problem
  • Detailed explanation of two ways to implement session persistence in Nginx reverse proxy
  • Implementation of Session Management with Nginx+Tomcat
  • nginx+redis realizes session sharing
  • nginx+tomcat implements load balancing and uses redis session sharing
  • Example of shared session configuration method in Nginx
  • Nginx load balancing multi-site shared session

<<:  MySQL stored procedure method example of returning multiple values

>>:  How to implement Ajax concurrent request control based on JS

Recommend

Using js to achieve the effect of carousel

Today, let's talk about how to use js to achi...

How to set mysql5.7 encoding set to utf8mb4

I recently encountered a problem. The emoticons o...

Solution to Tomcat server failing to open tomcat7w.exe

I encountered a little problem when configuring t...

Vue echarts realizes dynamic display of bar chart

This article shares the specific code of vue echa...

The new version of Chrome browser settings allows cross-domain implementation

Preface Currently, the front-end solves cross-dom...

How to view and optimize MySql indexes

MySQL supports hash and btree indexes. InnoDB and...

Summary of the most commonly used knowledge points about ES6 new features

Table of contents 1. Keywords 2. Deconstruction 3...

The whole process of node.js using express to automatically build the project

1. Install the express library and generator Open...

CSS to achieve compatible text alignment in different browsers

In the front-end layout of the form, we often nee...

Tutorial on Migrating Projects from MYSQL to MARIADB

Prepare the database (MySQL). If you already have...

Usage instructions for the docker create command

The docker create command can create a container ...

How to view the type of mounted file system in Linux

Preface As you know, Linux supports many file sys...