5
votes

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;
   } 

}
3
Static files are not served by Flask. - Daniel Roseman
So you now have a permissions issue. I've edited the answer. - doru
Who is the user set to in nginx.conf? - doru
You could use the namei -om /root/site-demo/static command line command to see the permissions of these folders. - doru
drwxr-xr-x root root / ------- drwx------ root root root ---------- drwxr-xr-x 777 www-data site-demo --------- drwxrwxrwx 777 www-data static This is the output i got. www-data is the owner i think - Tyler Durden

3 Answers

1
votes

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.

0
votes

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/

0
votes

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;
}