2013-03-22 48 views
0

我正在嘗試在服務器上進行magento安裝。這是我的Vhost文件。對於Js Css和圖像的Nginx 403錯誤

server { 
    listen 80; 
    ## SSL directives might go here 
    server_name development.magento.in ; ## Domain is here twice so server_name_in_redirect will favour the www 
    root /var/www/devcode/; 

    client_max_body_size 2M; 

    location/{ 
      index index.html index.php; ## Allow a static html file to be shown first 
      try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler 
      expires 365d; ## Assume all files are cachable 
    } 

    if ($request_uri = /index.php) { 
      rewrite^http://$host? permanent; 
    } 

    location /app/    { deny all; } 
    location /includes/   { deny all; } 
    location /lib/    { deny all; } 
    location /media/downloadable/ { deny all; } 
    location /pkginfo/   { deny all; } 
    location /report/config.xml { deny all; } 
    location /shell/    { deny all; } 
    location /downloader/   { deny all; } 
    location /cron.php   { deny all; } 

    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler 
     rewrite ^(.*.php)/ $1 last; 
    } 

    location /. { ## Disable .htaccess and other hidden files 
      return 404; 
    } 

    location @handler { ## Magento uses a common front handler 
     rewrite//index.php; 
    } 

    # serve static files directly 

    location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html|txt)$ { 
     # root /var/www/devcode/skin/; # I tried to added to this also but never worked 
     access_log  off; 
     expires   30d; 
    } 

location ~ \.php$ 
{ 
    fastcgi_pass 127.0.0.1:9000; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include fastcgi_params; 
    fastcgi_read_timeout 300; 
} 

} 

因此,通過此配置的Nginx顯示403 CSS和JS和圖像。有趣的是,如果我刪除服務靜態文件的部分,它會顯示。 我已經嘗試了Stackoverflow上的所有帖子。 這裏是我的媒體和皮膚目錄權限的樣子

drwxr-xr-x. 23 nginx nginx 4096 Mar 22 14:32  media 
drwxr-xr-x. 5 nginx nginx 4096 Mar 13 03:45  skin 

任何幫助或提示將被高度尊重!


編輯

我的新配置文件是這樣說:

server { 
    listen 80; 
    ## SSL directives might go here 
    server_name development.magento.in ; ## Domain is here twice so server_name_in_redirect will favour the www 
    root /var/www/devcode/; 

    index index.html index.php; ## Allow a static html file to be shown first 

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 

    include  /etc/nginx/fastcgi_params; 

    access_log /var/log/nginx/development.access.log ; 
    error_log /var/log/nginx/development.error.log ; 

    client_max_body_size 2M; 

    location/{ 
      try_files $uri $uri/ /index.php?$args; #@handler; ## If missing pass the URI to Magento's front handler 
      expires 365d; ## Assume all files are cachable 
    } 

    if ($request_uri = /index.php) { 
      rewrite^http://$host? permanent; 
    } 

    location ^~ /(app|includes|lib|media/downloadable|pkginfo|var)/ { internal; } 
    location /var/export/ { internal; } 

    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler 
     rewrite ^(.*.php)/ $1 last; 
    } 

    location /. { ## Disable .htaccess and other hidden files 
      return 404; 
    } 

    # serve static files directly 
    location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html|txt)$ { 
     access_log  off; 
     expires   30d; 
    } 

    location ~ \.php$ 
    { 
     fastcgi_pass 127.0.0.1:9000; 

     fastcgi_index index.php; 
     fastcgi_param MAGE_RUN_CODE default; 
     fastcgi_param MAGE_RUN_TYPE store; 
     fastcgi_read_timeout 300; 
    } 

} 

現在,我以前的問題是固定的。但現在我無法讓我的網站運行。我已檢查access.log它說HTTP 200 OK但index.php沒有收到任何請求。

回答

2

您可以簡單地使用alias指令對靜態文件:

location /skin { 
    alias /var/www/devcode/skin; 
    access_log  off; 
    expires   30d; 
} 

或者更明確地

location /skin/ { 
    alias /var/www/devcode/skin/; 
    access_log  off; 
    expires   30d; 
} 

(見末端的斜槓區別..)

嗯,你實際上也可以用root指令做到,但最後一個斜槓太多了。請參閱aliasroot之間的差異here。我仍然會建議使用alias而不是root,因爲它有很明顯的理由來服務靜態,並且只支持location級別。

不管我的回答,我會說這個配置看起來像阿帕奇重寫邏輯整體,而不是來自一個真正的優化良好的nginx的風格。您可能希望將靜態內容分隔在不同的子文件夾中,如上所示。那麼你也可能想知道爲什麼If is Evil。相反,改寫在@handler,可以try files的:

location @handler { ## Magento uses a common front handler 
    try_files  $uri $uri/ /index.php?$args; 
} 

我不能拿出爲deny all個聰明的主意。但是,我會將public文件夾中的php和靜態內容分開,並將其定義爲root,將這些deny排在首位。請注意,其中一些建議如果與您的特定需求相沖突,聽起來可能毫無意義。

+0

感謝您指引我正確的方向。你能說得更具體一些嗎,當你說'配置看起來像來自Apache重寫邏輯,並不是一個真正的優化nginx風格整體',並可以給我一些鏈接,以便我可以學習的方法 – SAM 2013-03-23 04:45:48

+0

當然,我已經更新盡我所能... – kirpit 2013-03-23 08:27:01

+0

for'deny all' [wiki.nginx.org]建議以下配置'location ^〜/(app | includes | lib | media/downloadable | pkginfo | report/config。xml | var)/ {internal; } location/var/export/{internal; }' 我想告訴你,我已經解決了這個問題。但在此之後,我在index.php上看不到任何請求。當我訪問該站點時,access.log顯示HTTP狀態200,但index.php不執行。 我正在更新新的Vhost文件在我的問題 – SAM 2013-03-23 09:18:43