Search code examples
djangonginxgunicornussdipsec

Accessing a Django app on a servers private IP/vpn tunnel


I am trying to access a django app via a private ip, i configured a vpn site to site with another server (server2) so that the server2 could access the app via a private ip i created (192.xx.xx.xx) on server1. Now the tunnel is up and running but when server2 try to reach the django app on server1 through the private ip, the django app is not reached.

The app uses nginx as a web server and gunicorn as the application server. Below are both config files

nginx.conf

server {
listen 80;
server_name 197.xxx.xx.xx 192.xxx.xx.xx;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
client_max_body_size 20M;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /var/www/project_folder/project/settings;
}

location /media/ {
    root /var/www/project_folder;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/var/www/project_folder/project.sock;
}}

gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/project_folder
ExecStart=/var/www/env/bin/gunicorn --workers 3 --bind unix:/var/www/project_folder/project.sock project.wsgi:application

[Install]
WantedBy=multi-user.target

settings.py

ALLOWED_HOSTS = ['197.xxx.xx.xx','192.xxx.xx.xx']

In the above snippets the 197.xxx.xx.xx is the server1's public ip while 192.xxx.xx.xxis the server1's private ip. So server to is not able to call the django app through server1 private ip. I can see that serve2 is reaching server1 through nginx access logs but nothing in the nginx error logs.

1) I am not sure how to get around this since is my first time accessing a django app through private ip (vpn tunnels). Any advice or directions on how i can get server2 to reach the django app on server1 would be highly welcome.

2) suppose i have 2 apps in one django project, and i want app1 to be accessible through the server1's public IP, and app2 to be accessed through server1's private IP. is this something possible? if so how can i make it happen.

Thank you in advance, i would appreciate someone reaching out with the directions on how i can achieve the above Also is it


Solution

  • I managed to fix this, the issue was my understanding of how the vpn tunnel works and i was also giving a wrong port in Nginx to app that is on a private IP.

    I fixed this by assigning port 80 and creating a separate Nginx conf file for the app i want on a private IP, and i kept the also the one conf for the public IP app, they both listen to same port but different IPs.

    ie

    private ip app conf

    server {
    listen 80;
    server_name 192.xxx.xx.xx;
    access_log /var/log/nginx/access_ussd.log postdata;
    error_log /var/log/nginx/error_ussd.log;
    client_max_body_size 20M;
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/IPH_USSD/iph_ussd/settings;
    }
    
    location /media/ {
        root /var/www/IPH_USSD;
    }
    
    location / {
        include proxy_params;
        proxy_pass http://192.168.10.10:9000;
    }}
    

    public IP app conf

    server {
    listen 80;
    server_name xxx.xxx.xx.xx;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    client_max_body_size 20M;
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/IPH_Dashboard_App/ingabo/settings;
    }
    
    location /media/ {
        root /var/www/IPH_Dashboard_App;
    }
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/IPH_Dashboard_App/ingabo.sock;
    }}
    

    I hope this helps anyone facing a similar issue in the future