2013-03-22 47 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