5

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/*;
}
4
  • Show some nginx configuration and more details of your application. Commented Apr 29, 2016 at 16:08
  • On your local machine are you running the flask wsgi server or gunicorn? Can you also post your nginx conf file? Commented Apr 30, 2016 at 18:57
  • @iurisilvio apologies I did not get back sooner, I have been flat out on another project. I have added the conf.d and nginx conf files. I am running the flask packaged server in my local dev environment. Commented May 14, 2016 at 8:54
  • @lesingerouge I am running the flask packaged server in my local dev environment. Commented May 14, 2016 at 10:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am reloading all config files and stopping and restarting gunicorn each time I deploy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.