Does nginx serve different files based on user login?

I am using Node.js with nginx. My Node application is built on express and uses a passport for authentication and uses sessions. The Node application responds to JSON requests on all URLs /api, while nginx serves static files from a shared directory.

I want to nginx filed index.htmlin /when the user is not logged in, and is app.htmlunder /the user logged on.

Here is my current nginx configuration.

upstream app_upstream {
      server 127.0.0.1:3000;
      keepalive 64;  
}

server {
            listen 0.0.0.0:80;
            server_name app.com default;
            access_log /var/log/nginx/app.com.access.log;
            error_log /var/log/nginx/app.com.error.log debug;

            root /home/app/app/public;

    location /api {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;

            proxy_cache_bypass 1;
            proxy_no_cache 1;
            expires off;
            if_modified_since off;
            add_header Last-Modified "";

            proxy_pass http://app_upstream;
            proxy_redirect off;
    }

    location / {
            access_log off;
            expires max;
    }
}

How do I get nginx to determine if a user is registered or not, and then submit another file? I noticed that express creates a cookie called connect.sid, but it doesn't seem to disappear when the user logs out.

+4
1

, , X-Accel-Redirect, nginx .

:

if (user.isLoggedIn) {
    response.setHeader("X-Accel-Redirect", "/app.html");
} else {
    response.setHeader("X-Accel-Redirect", "/index.html");
}
response.end();

/ .

location = / {
    # copy from location /api
}
+4

Source: https://habr.com/ru/post/1539089/


All Articles