title: Nginx serve different files based on user login?
body: <p>I am using Node.js with nginx. My Node app is built on express and uses passport for authentication and is using sessions. My Node app is responding to JSON requests on all <code>/api</code> urls, and nginx is serving static files from a public directory.</p>
  
  <p>I want nginx to serve <code>index.html</code> at <code>/</code> when the user is not logged in, and serve <code>app.html</code> at <code>/</code> when the user is logged in.</p>
  
  <p>Here's my current nginx config.</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>How do I get nginx to determine if a user is logged in or not, and then serve a different file accordingly? I've noticed that express creates a cookie called <code>connect.sid</code>, but that doesn't seem to go away when the user logs out.</p>