how to serve static files through nginx and django in windows - django

I am a newbie. Just got setup the apache server in windows. But now I need nginx to serve my static files and I think I have searched almost everywhere how to configure nginx to serve static files, got many answers, but it was hard to understand. Can someone please explain where do I start from and how to configure nginx and django in windows in a noob's, perspective. Any help will be appreciated. Thank you!

edit the nginx.conf file located in the conf folder of the unzipped nginx for windows and add the following directive (You have to update the path to match the folder of your django project)
location /static/ {
alias 'C:/Users/user1/your_django_project/staticfiles/';
}
an example of your final nginx.conf could look like this if you are running a django webserver like hypercorn or other on port 8000 on localhost
http{
server {
server_name localhost;
location / {
proxy_pass http://backend;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP 127.0.0.1;
proxy_set_header X-Forwarded-For 127.0.0.1;
proxy_set_header X-Forwarded-Proto http;
}
location /static/ {
alias 'C:/Users/user1/your_django_project/staticfiles/';
}
}
upstream backend {
server 127.0.0.1:8000;
# There could be more than a backend here
}
}
events {
worker_connections 1024;
}
tested on nginx 1.20

Try something like this in your server section:
location /static/ {
root /the_directory_with_your_files/;
}

Related

directive is not allowed here in /etc/nginx/default.d/app.conf:1 NGINX Config EC2

I'm trying to configure my app to run on ec2 with some difficulty.
It's a multi container app built with docker-compose consisting of django, drf, channels, redis gunicorn, celery and nuxt for frontend.
I've an instance running and can SSH to the instance and install the relevant packages, docker nginx docker-compose etc.
What I can't do is edit my app.conf nginx file to use the public ip 33.455.234.23 (example ip)
To route the backend, rest and frontend.
I've created and app.conf nginx file which works fine local but when I try edit the nginx files
after install to configure my app to the public ip's I run into errors.
The error I have when writing my config is
2020/11/13 01:59:17 [emerg] 13935#0: "http" directive is not allowed here in /etc/nginx/default.d/app.conf:3
nginx: configuration file /etc/nginx/nginx.conf test failed
This is my nginx config
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
client_max_body_size 100m;
upstream asgiserver {
server asgiserver:8000;
}
upstream nuxt {
ip_hash;
server nuxt:3000;
}
server {
listen 80 default_server;
server_name localhost;
location ~ /(api|admin|static)/ {
proxy_pass http://asgiserver;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
}
location /ws/ {
proxy_pass http://asgiserver;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# proxy_redirect off;
}
location / {
proxy_pass http://nuxt;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
}
}
}
What am I doing wrong here? What to I need to do in order to have my app running via reverse proxy on my ec2 public address?
It looks like this configuration file isn't treated as the main configuration file by nginx but being included from the main main configuration file /etc/nginx/nginx.conf which in turn already contains the http block with the include directive within, something like
include /default.d/*.conf;
Check this, if it is true, remove everything except upstream and server configuration blocks from the /etc/nginx/default.d/app.conf file. Move the client_max_body_size directive inside the server block.

I have problem to deploy my django project with docker nginx and gunicorn

If I have running my containers on the server side and everything is okay,for accessing route on the browser should I put ip of nginx container with domain in etc/host or it have to work without that?
my nginx.config
server {
listen 80;
server_name my_domain;
root /code/;
error_log /var/log/nginx/new_error.log debug;
location / {
proxy_pass http://web:8000/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /code/staticfiles/;
}
location /mediafiles/ {
alias /code/mediafiles/;
}
}
where web is my docker-container with gunicorn running
If your nginx container has the ports expose using the flag -p <host_port>:<container_port> you can access to nginx service adding the domain in your /etc/hosts file pointing to the ip of your localhost, however if you didn't use this flag you need pointing to the ip of your nginx container. What's the difference?...when you expose the ports you can use the service even outside of the host where the container was deployed.
I hope help you.

how to deploy react as frontend and drf (django rest framework) on same server( Digitalocean or aws )

I have Django react app. I am using drf for apis and react as front-end. i want to deploy them on digital ocean separately on same server. any suggestions?
First of all, I must say that my main stack is Django-uWSGI-NGNIX. There is a great instruction, which you can use with DigitalOcean:
How To Serve Django Applications with uWSGI and Nginx
And this one for Gunicorn:
How To Set Up Django with Postgres, Nginx, and Gunicorn
I recommend trying to deploy your Django App on the server first, using these instructions. Maybe it will be just a simple one-page project.
After that you can modify your ngnix configs. In my case it will be:
upstream my_backend_server_name_or_whatever {
# server unix:///path/to/your/mysite/mysite.sock; -- if you want to use sockets
server 127.0.0.1:5000;
}
server {
listen 80;
server_name yourdomain.com/api; #or /back, /backend, /what_you_like
charset utf-8;
client_max_body_size 75M;
location /media #your locantions configs
....
location / {
uwsgi_pass my_backend_server_name_or_whatever;
include /path/to/file/uwsgi_params;
}
}
Also, you need to run your CGI server on port 5000. After that, you can access to your Django app through yourdomain.com/api and it will upstream to localhost:5000.
Try this step with your current Django app. After that, you can configure your DRF to work with these links.
If it will work, the next step. Run your NodeJS server on the other port, like 5100. You can find the same instructions for Webpack or raw NodeJS. After that use the same technics, but for the NodeJS server. In my case:
upstream my_frontend_server_or_whatever {
server localhost:5100;
}
server {
listen 80;
server_name yourdomain.com;
access_log /var/log/nginx/ide.access.log;
error_log /var/log/nginx/ide.error.log;
client_max_body_size 75M;
location / {
proxy_pass http://my_frontend_server_or_whatever;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
}
}
Now you can save both files as backend.conf and frontend.conf, run ngnix and check all configs like in instructions above. After that, you can use yourdomain.com/api links in your React App.
In my opinion, this is the simplest way to try React+DRF. But these configs only for development!! Not for production.
Hope it's helpful.

Nginx redirects to default page

I am setting up a domain for my Django/Gunicorn/Nginx server. It works fine with IP address instead of domain name in server_name but when I add domain name it redirects to default Ubuntu Nginx page. My Nginx file looks like this (please note that I replaced my domain with example.com):
Path : /etc/nginx/sites-available/projectname
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
client_max_body_size 4G;
location = /favicon.ico {access_log off; log_not_found off;}
location /static/ {
root /path/to/static/dir;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unix:/path/to/gunicorn.sock;
}
}
I have run the command sudo nginx -t and sudo service nginx restart but no effect. Please let me know if I am doing anything wrong.
1- see main nginx.conf how include all config files. if it is including site-enabled path then go to path and see is a shortcut to config file of this site under site available?
or if all sites are enabled in nginx config file include directly available
include /etc/nginx/sites-available/*;
2-mix two server define code once and with rule forward non www to with www
3-if not work check dns config problem and see result from inside of server via putty not from outside of server with browser to see it is nginx problem or dns config problem.
note: changing dns name servers taken some hours to work and effect on clients.

Django, nginx, + gunicorn: connection refused when serving media files

I have a Django app served by gunicorn with nginx handling incoming requests. Static files are served just fine, and media files are served in my local development environment- I have MEDIA_ROOT and MEDIA_URL set.
However, on my web host (running Ubuntu 14.04), media files aren't served. Even if I run the Django dev server instead of letting gunicorn serve the app, media files do not show up and the following errors are output to the console:
Since taking gunicorn out of the equation results in the same behavior, I believe the issue is with my nginx configuration. This is my sites-available for the app:
server {
listen 443 ssl;
server_name <server name here>;
<ssl stuff here>
access_log off;
location /static {
alias /opt/testenv/testenv/proj/static/;
}
location /media {
autoindex on;
alias /opt/testenv/testenv/proj/media/;
}
location ~ /.well-known {
allow all;
}
location / {
proxy_pass http://127.0.0.1:9900;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
server {
listen 80;
server_name <server name here>;
return 301 https://$host$request_uri;
}
I was initially missing the location /media block, which I thought was the issue. However, even after adding it, restarting nginx, and running collectstatic, I still get CONNECTION REFUSED errors when attepting to retrieve the files.
I turned on autoindex to verify that the files can be navigated to manually, and that does work.
The nginx error log doesn't contain anything related to these connection errors, which is curious to me- could the request be getting rejected elsewhere? I recently setup HTTPS but this issue was occurring even before that.
Looks like you are making requests directly to gunicorn instead of nginx,from you configuration you should visit:
https://127.0.0.1
instead of:
http://127.0.0.1:9900