簡體   English   中英

Nginx不會加載Flask靜態文件

[英]Nginx will not load Flask static files

新的flask / nginx / uWSGI服務器。 應用程序會按預期加載和運行功能,但不會加載html / css模板。 在Chrome中使用開發人員工具顯示頁面未返回。

“ tail -f /var/log/nginx/error.log”表示文件或目錄不存在。 但這似乎是正確的道路。 完整路徑為“ /opt/netmgmt/app/static/css/main.css”

2016/09/15 22:04:40 [error] 4939#0: *1 open() "/app/static/css/main.css" failed (2: No such file or directory), client: 10.0.0.69, server: , request: "GET /static/css/main.css HTTP/1.1", host: "nms-srv2", referrer: "http://nms-srv2/"

Flask調試完全沒有問題。 我已經將我的頭撞了一段時間,發現其他多個線程也遇到了同樣的問題,但是沒有任何事情可以為我解決這個問題。

謝謝您的幫助!

/etc/nginx/nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;

    # Configuration containing list of application servers
    upstream uwsgicluster {

        server 127.0.0.1:8080;
        # server 127.0.0.1:8081;
        # ..
        # .

    }
    # Configuration for Nginx
    server {

        # Running port
        listen 80;

        # Settings to by-pass for static files
        location ^~ /static/  {

            # Example:
            # root /full/path/to/application/static/file/dir;
            root /app/;

        }

        # Serve a static file (ex. favico) outside static dir.
        location = /favico.ico  {

            root /app/favico.ico;

        }

        # Proxying connections to application servers
        location / {

            include            uwsgi_params;
            uwsgi_pass         uwsgicluster;

            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;

        }

應用程序文件(我嘗試過多種方式來更改靜態位置,並使用別名代替root來實現運氣。)

@app.route('/')
def home():
  return render_template('home.html')

Layout.html

<!DOCTYPE html>
<html>
  <head>
    <title>NetMgmt</title>
    <strong><link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}"></strong>
  </head>
  <body>
    <header>
      <div class="container">
        <h1 class="logo">NetMgmt (beta)</h1>
        <strong><nav>
          <ul class="menu">
            <li><a href="{{ url_for('home') }}">Home</a></li>
         </ul>
        </nav></strong>
      </div>
    </header>

    <div class="container">
       {% block content %}
       {% endblock %}
    </div>
  </body>
 </html>

在您的nginx配置中,使用指向static目錄的絕對路徑。 static應該在路徑中:

location ^~ /static/  {
    root /full/path/to/app/static/;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM