Solution to NGINX upstream servers HTTP keepalive enabled serving wrong URL and content

With HTTP keepalive enabled in NGINX upstream servers reduces latency thus improves performance and it reduces the possibility that the NGINX runs out of ephemeral ports. Following the suggestion given by NGINX doc that the proxy_http_version directive should be set to "1.1" and the "Connection" header field should be cleared:

  
upstream backend {
    server 127.0.0.1:8080;
    keepalive 16;
}
server {
  listen 80;
  server_name www.webfoobar.com;
  root /var/www/html;
  location /http/ {
    proxy_pass http://backend/index.php?q=$uri;    
    proxy_http_version 1.1;
    proxy_set_header Connection "";
  }
}
  

With this NGINX configuration, it serves wrong URL and content. It redirects the http://www.webfoobar.com to http://backend/index.php?q=/.

To solve this, add proxy_set_header directive in NGINX configuration to set the 'Host' header to the value in the client request or primary server name:

  
upstream backend {
    server 127.0.0.1:8080;
    keepalive 16;
}
server {
  listen 80;
  server_name www.webfoobar.com;
  root /var/www/html;
  location /http/ {
    proxy_pass http://backend/index.php?q=$uri;    
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header Host $host;
  }
}
  

Comments

Thank YOU SO MUCH!

I have been experimenting this one, as documentation did not tell anything about it, but telling disabling connection header..... I was debugging so I saw that bugged host...

You bring me the solution... thanks!

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.