Django server not opening with 0.0.0.0:8000 - django

I have a AWS server runs on Nginx and which hosts a React application working fine on server.
Now I want a Django app for restframework to be available on the same server.
Iam following the Document and uploaded the Django app on the server and try to run the app by trying python3 manage.py runserver 0.0.0.0:8000. There is no error but I cannot access my ip with http://server_domain_or_IP:8000.
Nginx config at /etc/nginx/conf.d/example.org.conf
server {
listen 80 default_server;
server_name example.org;
return 301 https://example.org;
}
server {
listen 443 ssl;
server_name example.org;
ssl_certificate /etc/nginx/ssl/ssl_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
location / {
root /home/ubuntu/example/build;
index index.html index.htm;
}
}
Please help where am I going wrong?

So your Django is running on localhost:8000 and in your Nginx setup, you are not forwarding your request to that address. You have to match the domain request with your Django running server, In production, you should have something like Gunicorn
Try with -
upstream backend {
server localhost:8000;
}
server {
server_name example.com www.example.com;
location / {
include proxy_params;
proxy_pass http://backend;
}
this would work for non-https. You can add listen 443 ssl, and SSL certificates to make your server block listen only to HTTPS.

Related

Serving Vue.js static files and Django app running on gunicorn over Nginx

I have a web backend implemented in Django and running on Gunicorn. Plus, I also have a Vue.js app that uses this backend. I want to run both of them on nginx and also do HTTPS configs.
This is how my "/etc/nginx/nginx.conf" file looks like:
...
server {
server_name .website.com;
listen 80;
return 307 https://$host$request_uri;
}
server {
location / {
proxy_set_header Host $host;
proxy_pass http://localhost:8080; # where the Django app over gunicorn is running
}
location /static {
root /code/frontend/dist/; # static frontend code created with vite on vue.js
autoindex on;
index index.html;
try_files $uri $uri/ /index.html;
}
# ssl configs
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/website.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
}
Both of them, Django and Vue.js part, are hosted in a single Docker container. 80 and 8080 ports of this container is mapped to 80 and 8080 ports of the host PC.
443, 8080 and 80 ports are open on the machine for inbound connections. Sending a post request to http://website.com:8080/bla do returns correct values, meaning that backend seems to be working but on http only and not on https.
Still when I go to the "website.com", I receive "This site can't be reached" error. Where am I doing wrong exactly and how can I run both on nginx and both over ssl/https?

Bad Gateway python-telegram-bot with webhook and Nginx

I'm trying to set up a webhook with python-telegram-bot and Nginx. I am faced with a problem, my bot doesn't get messages from telegram. I also tried to make GET/POST queries from the postman and I always get a "502 Bad Gateway" error. I also launched the netstat to monitor port 5000 where my telegram bot connects but it is always empty. It seems like webhook doesn't launch at all.
My Nginx default.conf file looks like the following:
upstream django {
server gunicorn:8000;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
ssl on;
server_name example.com www.example.com;
ssl_certificate fullchain.pem;
ssl_certificate_key privkey.pem;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location /TELEGRAM_TOKEN {
proxy_pass http://0.0.0.0:5000/TELEGRAM_TOKEN/;
}
location /static/ {
alias /static/;
}
location / {
proxy_pass http://example.com:8000;
}
}
And my telegram client code:
updater = Updater(api_token)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, custom_command))
jq = updater.job_queue
job_minute = jq.run_repeating(callback_minute, interval=5)
#updater.start_polling()
updater.start_webhook(listen="0.0.0.0", port=5000, url_path=api_token,
webhook_url=f'https://example.com/{api_token}')
updater.idle()
I also have Django options for the Nginx server but I've never seen any tutorial or documentation on how to tune the webhook with Django and it can be the reason for my problems.
Have anyone any idea about solving my problem?
CallMeStag, your advice to use the following code helped me solve the issue:
print(Bot(api_token).get_webhook_info()))
I had 2 problems with the Nginx config file
First: ssl on
It gave me an error connection refused and I deleted it
Second
I changed this
location /TELEGRAM_TOKEN {
proxy_pass http://0.0.0.0:5000/TELEGRAM_TOKEN/;
}
to this
location /TELEGRAM_TOKEN {
proxy_pass http://example.com:5000/TELEGRAM_TOKEN/;
}
and the webhook is now working.
Thank you.

Serving React and Django with SSL and a domain

Currently, I'm deploying a react app and using django as the backend API on an ubuntu nginx server. The react app is already online have a SSL certificate, but the backend API does not. By default, browsers can't show a http content on a https connection.
Do I need to get another SSL certificate afor the backend API? Or is there another way to do it?
Nginx conf file (for the frontend part. I'm not sure how to configure the backend):
The backend is currently running on xxx.xx.x.xx:8000 (using gunicorn --daemon --bind xxx.xx.x.xx:8000)
server {
server_name xxxxxx.com www.xxxxxx.com xxx.xx.x.xx;
root /var/www/frontend/build;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/fromnil.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/fromnil.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = www.xxxxxx.com) {
return 301 https://$host$request_uri;
}
if ($host = xxxxxx.com) {
return 301 https://$host$request_uri;
}
server_name xxxxxx.com www.xxxxxx.com xxx.xx.x.xx;
listen 80;
return 404;
}
Thanks
Found this link, but wasn't able to post a comment because my reputation is not enough. And I don't really understand. Can anyone help me?
How to deploy react and django in aws with a ssl and a domain

Django CMS not working with uWSGI

I have website developed in Django CMS.
When running it with the manage.py runserver it works just fine, but when I try to run it with nginx and uwsgi I get the following error:
view must be a callable or a list/tuple in the case of include().
I think it might the problem might be that I run the django cms in a virtualenv.
I used the same conf files for another django project but it wasn't inside an virtualenv
I start the uwsgi inside my virtualenv.
My wsgi.ini:
[uwsgi]
chdir = /var/www/user.name/mysite
module = mysite.wsgi
#home = /var/www/user
master = true
processes = 10
socket = /var/www/user/mysite/mysite.sock
chmod-socket = 666
vcuum = true
My nginx conf
upstream django {
server unix:///var/www/user/mysite/mysite.sock;
}
server{
# listen on port
listen 80 default_server;
listen [::]:80 default_server;
server_name example.name www.example.name;
return 301 https://$server_name$request_uri;
}
server{
#Default server?
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name example.name www.example.name;
charset utf-8;
client_max_body_size 75M;
ssl_certificate /etc/letsencrypt/live/user/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/user/privkey.pem;
include snippets/ssl-params.conf;
location ^~ /.well-known{
allow all;
alias /var/www/user/.well-known;
}
location /static {
alias /var/www/user/mysite/mysite/static;
}
location / {
uwsgi_pass django;
include /var/www/user/mysite/uwsgi_params;
}
}
You need to add virtual environment path to wsgi.ini, something similar to:
home = /var/www/myvenv
But I don't know if this will solve it

Django, ngnix, and uwsgi site cannot deploy on remote server

I need to deploy my django site using ngnix and uwsgi. it works fine in localhost. but when I give my remote server, it does not work. this is my ngnix.conf file
# nginx.conf
upstream django {
# connect to this socket
server unix:///tmp/uwsgi.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket
}
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 192.168.101.191; # substitute your machine's IP address or FQDN
charset utf-8;
#Max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/calcey/django_env_2/employee/media; # your Django project's media files
}
location /static {
alias /home/calcey/django_env_2/employee/static; # your Django project's static files
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # or the uwsgi_params you installed manually
}
}
"192.168.101.191" means my server IP address. if I put local host instead of "192.168.101.191" it works. but for remote IP it does not work. please give me a suggestion.
Your server IP address looks some kind of Internal IP. But anyway, you do Not need any Server IP address as long as your Nginx script is running on the Server.
You can use the following configurations Instead:
Server {
listen 80;
server_name 127.0.0.1;
....
....
....
}
Hopefully, this will solve your problem.