确认应用程序是否使用 nginx 来提供静态文件

Confirm if application is using nginx to serve static files

我正在使用这个 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

所有路由都工作正常。但是如果我这样做 http://localhost:8000/static 我会看到

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

显然我应该从静态文件夹中看到带有 <h1>Test!</h1> 的页面。

我做错了什么?

基本上我想知道如何配置nginx来提供静态文件然后确认。

-app.py
-static
   -index.html

你应该直接提出请求http://localhost:8000/static/index.html然后你会看到响应。

但是如果你想默认在 index.html 上查看,你应该在 conf:

中有类似的东西
location /static {
    alias  /home/www/flask_project/static/;
    try_files $uri $uri/index.html index.html;
}

首先,对端口8000的请求完全绕过了nginx,所以这里没有什么奇怪的。你应该去 localhost 没有端口号。

其次,您必须将此配置符号链接到 /etc/nginx/sites-enabled 并重新加载 nginx。

第三,你的静态位置是错误的。您有 location 没有尾部斜杠,alias 有尾部斜杠。它们应该总是同时带有或不带有尾部斜杠。在这种情况下,使用 root 指令会更好。

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