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
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
Related
so I have a ubuntu server that run two different website with two different domain:
www.firstwebsite.com
www.secondwebsite.com
But when I create an AAA record to create a subdomain with the first domain (like this)
demo.firstwebsite.com
If I go to this subdomain it automatically run the application website of my other domain (www.secondwebsite.com)
I tried creating a specific socket&service file for the subdomain for it but it still run the web application of the second domain.
Im not sure what is causing that and how to fix that? thank you
nginx config file
server {
listen 80;
server_name firstwebsite.com;
return 301 $scheme://www.firstwebsite.com$request_uri;
}
server {
listen 80;
server_name www.firstwebsite.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/firstwebsite;
}
location /media/ {
root /var/www/firstwebsite;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/firstwebsite.sock;
}
}
I'm trying to connect Django back-end to a React build provided to me by the front-end developer. I'm using Gunicorn for Django and Web server is Nginx.
The below config file is a result of extensive Googling.
Currently Django back-end works on port 80/8000 but whenever I change the port to anything else like 8001 below, the server does not respond.
The complete thing is running on Google Ubuntu VM.
I've executed sudo ufw disable for testing purposes.
server {
#listen 80;
listen 8001;
listen [::]:8001;
server_name xx.xx.7.xx;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location /static/ {
root /home/username/cateringm;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
#location / {
# try_files $uri $uri/cm_react_build/build/index.html; # this is where you serve the React build
# }
}
server {
listen 8002;
listen [::]:8002;
server_name xx.xx.7.xx;
root /home/username/cm_react_build/build;
index index.html index.htm;
location /static/ {
root /home/username/cm_react_build/build;
}
location /test {
root /home/username/cm_react_build/build;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
}
I'm new to configuring web servers. Help would be appreciated.
I found the problem. Google by default blocks all ports except port 80. I updated the firewall rules for different ports and the requests were going through.
I have been trying to auto transfer all requests to https protocol using nginx in django in ec2 instance but i am unable to do so.. here is my nginx file..
please suggest me the problem.
nginx file
server{
listen 443 ssl;
server_name www.priyamarya.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/project/aryapriyam/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/project/aryapriyam/project.sock;
}
}
server{
listen 80;
server_name priyamarya.com;
return 301 https://www.priyamarya.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/project/aryapriyam/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/project/aryapriyam/project.sock;
}
}
i have also added this in settings.py
settings.py
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT =True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/project/aryapriyam
ExecStart=/home/ubuntu/project/venv/bin/gunicorn --access-logfile - --
workers 3 --chdir /home/ubuntu/project/aryapriyam/ --bind
unix:/home/ubuntu/project/aryapriyam/project.sock
project.wsgi:application
[Install]
WantedBy=multi-user.target
i have also set my hostedzone A type record set to the alias provided by elb load balancer.
i have tried a lot of things like returning the https://sitename , and creating a different server block for both protocols but than it starts a loop in between requests. this is why i am posting the code from which i initially started. i have searched a lot but nothing is helping in regards of nginx and django both, please help.. i want all my forms request also to go through https only.
You need to add other server block for ssl and use following config for ssl
This config also redirects http request to https (i.e ssl port 443)
server {
listen 80;
server_name testing.com;
return 301 https://testing.com;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/sample_project/sample_project.sock;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name testing.com;
ssl on;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
location /static/ {
root /home/ubuntu/sample_project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/sample_project/sample_project.sock;
}
}
I am setting up a production server with Angular serving the front end and Django on the back. I got Nginx serving Angular properly but any requests to the backend dont go through and just time out.
I have Nginx serving Angular on port 80 and then Django on port 8800
This is the code I have in place for Django
server {
listen 8800;
server_name ADDRESS;
location = /favicon.ico {access_log off;log_not_found off;}
location = /static/ {
root /home/ubuntu/django/dbsystem;
}
location = /media/ {
root /home/ubuntu/django/dbsystem;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/django/dbsystem/dbsystem.sock;
}
}
This is the code I have in place for Angular
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ /index.html;
}
}
This is the code for Gunicorn
[Unit]
Description=gunicorn service
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/django/dbsystem/
ExecStart=/home/ubuntu/django/bin/gunicorn --access-logfile - --workers 3 -- bind unix:/home/ubuntu/django/dbsystem/dbsystem.sock dbsystem.wsgi:application
[Install]
WantedBy=multi-user.target
If I go to ADDRESS it pulls up Angular which is expected
If I go to ADDRESS/suburl it pulls up the appropriate Angular Route as expected
If I go to ADDRESS:8800 it loads up the Django (Not found) page since its in debug mode which is expected
If I go to ADDRESS:8800/suburl it waits a bit and goes into timeout which is the issue I am trying to solve. This is for both the /media route and all other routes. I dont have/use the static folder route I defined
There is a problem with location = in you nginx configuration. Please remove = and it would work.
I am using GoDaddy domain and connectiong it to django+gunicorn+nginx droplet hosted on DigitalOcean.
After all tried configs of DO, nginx, etc., I'm still getting the same result.
When I'm trying to connect via domain, it changes it to IP address and proceeds...
Last nginx config I stopped on is:
server {
listen 80;
server_name example.com www.example.com ip.ip.ip.ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/webapp;
}
location /media/ {
root /home/user/webapp;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/webapp/webapp.sock;
}
}
Removing IP completely from nginx conf results in same behavior but instead of getting Django webpage I'm getting redirected to nginx default webpage and the domain is again changed to IP.
Try using this statement
location \homepage {
rewrite ^ http://$host$request_uri? permanent;
}