Can't make Nginx work with uWSGI - python

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

Related

nginx how to host both react and django

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

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

Multiple Django app using nginx and gunicorn on Ubuntu 14.04 trusty server

I am new to server configuration. I do some google and config django app using gunicorn and nginx on ubuntu 14.04 trusty server. For the first django app I use port number 80 and my configfiles are :
/etc/init/gunicorn.conf :-
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid
setgid www-data
chdir /home/myserver/my_virtualenv_path/myproject
exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock myproject.wsgi:application
My nginx configuration file for first django app:
/etc/nginx/site-available :-
server {
listen 80;
server_name myapp.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myserver/my_virtualenv_path/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock;
}
}
After that, i link site to site-enabled .
Next, i create a new django app inside the first django app virtualenv like:
FirstApp_Virtual_Env\first_djangoapp\app files
FirstApp_Virtual_Env\Second_djangoapp\app files
Now i configure gunicorn for second app like :
/etc/init/gunicorn_t :-
description "Gunicorn application server handling myproject2"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid
setgid www-data
chdir /home/myserver/my_virtualenv_path/myproject2
exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock myproject2.wsgi:application
My nginx configuration file for second django app:
/etc/nginx/site-available :-
server {
listen 8000;
server_name myapp2.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myserver/my_virtualenv_path/myproject2;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock;
}
}
After that i link site to site-enabled .
Now here is my problem: when i type myapp.com then my first django app is working fine but for second app when i type myapp2.com its showing nginx page and when i type myapp2.com:8000 it's working fine . I do some google for that but i am unable to find solution. I am newbie to this so please give me a hint for that how to correct my problem. Thanks for your time.
You configured nginx to serve myapp2.com on port 8000:
server {
listen 8000;
server_name myapp2.com;
# ...
}
so why would you expect nginx to serve it on port 80 ?
[edit] I thought the above was enough to make the problem clear but obviously not, so let's start again:
You configured nginx to serve myapp2.com on port 8000 (the listen 8000; line in your conf, so nginx do what you asked for: it serves myapp2.com on port 8000.
If you want nginx to serve myapp2.com on port 80 (which is the implied default port for http so you don't have to specify it explicitely in your url - IOW 'http://myapp2.com/' is a shortcut for 'http://myapp2.com:80/'), all you have to do is to configure nginx to serve it on port 80 just like you did for 'myapp.com': replace listen 8000; by listen 80;.
If you don't type in a port, your client will automatically use port 80.
Typing myapp2.com is the same as typing myapp2.com:80
But myapp2.com is not running on port 80, it's running on port 8000.
When you go into production it is possible to redirect myapp2.com to port 8000 without explicitly typing it. You register myapp2.com with a DNS name server and point it towards myapp2.com:8000

How do I run standalone ruby or python file from nginx?

I have a ruby file index.rb that contains puts "this is to show in nginx landing page".
My nginx landing location is /usr/share/nginx/html and my configuration is
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.rb index.html index.htm;
server_name localhost;
}
How do I print those lines without ruby on rails or python django? Do I need to build my code?
Nginx cannot run the index.rb script, in part because it does not have an embedded ruby (or python) interpreter. What you might want to do is to run your script through a uWSGI app. As documented here, for ruby, you need to create an app.ru script with a call entry point (which will just load your index.rb script). It is then run via:
uwsgi --socket 127.0.0.1:3031 --rack app.ru
Your nginx server can then access the uWSGI process with the following configuration:
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
uwsgi_modifier1 7;
}

configuring web.py in nginx.. confusion

Hi I am new with nginx server, and I have uploaded my index.py file at /var/www/pyth/index.py ...
I am a little bit confused because in my local I can run freely
python index.py and access http://127.0.0.1:8080
I was wondering how can I do that in nginx, I have run python index.py but I can't access to mysite.com:8080
this is my config in /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;`
#root /usr/share/nginx/html;
#index index.php index.py index.html index.htm;
root /var/www/mysite.com;
index index.php index.py index.html index.htm;
# Make site accessible from http://localhost/
server_name mysite.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied reques$
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
...
does anyone has an idea about my case? any help will be appreciated.. thanks in advance
You should set up either a uwsgi (or similar), or a proxy_pass in nginx.
The option with UWSGI is better because it'll use the protocol designed for working with web-servers; though it's a bit harder to set up than just proxying everything via nginx.
proxy_pass
web.py has a web-server just for the development purposes, it shouldn't be used in production environment because it's really slow and inefficient in that case, and using proxy_pass wouldn't be a great idea if you are planning to release it.
With proxy_pass, you leave the 127.0.0.1:8080 server online, and then in nginx (on the same server), set up like that:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
The proxy_pass option redirects everything to the web.py server at 127.0.0.1:8080, the other ones - redirect the data about the connection (IP of the connected client and the host that was used for the connection on the nginx's side)
UWSGI
Using UWSGI, in short, is like that:
1) install uwsgi using your distro's package manager, or a pip, or using setup.py install.
2) in nginx, set up a server that will pass everything to the UWSGI server:
server {
listen 80;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
}
}
3) Then, in your web.py application (let's suppose it's called yourappfile.py), instead of app.run(), use:
app = web.application(urls, globals())
application = app.wsgifunc()
You can still have app.run(), just make sure to put it inside the if __name__ == '__main__' block; and make sure the application = app.wsgifunc() is outside so UWSGI could see it.
Then start a UWSGI server:
uwsgi --http :9090 --wsgi-file yourappfile.py
Take a look at these manuals, it may help you:
UWSGI Quickstart
Web.py running on the nginx uwsgi
Deployment of Web.py Applications Using uWSGI and
Nginx
UWSGI Wiki - Examples

Categories

Resources