5

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

}
5
  • Static files are not served by Flask. Commented Aug 3, 2015 at 7:50
  • So you now have a permissions issue. I've edited the answer. Commented Aug 6, 2015 at 8:43
  • Who is the user set to in nginx.conf? Commented Aug 6, 2015 at 16:07
  • You could use the namei -om /root/site-demo/static command line command to see the permissions of these folders. Commented Aug 6, 2015 at 19:08
  • 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 Commented Aug 7, 2015 at 6:07

3 Answers 3

1

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.

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

1 Comment

Changing permissions not solving the permission problem. I tried it. Still showing 403
0

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/

Comments

0

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

Comments

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.