在服务器私有 IP/vpn 隧道上访问 Django 应用程序

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

我正在尝试通过私有 ip 访问 django 应用程序,我将 vpn 站点配置为与另一台服务器 (server2) 站点,以便 server2 可以通过我创建的私有 ip 访问该应用程序 (192.xx.xx.xx) 在服务器 1 上。现在隧道已经启动 运行 但是当 server2 尝试通过私有 ip 访问 server1 上的 django 应用程序时,django 应用程序无法访问。

该应用程序使用 nginx 作为网络服务器,使用 gunicorn 作为应用程序服务器。 下面是两个配置文件

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']

在上面的代码片段中,197.xxx.xx.xx 是 server1 的 public ip,而 192.xxx.xx.xx 是 server1 的私有 ip。因此服务器无法通过 server1 私有 ip 调用 django 应用程序。我可以看到 serve2 通过 nginx 访问日志到达 server1 但 nginx 错误日志中没有任何内容。

1) 我不确定如何解决这个问题,因为这是我第一次通过私有 ip(vpn 隧道)访问 django 应用程序。非常欢迎任何关于如何让 server2 到达 server1 上的 django 应用程序的建议或指导。

2) 假设我在一个 Django 项目中有 2 个应用程序,我希望可以通过服务器 1 的 public IP 访问应用程序 1,并通过服务器 1 的私有 IP 访问应用程序 2。这有可能吗?如果可以,我该如何实现。

提前谢谢你,如果有人能指导我如何实现上述目标,我将不胜感激 也是吗

我设法解决了这个问题,问题是我对 vpn 隧道如何工作的理解,我还在 Nginx 中为私有 IP 上的应用程序提供了错误的端口。

我通过分配端口 80 并在私有 IP 上为我想要的应用程序创建一个单独的 Nginx conf 文件来解决这个问题,我还为 public IP 应用程序保留了一个 conf,他们都听到相同的端口但 IP 不同。

私有 ip 应用配置

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 应用程序配置

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;
}}

我希望这对以后遇到类似问题的人有所帮助