Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask nginx and static files issue for non default static location

I am using nginx (via gunicorn) to serve static files for a flask app.

Static files in the default static folder are working fine:

<link rel="stylesheet" href="{{ url_for('static', filename='css/fa/font-awesome.min.css') }}" />

However for other static files which I want to restrict access to for logged in users only I am using a static folder served by Flask:

app.register_blueprint(application_view)

application_view = Blueprint('application_view', __name__, static_folder='application_static')

in html I'm calling a static file thus:

<link rel="stylesheet" href="{{ url_for('application_view.static', filename='css/main.css') }}" />

then in application/application_static I have the restricted static files. This works fine on a local Flask install, however when I deploy to a production machine with Nginx serving files from the /static folder I get a "NetworkError: 404 Not Found - website.com/application_static/main.css".

Any ideas on how to configure Ngix to handle this issue?

conf.d/mysitename.conf file:

upstream app_server_wsgiapp {
     server localhost:8000 fail_timeout=0;
}

server {
 listen 80;
 server_name www.mysitename.com;
 rewrite ^(.*) https://$server_name$1 permanent;
}


server {
  server_name           www.mysitename.com;
  listen                443 ssl;
  #other ssl config here
  access_log            /var/log/nginx/www.mysitename.com.access.log;
  error_log             /var/log/nginx/www.mysitename.com.error.log info;
  keepalive_timeout     5;
  # nginx serve up static files and never send to the WSGI server
  location /static {
    autoindex on;
    alias /pathtositeonserver/static;
  }

  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://app_server_wsgiapp;
         break;
    }
  }

  # this section allows Nginx to reverse proxy for websockets
  location /socket.io {
    proxy_pass http://app_server_wsgiapp/socket.io;
    proxy_redirect off;
    proxy_buffering off;

    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_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
  }
}

nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
like image 923
Don Smythe Avatar asked Nov 09 '22 16:11

Don Smythe


1 Answers

gunicorn will have the old code still running, unless you reload the configuration file.

You either stop and restart gunicorn, or send a HUP signal to the gunicorn process.

like image 118
sahuk Avatar answered Dec 29 '22 15:12

sahuk