2017-03-07 90 views
0

我有一個運行gunicorn -w 1 -b 0.0.0.0:8000 flaskapp:app的瓶型應用程序,下面的nginx配置。但是,我怎樣才能知道nginx是否真的在爲靜態文件提供服務?我嘗試將alias /home/pi/Public/flaskapp/static/;更改爲.../static-testing/;,然後在此處放置佔位符style.css,但頁面似乎像以前一樣加載。確認Nginx正在提供靜態文件而不是Flask

server { 
    listen 5000; 
    server_name _; 
    location/{ 
     proxy_pass http://127.0.0.0.1:8000; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
    } 
    location /static { 
     alias /home/pi/Public/flaskapp/static/; 
    } 
} 

我是否缺少明顯的東西?是否必須在燒瓶的路線中指定某些東西?

+0

你有規則,所以它是爲他們服務。如果您刪除該規則,Flask將爲其服務(效率低下)。其餘的可能是瀏覽器緩存。 – davidism

+0

但是,有沒有辦法說明這種情況? –

+0

看看Gunicorn的原木。它處理靜態資產嗎?如果是這樣,那麼Nginx不是。 – davidism

回答

0

所以最後正確配置nginx的。我添加了root並刪除了靜態的硬路徑,還添加了清晰顯示靜態和css正在從nginx加載的日誌文件!我也將聽音端口改爲80(驚喜)。

server { 
    listen 80; 

    server_name myapp.com; 
    root /home/pi/Public/myapp; 

    access_log /home/pi/Public/myapp/logs/nginx-access.log; 
    error_log /home/pi/Public/myapp/logs/nginx-error.log; 

    location/{ 
     proxy_pass http://127.0.0.1:8000; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
    } 

    location /static/ { } 

    location /uploads/ { } 
} 
0

您可以將自定義標題添加到您的nginx位置塊,並查看它是否設置在您的靜態文件中。

+1

我該怎麼做?我新使用nginx。 –

0

你也許可以用/ static/location中提到的空路徑來測試它。

server { 
    listen 5000; 
    server_name _; 

    location /static/ { 

    } 

    location/{ 
     proxy_pass http://127.0.0.0.1:8000; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
    } 

}

這將給404錯誤,因此你可以驗證,如果文件是由Nginx的服務。

+0

仍然在靜態文件夾中加載文件。結論:我的設置不正常? –

+1

你可以試着把'/ static'塊放在'/'塊的上面,然後看看它是否有效? –

0

我認爲最簡單的方法是將一些上游變量記錄到訪問日誌中。

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables

你應該在HTTP塊

upstream backend { 
    server 127.0.0.0.1:8000; 
} 

然後proxy_pass更改爲http://backend添加到您的nginx的conf這一點;

現在添加

log_format upstream '$upstream_bytes_received $upstream_response_time'; 
access_log /var/log/nginx-upstream upstream; 

到您的服務器模塊,然後重新啓動nginx的。 當nginx沒有請求上游時,你會看到' - '。

文件:http://nginx.org/en/docs/http/ngx_http_log_module.html & http://nginx.org/en/docs/http/ngx_http_upstream_module.html