0

I have setup a reverse proxy, sending requests from nginx (openresty) to apache, using this is only to allow more control through openresty to check the requests, but essentially the server is running on apache.

The problem is that apache is on port 8080, and nginx on 80, so whenever apache does a redirect it redirects to port 8080 and I have not found any conclusive way to prevent this and instead have it go to port 80.

My server config for nginx is the following

server {
  listen 80 default_server;
  server_name localhost;
  client_max_body_size 1024m;
  client_body_timeout 300s;

  location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

No lua added yet though.

As for Apache:

<VirtualHost *:8080>
  UseCanonicalName Off
  ServerName domain.com
  ServerAdmin web@domain.com
  DocumentRoot /home/user1/environments/production
</VirtualHost>

<VirtualHost *:8080>
  ServerAlias *.*.domain.com
  VirtualDocumentRoot /home/%2/environments/%1
</VirtualHost>

<VirtualHost *:8080>
  ServerAlias *.domain.com
  VirtualDocumentRoot /home/%1/environments/production
</VirtualHost>

This works but if apache does any sort of redirect it redirects though port 8080. I effectively want to close off port 8080 from being accessed through anything other then nginx, but through nginx apache should be running as if it were also on port 80.

  • I think you need the Nginx equivalent of Apache’s ProxyPassReverse – Cameron Kerr Nov 9 '17 at 3:55
  • That is effectively the proxy_set_header directives, but thank you, trying to see if there is more I can add to get it to work correctly. – Jordan Ramstad Nov 9 '17 at 17:39
0

I noticed after playing around that regardless of using the proxy in nginx or not, it would redirect.

This told me there must be some sort of caching interfering, but I had browser cache disabled the entire time.

So I am not entirely sure what fixed it, but all I updated was my location block as follows:

location / {
  proxy_redirect off;
  port_in_redirect off;
  proxy_pass http://127.0.0.1:8080;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $http_host;
  proxy_set_header X-Forwarded-Host $http_host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_connect_timeout 90;
  proxy_send_timeout 90;
  proxy_read_timeout 90;
  proxy_buffer_size 16k;
  proxy_buffers 32 8k;
  proxy_busy_buffers_size 64k;
}

This is after around 6-8 hours of scouring google and trying different things. After trying to clear it all out and just output hello world from nginx I seen it was still redirecting.

Trying an incognito window managed to stop the redirect, so if you try this solution do not depend on disabling cache, as it seems only incognito stops it from caching the redirect in this instance (at least in chrome 61). Also other updates to the server config takes effect, just not the redirect.

I am thinking

proxy_redirect off;
port_in_redirect off;
proxy_set_header X-Forwarded-Host $http_host;

Are the most likely to have resolved the original issue.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.