Using a static Index page with Django and Nginx - django

I'm building a website using Django + Apache and Nginx to serve my static content. My site's index does not require any backend Django coding, so what would I need to change in nginx.conf to send requests for location / { } to some index.html in my static-content, while still allowing my urls.py to handle patterns appropriately?
upstream backend {
server 127.0.0.1:8080;
}
server {
listen 192.168.1.20:80;
server_name www.example.com example.com;
access_log /home/userxyz/public_html/example.com/logs/nginx_access.log;
error_log /home/userxyz/public_html/example.com/logs/nginx_error.log;
location /
{
proxy_pass http://127.0.0.1:8080;
include /etc/nginx/proxy.conf;
}
location ~ ^/(system|images|robots\.txt|css|js|favicon\.ico).*$
{
root /home/userxyz/public_html/example.com/static-content/;
}
location /media/
{
root /home/userxyz/public_html/example.com/;
}
}

location = / {
root /home/userxyz/public_html/example.com/static-content/;
}

What about something like:
location ~ ^/$
{
root /PATH/TO/index.html;
}
The idea is to give nginx a rule for exactly matching '/'.

Related

Nginx subdomain running the wrong web application

so I have a ubuntu server that run two different website with two different domain:
www.firstwebsite.com
www.secondwebsite.com
But when I create an AAA record to create a subdomain with the first domain (like this)
demo.firstwebsite.com
If I go to this subdomain it automatically run the application website of my other domain (www.secondwebsite.com)
I tried creating a specific socket&service file for the subdomain for it but it still run the web application of the second domain.
Im not sure what is causing that and how to fix that? thank you
nginx config file
server {
listen 80;
server_name firstwebsite.com;
return 301 $scheme://www.firstwebsite.com$request_uri;
}
server {
listen 80;
server_name www.firstwebsite.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/firstwebsite;
}
location /media/ {
root /var/www/firstwebsite;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/firstwebsite.sock;
}
}

Nginx 403 while accessing static files in docker volumes

I am trying to serve static files in docker volumes for my Django project. Nginx is able to access the files(403) error. I tried to solve this in different ways like updating the file permission. Nginx I am installed in a normal way without a container and Django, Postgres database is running in a container
Nginx configuration
server {
listen 80;
server_name 139.59.73.115;
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000;
}
location /static {
root /var/lib/docker/volumes/m3-mobiles_m3-mobiles-assets/_data/;
}
}
I think need changed:
server {
listen 80;
listen [::]:80;
server_name 139.59.73.115;
rewrite ^ https://$host$request_uri? permanent;
location / {
root /var/www/html/;
autoindex on;
}
location ~ /.well-known {
allow all;
}
}
On run docker the containe map your path location: /home/xxxxx/xxxx:/var/www/html/

How to set up nginx configuration site for django

I have a problem with nginx and django.
Config:
server {
listen 80;
server_name 193.88.95.120;
charset utf-8;
root /root/djangoprojects/megaproject;
client_max_body_size 75M;
location /media {
alias /root/djangoprojects/media/;
}
location /static {
alias /root/djangoprojects/megapproject/megaapp/static/;
}
location / {
include /root/djangoprojects/megaproject/uwsgi_params;
}
}
Error 403 - forbidden.
When I create an index file in the project folder .html " - it opens.
But I need Django to work.
Please help me.

django, nginx, gunicorn 404 on any other page than the root

My issue is that whenever I try to view a webpage that isn't the root webpage aka /user/ nginx returns a 404 error and the error log states
"/usr/share/nginx/html/user/login/index.html" is not found.
current nginx configuration
server {
listen 80;
server_name ip_address;
location = /static {
root /opt/scrumban/kanbanboard/;
autoindex off;
}
location = / {
include proxy_params;
proxy_pass http://unix:/opt/scrumban/kanbanboard/kanban.sock;
}
}
Remove those = signs; they mean that only the paths "/" and "/static" are matched. Without the symbol they match as prefixes, which is what you want.

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.