Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change static file serving to nginx from flask?

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

}
like image 652
Tyler Durden Avatar asked Nov 09 '22 08:11

Tyler Durden


1 Answers

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.

like image 171
doru Avatar answered Nov 14 '22 23:11

doru