Nginx不提供静态文件

时间:2012-08-17 18:31:59

标签: nginx

我一直在讨论将网站从Apache迁移到Nginx的过程,我将会失去理智。虚拟主机不想提供静态资源(css,js等),我似乎无法弄清楚原因。服务器块如下所示:

server {
  listen      443 default_server ssl;
  server_name dev.myproject.mydomain.net;
  root        /opt/dev/myproject;
  index       index.php;

  ssl_certificate      /etc/ssl/certs/server.crt;
  ssl_certificate_key  /etc/ssl/certs/server.pem;

  access_log /var/log/nginx/vh.project.dev.access.log;
  error_log  /var/log/nginx/vh.project.dev.error.log;

  location ~ ^/alias_name/(.*) {
    alias /opt/dev/myproject/www/$1;

    location ~ ^/alias_name/(.+\.php)$ {
      alias /opt/dev/myprojectp/www/$1;
      include /etc/nginx/conf/php;
    }
  }

  location ~ \.php$ {
    include /etc/nginx/conf/php;
  }

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  location ~ /\.ht {
    deny all;
  }
}

我错过了什么?我知道这是我对Nginx的经验不足,但此时我们会非常感激任何建议。

感谢。

更新

这似乎与我以前遇到过麻烦的别名有关。如果我将文档根目录指向别名位置(/opt/dev/myprojectp/www),并尝试在没有别名的情况下呈现静态内容,则呈现正常。只要我在URL中抛出别名......就没那么多了。

2 个答案:

答案 0 :(得分:1)

取自:http://kbeezie.com/view/nginx-configuration-examples/

# This block will catch static file requests, such as images, css, js
# The ?: prefix is a 'non-capturing' mark, meaning we do not require
# the pattern to be captured into $1 which should help improve performance
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
    # Some basic cache-control for static files to be sent to the browser
    expires max;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

答案 1 :(得分:0)

好的,所以我可能通过暴力试验和错误找到了我自己的答案。此虚拟主机服务器块似乎正确地提供了PHP 静态内容:

server {
  listen      443 default_server ssl;
  server_name dev.myproject.mydomain.net;
  root        /opt/dev/project-root;
  index       index.php;

  ssl_certificate      /etc/ssl/certs/server.crt;
  ssl_certificate_key  /etc/ssl/certs/server.pem;

  access_log /var/log/nginx/myproject.dev.access.log;
  error_log  /var/log/nginx/myproject.dev.error.log;

  location ~ ^/alias_name/(.+\.php)$ {
    alias /opt/dev/project-root/www/$1;
    include /etc/nginx/conf/php;
  }
  location ~ ^/alias_name/(.*) {
    alias /opt/dev/project-root/www/$1;
  }

  location ~ \.php$ {
    include /etc/nginx/conf/php;
  }
}

我不知道我是否会遇到问题,我不能说我完全理解这些差异,但只是删除嵌套的location块似乎已经成功了。