title: Nginx reverse proxy causing infinite loop
body: <p>I have the following in my Nginx site config file:</p>
  
  <pre><code>server {
      listen 80 default_server;
      listen [::]:80 default_server ipv6only=on;
  
      root /usr/share/nginx/html;
      index index.php index.html index.htm;
  
      server_name example.com;
  
      location / {
          try_files $uri $uri/ /index.php?q=$uri&amp;$args;
      }
  
      error_page 404 /404.html;
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
          root /usr/share/nginx/html;
      }
  
      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          fastcgi_index index.php;
          include fastcgi_params;
      }
  }
  
  
  server {
      listen 80;
      server_name example2.com;
  
      location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header Host $host;
          proxy_pass http://localhost/page-1/;
      }
  }
  </code></pre>
  
  <p>The issue only seems to occur when I add the <code>proxy_set_header Host $host;</code> line. It appears that the <code>$host</code> variable creates a redirect loop and the GET request from the error server logs goes something like this <code>...page-1/page-1/page-1/page-1...</code> with the server responding with an internal error 500.</p>
  
  <p>I'd be really grateful if anyone could tell me what I'm doing wrong. Many thanks in advance!</p>