我正在使用这个tutorial - part 1,但我不确定如何测试应用程序是否运行nginx服务静态文件。
我有完全相同的密码。
/etc/nginx/sites-available/flask_project
server {
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask_project/static/;
}
}
然后:
gunicorn app:app -b localhost:8000
所有路线都运转良好但是如果我这样做了,我会看到
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
显然我应该看到静态文件夹中
http://localhost:8000/static
的页面。我做错什么了?
基本上我想知道如何配置nginx来提供静态文件,然后确认。
-app.py
-static
-index.html
最佳答案
首先,请求端口8000
完全绕过nginx,所以这里没什么奇怪的您应该在没有端口号的情况下转到localhost
。
其次,必须将此配置符号链接到/etc/nginx/sites-enabled
并重新加载nginx。
第三,你的静态位置不对。没有斜杠,只有一个它们应该始终同时带有或不带有尾随斜线。在这种情况下,最好有location
指令。
server {
root /home/www/flask_project;
index index.html;
location / {
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static/ {
# empty. Will serve static files from ROOT/static.
}
}