How to set up Django with nginx reverse proxy - django

I have a Django project that I have up and running with the development server at 127.0.0.1:8888. I'm trying to get it to run on my vps with nginx, so I can see it at example.com/djangoApp.
Here's my nginx.conf:
server {
server_name example.com;
location /otherLocation/ {
proxy_pass http://127.0.0.1:10000;
}
location /djangoApp/ {
proxy_pass http://127.0.0.1:8888;
}
When I navigate to example.com/djangoApp, it throws an error: "Using the URLconf defined in djangoApp.urls, Django tried these URL patterns, in this order:
/admin
The current path, djangoApp/, didn't match any of these."
Can I modify the root url in settings.py to mitigate this?

I fixed this by adding to nginx.conf:
location /djangoApp {
rewrite ^/djangoApp/(.*) /$1 break;
proxy_pass http://127.0.0.1:8888;
}
Thanks to this SO exchange.

server {
server_name example.com;
location /otherLocation/ {
proxy_pass http://127.0.0.1:10000/;
}
location /djangoApp/ {
proxy_pass http://127.0.0.1:8888/;
}
}
The above should work. You are missing the '/' at the end of the proxy_pass url
Alternatively, you can do
server {
server_name example.com;
location /otherLocation {
proxy_pass http://127.0.0.1:10000;
}
location /djangoApp {
proxy_pass http://127.0.0.1:8888;
}
}

Related

How to configure nginx for websocket. I have django for REST in backend. The standard configurations i found over net wont work for nginx websocket

I have nginx to server to browser. I want nginx to serve for websocket requests from browser. Nginx has internally proxy to django (gunicorn uwsgi) using proxy configs. I am not able to set up the config in nginx for websocket. Tried different configs from internet but no success. My default file in nginx config file :
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/gmc/dist;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404 /index.html index.js;
}
location ~ /redfish.* {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8000;
}
}
These are the basic settings you need to apply for your django-project:
server {
listen 80;
server_name **Server Name or IP**;
#Website LOGO:
location = /favicon.ico { access_log off; log_not_found off;}
#Static Files like CSS, JS
location = /static/ {
root /home/hitshell/website/project/
}
#Media Files like Images, Videos etc.
location = /media/ {
root /home/hitshell/website/project/
}
#proxy
location = / {
include proxy_params;
proxy_pass http://unix:/home/hitshell/website/project/project.sock;
}
}
REFERENCE:
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn
https://www.shellvoide.com/hacks/installing-django-application-with-nginx-mysql-and-gunicorn-on-ubuntu-vps/
The First one has some missing configurations in the middle of the tutorial.

Nginx Gunicorn one ip multiple django sites in folders

thanks for reading my question.
I'm trying to serve multiples Django sites on their own folders in one server without domain (only IP address) using Gunicorn and Nginx. Something like that:
20.20.20.20/demos/myapp1/ --> Django app
20.20.20.20/demos/myapp2/ --> Django app
20.20.20.20/demos/myapp3/ --> Django app
I have tested a lot of settings but I can't make it work. When i tried to load URL 20.20.20.02/demos/myapp1/ i get a 404 not found error :(
Example one site nginx conf:
upstream app1_server {
server unix:/webapps/myapp1/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 20.20.20.20;
keepalive_timeout 5;
client_max_body_size 4G;
location /static/ {
alias /webapps/myapp1/static/;
}
location /media/ {
alias /webapps/myapp1/media/;
}
location /demos/myapp1/ {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app1_server;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/myapp1/static/;
}
}
What is wrong with my myapp1.conf file?
For the record, if i change "location /demos/myapp1/" to "location /" my first app works, but the others apps still not working.
Thanks in advance.
Edit 1:
Checking my problem.. For now i found a solution. Rewrite rule:
location /myapp1/ {
rewrite ^/myapp1(.*) $1 break;
try_files $uri #proxy_to_app;
}
Is a good solution? My Django apps broken their urls :(
Well, reading about Nginx, i solved my problem in 4 steps:
Using rewrite rule like my edit post.
Listen each app in diferent port, like this:
server {
listen 81;
server_name 20.20.20.20;
location /demos/myapp1/ {
rewrite ^/demos/myapp1(.*) $1 break;
try_files $uri #proxy_to_app;
}
...
}
server {
listen 82;
server_name 20.20.20.20;
location /demos/myapp2/ {
rewrite ^/demos/myapp2(.*) $1 break;
try_files $uri #proxy_to_app;
}
...
}
server {
listen 83;
server_name 20.20.20.20;
location /demos/myapp3/ {
rewrite ^/demos/myapp3(.*) $1 break;
try_files $uri #proxy_to_app;
}
...
}
Reload Nginx
sudo service nginx restart
Test it:
http://20.20.20.20:81/myapp1/
http://20.20.20.20:82/myapp2/
http://20.20.20.20:83/myapp3/
If you have a better way to solve my problem, please let me know!

Django server - 502 error bad gateway

I have a centOS with a virtualenv, i can use the project in my localhost , but when the project is upload to a server give an error :
502 - bad gateway
I think the problem probably is in my nginx file.
server {
listen 80;
server_name www.site.com.br site.com.br;
root /var/www/html/agrodez/src/;
if ($http_host != "www.site.com.br") {
rewrite ^ http://site.com.br$request_uri permanent;
}
location /static/ {
alias /var/www/html/site/src/sistema/static/;
}
location /{
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
}
I don't know much about django, but recently I had to help a team with server configuration, and I've used the following nginx virtualhost configuration:
upstream django.local {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name site.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://django.local/;
}
location /static {
autoindex on;
# this line would be STATIC_ROOT dir
alias /var/www/html/agrodez/src/staticfiles;
}
location /media {
autoindex on;
# this line would be MEDIA_ROOT dir;
alias /var/www/html/agrodez/src/media;
}
# If you want nginx logs, then can add these
error_log /var/log/nginx/site.com_error.log;
access_log /var/log/nginx/site.com_access.log;
}
You can give it a try.

How to run Django and Wordpress using Nginx and Gunicorn at the same domain?

I have a Django app that is running on a domain e.g. www.example.com
I want to create a Wordpress landing page, and point this landing page to the home url www.example.com and the wordpress admin site to www.example.com/admin or www.example.com/wp-admin. All the other URLs should be served by Django.
So, I want:
www.example.com -> wordpress
www.example.com/admin or www.example.com/wp-admin -> wordpress
All the other URLs to be served by Django
Till now, this is my Nginx configuration using Django:
upstream django_server {
server unix:/path/to/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.example.com example.com
client_max_body_size 4G;
access_log /path/to/nginx-access.log;
error_log /path/to/nginx-error.log;
location /static/ {
alias /path/to/static/;
}
location /media/ {
alias /path/to/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://django_server;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /path/to/static/;
}
}
Any help would be greatly appreciated.
WordPress uses an indeterminate set of URLs and so it is important to have a clear partition between that and the set of URLs available to Django. The best solution is to place WordPress into a subdirectory (which is surprisingly easy).
For example:
server {
...
# existing Django configuration
...
location = / {
return $scheme://$host/blog/;
}
location ^~ /blog {
alias /path/to/wordpress;
index index.php;
if (!-e $request_filename) { rewrite ^ /blog/index.php last; }
location ~ /wp-content/uploads/ { expires 30d; }
location ~ \.php$ {
if (!-f $request_filename) { rewrite ^ /blog/index.php last; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
...
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
if (!-f $request_filename) { rewrite ^ /blog/index.php last; }
expires 30d;
}
}
}
You will need to set the Site and Home URLs. See this document for details.
See this document for more.

nginx + uwsgi custom 502 will not work

Hi I am trying to get a custom 502 page working on a website and can't seem to get it working.
Basically the way i'm testing it is I'm just stopping uwsgi and accessing the page and every time i get the default nginx 502 page. Can someone please explain to me how to get this working? I've been at this for over a week with 0 success. I have a file named 502.html in public_html and i can access it directly with http://ask.ploy.io/502.html but as soon as i stop uwsgi and try to access the main domain http://ask.ploy.io I get the default 502 page. Here is the vhost config:
### nginx vhost conf for ployio
server {
listen 80;
server_name ask.ploy.io www.ask.ploy.io;
access_log /usr/local/apache/domlogs/ask.ploy.io main;
error_log /home/ployio/access-logs/ask.ploy.io debug;
root /home/ployio/public_html;
index index.html index.htm index.php;
location /502.html {
root /home/ployio/public_html;
}
location ~ /\.ht {
deny all;
}
location / {
error_page 404 403 = #uwsgi;
log_not_found off;
error_page 502 /502.html;
root /home/ployio/public_html;
}
location #uwsgi {
internal;
uwsgi_pass unix:/home/ployio/.uwsgi/uwsgi.sock;
include /usr/local/nginx/conf/uwsgi_params;
}
location ~* ^.*\.php$ {
if (!-f $request_filename) {
return 404;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://204.61.223.114:8888;
}
location /cpanel {
rewrite ^/(.*) https://cpanel.ask.ploy.io:2083/$1 permanent;
}
}
If 502 it's the only error code you want to handle with a custom error page, you just need to be specific in the location:
location /502.html {
root /home/ployio/public_html;
}
Your current location is only being matched with the exact "/50x.html" path, which indeed does not exists in your server: http://ask.ploy.io/50x.html
It's also possible using nginx variables ($uri or something similar) to redirect all 50x errors to that root directory, but for your needs this should be enough.
The main issue is in the location #uwsgi section.. it never seems to handle the 502 return correctly.. maybe by design?
This is a working config
server {
listen 80;
server_name ask.ploy.io www.ask.ploy.io;
access_log /usr/local/apache/domlogs/ask.ploy.io main;
error_log /home/ployio/access-logs/ask.ploy.io debug;
root /home/ployio/public_html;
index index.html index.htm index.php;
location / {
uwsgi_pass unix:/home/ployio/.uwsgi/uwsgi.sock;
include /usr/local/nginx/conf/uwsgi_params;
}
error_page 502 503 504 #maintenance;
location #maintenance {
root /home/ployio/public_html_502;
rewrite ^(.*)$ /502.html break;
}
}
Make sure you put the 502.html in a new root and reference it there.. ps.. randall sucks