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

Mysql date formatting and complex date range query

Table of contents Preface Query usage scenario ca...

Detailed method of using goaccess to analyze nginx logs

Recently I want to use goaccess to analyze nginx ...

Talk about implicit conversion in MySQL

In the course of work, you will encounter many ca...

Detailed explanation of the difference between docker-compose ports and expose

There are two ways to expose container ports in d...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...

What to do if you forget the initial password of MySQL on MAC

The solution to forgetting the initial password o...

Sample code for implementing 3D book effect with CSS

Without further ado, let's take a look at the...

Markup language - web application CSS style

Click here to return to the 123WORDPRESS.COM HTML ...

CentOS 8 officially released based on Red Hat Enterprise Linux 8

The CentOS Project, a 100% compatible rebuild of ...

How to use provide to implement state management in Vue3

Table of contents Preface How to implement Vuex f...

Solution to the problem that the image name is none after Docker load

Recently, I found that after using the docker loa...

Docker container time zone error issue

Table of contents background question Problem ana...