如何使用Nginx提供Flask静态文件? [英] How to serve Flask static files using Nginx?
问题描述
我有一个具有以下结构的Web应用程序:
I've a web application with this structure:
|
|__ static
|__style.less
|__images
|__ myapp.py
|__ wsgi.py
我已经设法使用nginx和wsgi运行Web应用程序,但是问题是没有提供静态文件,我的意思是,当我访问它们的URL时,服务器找不到它们.它给了我404.
I've managed to run the web application using nginx and wsgi, but the problem is that the static files are not served, i mean, the server can't find them when i go to their URL. It gives me 404.
这是我的Nginx配置文件部分:
Here's my nginx configuration file part:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/public_html;
index index.php index.html index.htm;
server_name xxxxxxx.com;
location / {
try_files $uri $uri/ =404;
}
location /myapp {
include uwsgi_params;
uwsgi_pass unix:/var/www/public_html/myapp/myapp.sock;
}
是否缺少某些东西?
推荐答案
将此添加到您的nginx配置
Add this to your nginx configuration
location ^~ /static/ {
include /etc/nginx/mime.types;
root /project_path/;
}
将/project_path/
替换为应用的绝对路径,您应注意,它不包括静态目录,并且/project_path/static/
中的所有内容都将存储在其中网址/static/
.
replace /project_path/
with your app's absolute path, you should note that it doesn't include static directory and all the contents inside /project_path/static/
will be serverd in url /static/
.
这篇关于如何使用Nginx提供Flask静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!