Change static file serving to nginx from flask? - python

I am running my flask project in nginx. This is the conf file
server {
listen 80;
server_name site.in;
root /root/site-demo/;
access_log /var/log/site/access_log;
error_log /var/log/site/error_log;
location / {
proxy_pass http://127.0.0.1:4000/;
proxy_redirect http://127.0.0.1:4000 http://site.in;
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 $scheme;
}
}
When i tried to put the expires part for static files into the conf it failed. I read that this may be due to the fact that the static files are served by flask rather than nginx. If so what changes should i bring to the above conf file so that the static file serving can be done by nginx for my project.
As per the answer i changed the conf as below. Now all static file shows 403 error.
server {
listen 80;
server_name site.in;
root /root/site-demo/;
access_log /var/log/site/access_log;
error_log /var/log/site/error_log;
location / {
proxy_pass http://127.0.0.1:4000/;
proxy_redirect http://127.0.0.1:4000 http://site.in;
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 $scheme;
}
location /static {
alias /root/site-demo/static;
autoindex on;
expires max;
}
}

Add this to your nginx configuration:
location /static {
alias /path/to/your/static/folder;
autoindex on;
expires max;
}
EDIT
nginx requires the whole tree to be readable and not just where your root starts in nginx.conf. So the command
sudo chmod -R 777 /root/site-demo/static
should solve the permissions problem. But, I think, is not a good thing - for security reasons - to put your site in the /root directory of your web server. Usually a site is put under the /var/www folder.
P.S.
The chmod -R 777 command gives owner, group and others permission to read, write and execute files in a folder and in all its subfolders.

check your nginx configuration here:
/etc/nginx/sites-enabled/
/etc/nginx/sites-available/
I was experiencing the same issue you describe
Noticed that I had several configuration files
Leaving only one config file fixed
This site is also helpful:
https://realpython.com/blog/python/kickstarting-flask-on-ubuntu-setup-and-deployment/

if you run on server or docker ,you should do like that:
server {
listen 443;
server_name sample.xx.code;
location /{
proxy_pass http://127.0.0.1:5000;
}
location /static {
proxy_pass http://video/static;
expires max;
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
}

Related

Waitress Nginx Unable to serve static files with relative path

I am using waitress as a WSGI to serve my Django application and I am using nginx as a reverse proxy. When I am giving the absolute path to the static files it works fine but if I try to give relative path it doesn't work.
server {
listen 8000;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
access_log "logs/nginx-access.log";
error_log "logs/nginx-error.log";
location /static/ {
alias "C:/Users/mateen/Documents/MDA-Configuration-Server/static/";
#alias "static/";
}
location / {
proxy_pass http://127.0.0.1:8008;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
The commented part in location /static/ is what I want to make it work. I have also made sure that the file location is correct. I gave the relative paths in access log and error log and they work fine. Is it something I am missing here?

502 Bad Gateway nginx, I have one dominion for two distinct apps (FLASK and DJANGO). I'm using one index page to distribute routes

I think this erros happens, because of proxy_pass is incorrect, if someone has any idea please tell me thanks
NGINX FILE
proxy_cache_path /var/nginx/cache/project
keys_zone=djangoproject_cache:60m;
gzip on;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/javascript application/x-javascript;
gzip_vary on;
gzip_disable msie6;
upstream appserver {
server unix:/home/dir/projects/djangoproject/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name www.subdominion.dominion www.subdominion.dominion;
add_header X-Frame-Options "SAMEORIGIN";
ssl_certificate certificate.crt;
ssl_certificate_key certificate.key;
ssl on;
server_tokens off;
root /var/www/html/;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location /djangoproject{
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 $scheme;
# Fix the “It appears that your reverse proxy set up is broken" error.
rewrite ^/djangoproject(.*) /$1 break;
proxy_pass http://unix:/home/sftpserver/projects/podaexpress/gunicorn.sock; -- I think the error is here
try_files $uri #proxy_to_appserver;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile_max_chunk 1024k;
root /home/dir/projects/djangoproject/static;
proxy_read_timeout 90;
}
location /flaskproject{
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 $scheme;
# Fix the “It appears that your reverse proxy set up is broken" error.
rewrite ^/flaskproject(.*) /$1 break;
proxy_pass http://unix:/home/dir/projects/flaskproject/gunicorn.sock;
proxy_read_timeout 90;
}
location #proxy_to_appserver {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_redirect off;
proxy_pass http://appserver;
}
}
using this nginx file I got one page manager index.html, in this page has two buttons with href link for my dominion/djangoproject or dominion/flaskproject , everything is correct until here, but when I click in button whose link directs for another project has one error http 502, but gunicorn file is correct
gunicorn file from djangoproject
#!/bin/bash
NAME="djangoproject" # Name of the application (*)
DJANGODIR=/home/dir/projects/djangoproject/ # Django project directory (*)
SOCKFILE=/home/dir/projects/djangoproject/gunicorn.sock # we will communicate using this unix socket (*)
USER=root # the user to run as (*)
GROUP=root # the group to run as (*)
NUM_WORKERS=15 # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=project.qasettings # which settings file should Django use (*)
DJANGO_WSGI_MODULE=project.wsgi # WSGI module name (*)
echo "------------- INICIANDO $NAME gunicorn -------------"
# Activate the virtual environment
cd $DJANGODIR
source /home/dir/projects/djangoproject/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /home/dir/projects/djangoproject/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user $USER \
--bind=unix:$SOCKFILE \
--daemon
in both links I get the correct url example https://subdominion.dominion.com.br/djangoprojects or https://subdominion.dominion.com.br/flaskproject

Serving multiple sites with gunicorn and nginx?

I'm trying to serve both a Django and a Flask site using gunicorn and nginx. The Django site is currently available at example.com, and I'd like for the Flask site to be availabe at myapp.example.com .
My nginx configuration file is as follows for the Django site:
cat /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com;
location /static {
alias /home/me/sites/example.com/static;
}
location / {
proxy_set_header Host $host;
proxy_pass http://unix:/tmp/example.com.socket;
}
}
I also have a second nginx config file for the Flask site:
cat /etc/nginx/sites-available/myapp.example.com
server {
listen 80;
server_name myapp.example.com;
access_log /var/log/nginx/access.log;
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I'm pretty sure that I want to change my proxy_pass variable, but am not sure what I should change it to.
My gunicorn file currently is as follows:
description "Gunicorn server for example.com"
start on net-device-up
stop on shutdown
respawn
setuid me
chdir /home/rowan/sites/example.com/source
exec ../virtualenv/bin/gunicorn \
--bind unix:/tmp/example.com.socket \
example.com.wsgi:application
Do I need a second gunicorn config file for the Flask site? Which I would then put in /etc/init/ and start through the sudo start gunicorn-myapp.example.com command?

Unable to load static files on django with gunicorn ,nginx on Google compute engine

I am trying to deploy a sample app on google compute engine and have a centos7 instance setup. I am following this tutorial.
Everything is working fine but the static files are not loading. I have used the setting :
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
and ran collectstatic command. The static file directory is created in the project and have all the static files.
Here is my nginx.conf:
server {
listen 80;
server_name My_External_IP ;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
autoindex on;
alias /home/user/myproject/static/;
}
location / {
proxy_set_header Host $http_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 $scheme;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
Here is a sample error from nginx error.log:
2015/06/29 06:49:49 [error] 3566#0: *3 open() "/home/user/myproject/static/admin/css/login.css" failed (13: Permission denied), client: <<MY_IP>>
, server: <<SERVER_IP>>, request: "GET /static/admin/css/login.css HTTP/1.1", host: "<<SERVER_IP>>", referrer: "http://<<SERVER_IP>>/admin/login/?next=/admin/"
Please suggest what to do.
Edit:
I am also getting this message when I run systemctl status nginx:
Failed to read PID from file /run/nginx.pid: Invalid argument

Django using Nginx to serve static content

I am trying to configure nginx to serve the static content for my django project on a remote VPS. I'm using the following configuration for my nginx instance:
server {
server_name myVPSip;
access_log off;
location /static/ {
alias /usr/local/pcat/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
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"';
}
}
I created the config file in ../nginx/sites-available/, linked it to /sites-enabled/, and restarted nginx however when I hit myip:8001/static I get a django 404.
You shouldn't access your website by 123.123.123.123:8001 because that's the port django is using. You are supposed to access 123.123.123.123 (that is port 80) where nginx is running.

Categories

Resources