The reverse proxy does not direct to a specific vhost

gusto

New Member
Joined
Apr 20, 2021
Messages
2
Reaction score
1
Credits
24
I am using a nginx reverse proxy server. Behind the reverse proxy are 3 apache servers on 3 IP addresses
Code:
apache1 - 192.168.1.101 (4x vhost)
apache2 - 192.168.1.106 (1x vhost)
apache3 - 192.168.1.107 (1x vhost)
nginx   - 192.168.1.105
apache 2 and apache 3 work great.

This is the nginx configuration for apache2

/etc/nginx/sites-available/www.domain1.tld
Code:
server {
    listen 80;
    server_name domain1.tld;
    return 301 http://www.domain1.tld$request_uri;
}

server {
    listen 80;
    server_name www.domain1.tld;

    location / {
        proxy_pass http://192.168.1.106;
        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;
    }
}

This is the apache2 configuration

/etc/apache2/sites-available/www.domain1.tld.conf
Code:
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName domain1.tld
    ServerAlias www.domain1.tld
    DocumentRoot /var/www/html/www.domain1.tld/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I created a similar configuration for apache1

/etc/nginx/sites-available/www.domain2.tld
Code:
server {
    listen 80;
    server_name domain2.tld;
    return 301 http://www.domain2.tld$request_uri;
}

server {
    listen 80;
    server_name www.domain2.tld;

    location / {
        proxy_pass http://192.168.1.101;
        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;
    }
}

/etc/apache2/sites-available/www.domai2.tld.conf

Code:
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName domain2.tld
    ServerAlias www.domain2.tld
    DocumentRoot /var/www/html/www.domain2.tld/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

When I paste an address domain1.com into a web browser, I see the correct content. When I paste an address domain2.com into a web browser, I see
Code:
Not Found

The requested URL was not found on this server.
Apache/2.4.25 (Debian) Server at www.domain2.tld Port 80
Nginx will probably forwarded it to the correct server, but it will not forwarded it to a specific vhost.
I think that apache2 and apache3 work correctly because they contain only 1 vhost. Apache1 contains 4 vhosts and therefore it does not work.
I tried to set the proxy_pass directive to the root directory

Code:
proxy_pass http://192.168.1.101/www.domain2.tld;
However, nothing has changed.
Can anyone advise me why a reverse proxy does not route specific hosting requests?

I'm sorry for my english
 
Last edited:


Here's something you could try and if that doesn't work you can also try the following, on each apache webserver have every virtual host run on a different port. Then in your nginx configuration have it proxypass to the ip and port of that specific virtualhost, for example domain1.
Code:
<VirtualHost *:8090>
    ServerAdmin [email protected]
    ServerName domain1.tld
    ServerAlias www.domain1.tld
    DocumentRoot /var/www/html/www.domain1.tld/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Code:
server {
    listen 80;
    server_name domain1.tld;
    return 301 http://www.domain1.tld$request_uri;
}

server {
    listen 80;
    server_name www.domain1.tld;

    location / {
        proxy_pass http://192.168.1.106:8090;
        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;

    }

}
This way you can have your nginx configuration pointing to the correct virtualhost by making it point on basis of port and if that doesn't work you could still trying setting ip based virtualhosts instead of name based virtualhosts. You would first then need to add extra ip addresses to the systems which looks like you can do since you are using private ip adresses on the apache systems.
 
Last edited:
3x mea culpa :D
The error was in the documentroot
Code:
DocumentRoot /var/www/html/www.domain2.tld/
Code:
DocumentRoot /var/www/www.domain2.tld/
 
Impossible for me to find that error without knowing your directory structure, that is why I was assuming else and just suggesting things you could have tried. Glad that you find the error yourself!
 


Top