flask can't load static files when using nginx - nginx

I'm using flask + gunicorn for my website. and I'm using nginx for reverse proxy only. and set up as nginx config flask proxy setups. I haven't added location /static info in nginx.conf file. so all static files are supposed to be handled by flask itself. but when visiting from public network, it can't load css files. I tried to run flask + gunicorn alone without nginx, it worked well. so why can't flask serve static files by itself behind nginx?
Then I added location /statis info into nginx config file so that nginx may serve static file instead. Still can't load static files. when i looked into error log. it shows
"/var/www/my_project_folder/static/css/style.css" failed (2: No such
file or directory), client: 61.152.126.178, server: _, request: "GET
/static/css/style.css HTTP/1.1", host: "47.97.209.250", referrer:
"http://47.97.209.250"
the path in the error message actually is correct. the last method I tried is to run all script and service under root and chmod my_project to 666. still no success.
I searched on stackoverflow, but no one mentioned the issue that flask itselft can't serve static files behind nginx.
So i hope anyone have any idea or clue about this. and nginx serving issue
Thank

I solved it with:
https://www.reddit.com/r/flask/comments/avkjh0/ask_flask_serve_static_files_from_flask_app_via/
nginx.sh:
USE_STATIC_PATH=${STATIC_PATH:-'/app/static'}
location /webappB/static {
#alias /path/to/webappB/static;
alias $USE_STATIC_PATH;
}
main.py:
app = Flask(__name__, static_url_path="/webappA")

Related

Trying to set file upload limit in mup/nginx-proxy

I am running into a file upload error with files > 10M. I have followed the advice here: http://meteor-up.com/docs.html#advanced-configuration which says how to set it in the nginx proxy by setting the clientUploadLimit: '50M'
I pushed the changes using mup proxy reconfig-shared, and it told me it had restarted the proxy. It didn't work, I still get the 413 (Request Entity Too Large) error.
I checked inside the nginx-proxy docker instance, and the file /etc/nginx/conf.d/my_proxy.conf has the correct entry client_max_body_size 50M. I restarted the EC2 box to make sure, but it's still not working.
This article https://www.tecmint.com/limit-file-upload-size-in-nginx/ suggests that the setting needs to go inside a http block, like this:
By default, Nginx has a limit of 1MB on file uploads. To set file upload size, you can use the client_max_body_size directive, which is part of Nginx’s ngx_http_core_module module. This directive can be set in the http, server or location context.
http {
client_max_body_size 100M;
}
I can't see how to achieve this, as the .conf file is read only and somehow locked.
Any ideas on how to proceed?
I suppose I could try a custom nginx.conf file, but I'm not sure what should go in there, and in fact whether it will even improve the situation.
Any help is appreciated :)
I'm happy to report that I solved it... I will explain how.
I was setting the limit in the nginx reverse proxy in the mup.js file
proxy: {
domains: 'website.com,www.website.com',
shared: { clientUploadLimit: '50M' }
}
But it turns out that there is an option to set it for each independent server like this:
proxy: {
domains: 'website.com,www.website.com',
clientUploadLimit: '50M'
}
The limit was being set to 10M by default. I found it by shelling into the nginx-proxy docker image and doing a search with the command grep -R client_max_body_size /etc/nginx and it showed me all the places where it was set (for each vhost)
So I changed the mup.js file for my server, did a mup stop, and a mup setup (to re-do the settings) and then a mup deploy
Now this is speculation but have you tried going to the docker container's root shell changed the permissions to give write permission to root or your user chmod 760 /etc/nginx/nginx.conf and edit the nginx file there?

How to setup nginx for a Flask app on a server with Plesk installed

I want to deploy a little Flask webapp on a root cloud server with Plesk installed. I followed this tutorial as it uses Nginx and Gunicorn, like proposed by the developer of Flask: Miguel Grinberg.
The problem is that Plesk has a modified variant of nginx pre-installed named sw-nginx and I've absolutely no clue on how to implement that line of code:
server {
listen 80;
server_name my.subdomain.com;
location / {
include proxy_params;
proxy_pass http://unix:/var/www/vhosts/mydomain.com/my.subdomain.com/myproject/app.sock;
}
}
If I just put in in a .conf file, the configtest of Nginx fails:
$ nginx -t
nginx: [emerg] open() "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/conf.d/myproject.conf:6
nginx: configuration file /etc/nginx/nginx.conf test failed
Maybe the syntax of the proxy_pass value is wrong (I tried many, here I put one of them), or maybe the proxy_pass param isn't supported or has to be implemented somewhere else?
PS: I also found this answer How to deploy Flask project on Plesk subdomain
But I read somewhere, that replacing sw-nginx by the standard nginx wouldn't be supported by Plesk. Don't want to break anything...
Thanks for your help.

Lapis - 403 error when accessing /static/ directory. Lapis unable to use port 80

I have Lapis installed and working on Debian 9. I have two beginner issues I need to solve running the simple init setup that serves static content from /static/ and the Lua app from all other requests:
I put an index.html file in a directory called static which is in the same directory I start Lapis from, but attempting to access via IP/static/index.html results in a 403 access error.
I cannot set the server to use port 80 because the address is already in use by nginx (confirmed with netstat). There is a "Welcome to OpenResty!" page served there, with the Lapis app only being served on other set ports.
I'd appreciate any advice that would solve these issues. Also any pointers towards documentation that fully describes the relationship between nginx, OpenResty and Lapis would be very helpful.
Thanks to help from LordRyan on the moonscript IRC the issues are solved.
The 403 issue was a permission error caused by the Lapis app directory being at root level.
Inability to use port 80 was caused by an nginx instance running in the background which was discovered using...
ps aux | grep nginx root
... shutting it down solved the issue and gave the Lapis app control.

Nginx and Gunicorn with Virtualbox serves old verison of Django static files padded/cropped to new file size, sendfile off doesn't work

My problem is the same one as described here and here, but with the exception that setting sendfile off; in nginx.conf does not help.
I'm running Vagrant with Virtualbox (Ubuntu 16.04) and Nginx, Gunicorn, and Django. I use this setup for development, so I want to avoid having to call collectstatic every time a file is changed. As such, Nginx routes all requests to Gunicorn (even '/static/'), and Gunicorn serves the static files using this approach:
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
This works, with the exception that if I change something in a static file, the contents are not updated, only the file size. I have tried setting sendfile off; in nginx.conf, and also in my Gunicorn configuration. Still, the problem remains. Is there any way I could try to narrow down the possible cause of this issue?
All Django files are in folder that is shared with the Windows 10 host system, but if I use vagrant ssh and check out the files, they have updated properly.
Edit: The problem persists if I run Gunicorn only without Nginx, so it seems to be a Gunicorn/Django problem.
Figured it out.
I needed to run Gunicorn with the --no-sendfile flag as well. I tried setting it in my ini-file before (no_sendfile = True), which did not work. However, adding --no-sendfile to my Gunicorn run command in supervisor worked!

Nginx Cache file is too small

I have tried enabling Nginx caching in my Elastic Beanstalk application. For this matter I've added the following lines in my Nginx configuration file -
proxy_cache_path /tmp levels=1:2 keys_zone=analytics-cache:50m max_size=1g inactive=5m use_temp_path=off;
proxy_cache analytics-cache;
The problem is that once I start up Nginx I get the following error in the error.log - cache file "/tmp/restore_docker_image_names.sh" is too small.
I have no idea what this error means, and it persisted even after trying to increase the size of my cache key from 5m to 50m.
How can I avoid this error?
The fact that nginx is trying to open a .sh file for it's cache looks suspicious. /tmp is used by the whole system so non cache files already exist there.
Use a proxy_cache_path that is empty and only nginx will use, like /tmp/nginx/cache or /var/cache/nginx

Resources