简体   繁体   中英

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

}

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.

PS

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.

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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM