2

我一直在忙于将站点从 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 中添加别名……就没有那么多了。

4

2 回答 2

2

取自: 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";
}
于 2012-08-17T18:48:39.283 回答
1

好的,所以我可能通过蛮力试错找到了自己的答案。这个虚拟主机服务器块似乎可以正确地提供 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块似乎就可以了。

于 2012-08-17T19:56:32.293 回答