0

I am running an nginx reverse proxy with apache as a backend server. I have multiple applications running without any problems, but some php-js websites fails to redirect properly. when I sent a request to example.com/admin it should redirects to example.com/admin/ but instead I get a redirection to example.com:internal_port/admin/.

here is some useful information:

  • Nginx configuration:

     server {
         server_name example.com;
         root /var/www/html/;
         index index.php index.html index.htm;
         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:8080;
         proxy_read_timeout 600s;
    
         }
    
          location ~ /\.ht {
                 deny all;
         }
    
         listen 80;
     }
    
  • apache configuration:

     <VirtualHost localhost:8080>
    
     DocumentRoot /var/www/html/example.com/
     ServerName localhost:8080
     LogLevel debug
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
    
     </VirtualHost>
    
  • Http headers when requesting from backend server:


$ curl localhost:8080/admin -v

 GET /admin HTTP/1.1
 Host: localhost:8080
 User-Agent: curl/7.58.0
 Accept: */*

 HTTP/1.1 301 Moved Permanently
 Date: Wed, 06 Jul 2022 17:59:37 GMT
 Location: http://localhost:8080/admin/
 Content-Length: 313
 Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://localhost:8080/admin/">here</a>.</p>
<hr>
</body></html>
  • http headers when requesting the virtual host:

$ curl example.com/admin -v


> GET /admin HTTP/1.1
> Host: example.com
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.14.0 (Ubuntu)
< Date: Thu, 07 Jul 2022 09:13:15 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 325
< Connection: keep-alive
< Location: http://example.com:8080/admin/
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://example.com:8080/admin/">here</a>.</p>
<hr>

3
  • Most probably your backend server is configured with the wrong base URL and is generating the redirect. Jul 6, 2022 at 19:16
  • In your question you show response from the apache (curl localhost:8080/admin -v), not from the nginx (curl localhost/admin -v). The implicit proxy_redirect default directive should made nginx removing that port from the apache response; if it doesn't happen, most likely you have an explicit proxy_redirect off directive on the upper nginx configuration level. Jul 6, 2022 at 19:50
  • @IvanShatsky I will edit my question and add a curl for what you have asked for.
    – A. Bader
    Jul 7, 2022 at 9:04

1 Answer 1

0

I just added the following line to nginx config in the server block:

        rewrite /admin$ $scheme://$host$uri/ permanent;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .