arp
arp

Reputation: 11

nginx reverse proxy redirects to internal ip address

I have a nginx as reverse proxy server and apache to server nextcloudpi web application.

i have the following as nginx config

server {

server_name drive.example.com;

location / {
proxy_pass http://192.168.0.7/;
proxy_set_header X-Real-IP  $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/drive.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/drive.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = drive.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


listen 80;
listen [::]:80;

server_name drive.example.com;
    return 404; # managed by Certbot


}

and the following as apache config

<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    DocumentRoot /var/www/nextcloud
ServerName drive.example.com
    CustomLog /var/log/apache2/nc-access.log combined
    ErrorLog  /var/log/apache2/nc-error.log
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/drive.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/drive.example.com/privkey.pem
  </VirtualHost>
  <Directory /var/www/nextcloud/>
    Options +FollowSymlinks
    AllowOverride All
    <IfModule mod_dav.c>
      Dav off
    </IfModule>
    LimitRequestBody 0
    SSLRenegBufferSize 10486000
  </Directory>
</IfModule>

Note: Previously, i had apache as the direct front end to the internet and now i want to use nginx as the front end and apache still as the web application server

Any help is appreciated if i can reach drive.example.com without redirecting to internal ip address?

Thank you.

Upvotes: 1

Views: 6302

Answers (1)

Ridwan
Ridwan

Reputation: 387

It seems like you need to disable your proxy redirect headers, try changing and updating your configuration file for nginx (the reverse proxy), this will make sure that your nginx runs as a middle man between the apache server and the client, (instead of the nginx just offloading the client to the apache server with a redirect and not acting as a middle man):

server {
listen 80;
listen [::]:80; # if you're not using ipv6 do remove this line.    
server_name drive.example.com;

location / {
    proxy_redirect              off;
    proxy_read_timeout          1m;
    proxy_connect_timeout       1m;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
    proxy_pass http://192.168.0.7/;    
}

    listen [::]:443 ssl; # if you're not using ipb6 do remove this line
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/drive.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/drive.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = drive.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot




server_name drive.example.com;
    return 404; # managed by Certbot   
}

Upvotes: 1

Related Questions