nginx how to host both react and django - python

i have a react frontend imported inside a django backend. communication between the two is done through django-rest-framework. on the react's side, fetching is done through relative paths therefore in my package.json i have added the line:
"proxy": "http://127.0.0.1:8000",
django is hosting react-app locally without problems when i run: python3 manage.py runserver.
on the remote i am trying to use nginx with gunicorn to deploy this app on aws ubuntu instance and run into the problem:
first, i'm running python3 manage.py collectstatic
later, i'm pointing nginx to that static_files for the index.html
success! nginx serves react static files
use gunicorn myapp.wsgi -b 127.0.0.1:8000 to run django
problem! nginx served react files do not fetch anything. fetch does not call for this local path but instead calls public ip of aws instance. also, i cannot simulate get/post requests to the django backend because i think nginx "covers" django's gunicorn generated paths.
please tell how can i connect nginx-served react frontedn to gunicorn run django
my nginx sites-enabled/example
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/fandigger/frontend/build/;
server_name public_ip_without_port;
location / {
try_files $uri $uri/ =404;
}
}

Related

How to Deploy Django App on Centos server having domain secured With SSL?

I have a Django App which I want to deploy on a Centos Linux server having a global/public IP which is assigned to a domain and is secured with SSL.
The System configuration is as:
centos-release-6-10.el6.centos.12.3.x86_64
Apache/2.2.15 (CentOS)
When I run the server using:
python manage.py runserver 0.0.0.0:8000,
then it is only accessible from the browser by passing a local IP say
http://192.xxx.xx.xxx:8000/django_app/home
But I want to access it from the public/global IP, but it gives an error when I replace the local IP with Global/Public IP or domain assigned to the public IP as:
105.168.296.58 took too long to respond.
Try:
Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_TIMED_OUT
When I simply put this public IP in the browser as https://105.168.296.58 then it redirects the browser to the app/website say https://mywebsite.com/home which is running at port 80 on Apache, and shows the assigned domain instead of IP, so IP is working fine.
in settings.py: all hosts are allowed as ALLOWED_HOSTS = ['*']
Methods tried are:
by using django-extensions as:
python manage.py runserver_plus --cert certname 0.0.0.0:8000
by using django-sslserver as:
python manage.py runsslserver 0.0.0.0:8000
The app runs from both of these methods only locally at http://192.xxx.xx.xxx:8000/django_app/home, but None of them worked from a Public IP.
Kindly tell, How to deploy or configure this Django App to Run Outside the Local Network?
i would deploy django like this:
install uwsgi in your virtualenv
pip install uwsgi
create a uwsgi config file (everything inside <> has to be edited to your needs
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = </path/to/your/project>
# Django's wsgi file
module = <project>.wsgi
# the virtualenv (full path)
home = </path/to/virtualenv>
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = </path/to/your/project/mysite.sock>
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
install nginx and create following config
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///<path/to/your/mysite/mysite.sock>;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name <example.com;> # 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 </path/to/your/mysite/media;> # your Django project's media files - amend as required
}
location /static {
alias </path/to/your/mysite/static>; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include </path/to/your/mysite/uwsgi_params;> # the uwsgi_params file you installed
}
}
Download and copy uwsgi_params to your directory
https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
symlink config from /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/sites-enabled/
Start uwsgi and nginx
uwsgi --ini mysite_uwsgi.ini
sudo service nginx restart
don't forget collectstatic so that these files can be served.
we added the mysite_uwsgi.ini and uwsgi_params files to our repository

How do I make my nginx load all static files from django docker app over HTTPS?

I have my nginx installed on the server and I have my Django application running inside a docker container. While my app loads fine over HTTP, it doesn't load any static files (CSS) over HTTPS. What changes should I make in nginx conf or docker app to solve this?
Since my application is in python (Django App), I collected all the static files using "python manage.py collectstatic -c" and then mounted that directory (which contains these static files - /apps/test_app/static ) on the host server running both docker and nginx.
The command I used to mount was
docker run --net=host -v /static:/apps/test_app/static -d -p 8000:8000 image_id
Here static is the directory on the host server where as /apps/test_app/static is directory inside the container.
Once this is done, I added the following lines inside the nginx.conf or ssl.conf file
location /static/ {
# root /var/www/app/static/;
alias /static/;
autoindex off;
}
Followed by nginx restart and then that solved the problem.

Can't make Nginx work with uWSGI

I am having this test file called test.py:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
and I run uWSGI using this command:
uwsgi --http :8001 --wsgi-file test.py
I am able to see the "Hello World" in browser by going to xxxx.xxxx.xxxx.xxxx:8001
Now I am trying to add Nginx to this. Here is my file called mysite_nginx.conf:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000 default_server;
# the domain name it will serve for
server_name _;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /home/wz/dispatcher/OurAwesomeDjangoApp/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/wz/dispatcher/OurAwesomeDjangoApp/uwsgi_params; # the uwsgi_params file you installed
}
}
When i go to xxxx.xxxx.xxxx.xxxx I see the standard "Welcome to Nginx".
However when I go to xxxx.xxxx.xxxx.xxxx:8000 I am unable to connect.
Django server works on this port python manage.py runserver 0.0.0.0:8000 though.
uwsgi_params is present in my project directory.
I have also created a symlink to /etc/nginx/sites-enabled
What am i missing? Any help appreciated.
EDIT:
When i installed nginx using yum install nginx there were no sites-available and sites-enabled directories in /etc/nginx/.
I created them manually and added:
include /etc/nginx/sites-enabled/*;
to /etc/nginx/nginx.conf

Deploy flask in production with GeventWSGI and Nginx

i actually have a rest api written in python with flask and flaskrestful extension.
I use gevent WSGI
def runserver():
api.debug = True
http_server = WSGIServer(('', 5000), api)
http_server.start()
All works like a charm on my machine.
I want go in production on a linux vm,on the internet i searched for hours,i don't choosed mod_wsgi because gevent doesn't work properly with it,so i prefer use nginx.
On the internet i saw flask apps hosted with uWSGI,my answer is i need to use uWSGI?
Even i use geventWSGI in my flask application?
How to work with this?
In case i don't need uWSGI,i only need to config nginx sites to pass the request properly to my flask app?
I'm newbie to all this so i'm a little confused.
Thanks in advance
You can run Uwsgi in Gevent mode http://uwsgi-docs.readthedocs.org/en/latest/Gevent.html and then route all flask requests to it via nginx.
server {
listen 80;
server_name customersite1.com;
access_log /var/log/customersite1/access_log;
location / {
root /var/www/customersite1;
uwsgi_pass 127.0.0.1:3031;
include uwsgi_params;
}
see http://uwsgi-docs.readthedocs.org/en/latest/Nginx.html for more details

Trouble with using Nginx with django and uwsgi

I follow the steps in http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html but when all the steps done without any error I visit 127.0.0.1:8000, it response with a time-out, my nginx log shows that
upstream timed out (110: Connection timed out) while reading response header from upstream,
By the way, I can access 127.0.0.1:8001 where uwsgi and django works well.
And I can access image in 127.0.0.1:8000/image/1.jpg as well, but just cannot access 127.0.0.1:8000
here's my nginx.conf
upstream django {
server 127.0.0.1:8001;
}
server {
listen 8000;
server_name 127.0.0.1
charset utf-8;
client_max_body_size 75M;
location /media {
alias /home/zhaolei/virtualdjango/bin/mysite/media;
}
location /image {
alias /home/zhaolei/virtualdjango/bin/mysite/image;
}
location / {
uwsgi_pass django;
include /home/zhaolei/virtualdjango/bin/mysite/uwsgi_params;
}
}
I use uwsgi --http 127.0.0.1:8001 --chdir=mysite --module=mysite.wsgi
to run uwsgi. I use uwsgi_params hosts in https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
uWSGI have 2 protocols to communicate with web server. One of them is normal HTTP protocol, that can also be used to communicate directly with clients. But there is also special uwsgi protocol, optimized for communication between HTTP Proxy server and uWSGI.
That protocol is used by nginx when using uwsgi_pass directive, and by uWSGI when you're starting your uWSGI server with --socket param.
If you're starting uWSGI with --http param, uWSGI will use HTTP protocol (that is what you're doing), but if nginx is still using uwsgi_pass it's expecting uWSGI protocol on that socket, not HTTP.
To fix it you have to either change your uwsgi start command to use --socket instead of --http (that's recommended way, but you won't be able to check if uWSGI is functioning properly by entering 127.0.0.8001 directly in your browser, but that's okay: if your command with --http worked properly, there won't be any difference using --socket) or use proxy_pass instead of uwsgi_pass in your nginx config.
And it's described on link that you're mentioned, right here

Categories

Resources