簡體   English   中英

使Nginx在Django本地服務器之前提供靜態內容

[英]Getting Nginx to serve static content in front of Django local server

我正在嘗試在Django的localhost網絡服務器(127.0.0.1:8000)之前使用Nginx來提供靜態內容。 我希望Nginx在'/ static'下提供所有文件,如果沒有,請將請求傳遞到Django的網絡服務器上,但是我被卡住了! 這是我所做的:

  1. Nginx在我的OSX上運行,所以“歡迎使用Nginx!” 頁面顯示在localhost上。
  2. 更改了我的/ etc / hosts文件以添加'testdev.com':

    127.0.0.1本地主機

    127.0.0.1 testdev.com

  3. 在/usr/local/src/nginx-1.2.6中制作了/ sites-available和/ sites-enabled文件

  4. 我的/ conf中的nginx.conf文件是默認值加上include語句:

    包括/usr/local/src/nginx.1.2.6/sites-enabled/testdev.com

5.我的testdev.com文件在站點可用中,在/ sites-enabled中帶有符號鏈接。

server {
root /<path-to-my-django-project>/website/static;
server_name testdev.com;
gzip            off;
listen         8000;

location = /favicon.ico  {
rewrite "/favicon.ico" /img/favicon.ico;
}
proxy_set_header Host $host;
location / {
if (-f $request_filename) {
     add_header X-Static hit;
     access_log   off;
 }

 if (!-f $request_filename) {
     proxy_pass http://127.0.0.1:8000;
     add_header X-Static miss;
 }
 }
}

如果我卷曲testdev.com,它將顯示Nginx:

curl -I http://testdev.com
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Mon, 22 Apr 2013 18:37:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 21 Apr 2013 19:39:47 GMT
Connection: keep-alive
Accept-Ranges: bytes

但是,如果我嘗試訪問靜態文件,則不會執行任何操作:

curl -I http://testdev.com/static/css/style.css
HTTP/1.1 404 Not Found
Server: nginx/1.2.6
Date: Mon, 22 Apr 2013 18:38:53 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

所有這一切都是從谷歌搜索為主,並發現

我在

listen 8000

我認為這是Nginx虛擬主機所需的testdev.com conf文件中的語句,但我非常困惑。 博客作者使用

127.0.1.1 testdev.com

在他的主機文件中,但是如果我添加它,則第一個curl語句將掛起。

我究竟做錯了什么?

謝謝大家-我已經開始工作了,這是我正在工作的testdev conf:

server {
    root /<path-to-django-site>;
    server_name testdev.com;
    gzip            off;
    autoindex       on;

    proxy_set_header Host $host;

    location /static/ {
        add_header X-Static hit;
    }

    location / {
        proxy_pass       http://127.0.0.1:8000;

    }
}

如果您不提供位置塊,則該位置塊看起來像是服務器的根路徑。 現在,當我卷曲時:

curl -I  http://testdev.com/static/js/utils.js
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Tue, 23 Apr 2013 01:36:07 GMT
Content-Type: application/x-javascript
Content-Length: 2730
Last-Modified: Thu, 13 Dec 2012 18:54:10 GMT
Connection: keep-alive
X-Static: hit
Accept-Ranges: bytes

非常感謝@Evgeny-讓我處於正確的位置。 Miget對希望這樣做的其他人很有用。

暫無
暫無

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

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