nginx vhost 不提供静态文件服务

nginx vhost 不提供静态文件服务
server {
  listen 80;
  server_name www.site.dk;
  access_log /var/www/www.site.dk/logs/access.log;
  error_log /var/www/www.site.dk/logs/error.log;

  root /var/www/www.site.dk/;


  location / {

    index index.php index.html;

    if (-f $request_filename) {
      break;
    }

    if (!-f $request_filename) {
      rewrite ^/(.+)$ /index.php last;
      break;
    }
  }

  location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/www.site.dk$fastcgi_script_name;
  }
}

我试图让 nginx 为任何物理文件(css、图像、js)提供服务,而不对其执行任何操作,让 php 处理所有其他请求。所有非物理文件都应传递给 php。

但它不起作用,php 正在执行,但调用 .css 文件也作为请求传递给 php。

[更新] 我尝试直接在浏览器 www.site.dk/css/file.css 中加载 css 文件。

答案1

server {
    server_name www.site.dk; #Default is port 80
    root /var/www/www.site.dk/webroot; #Use a webroot!

    access_log /var/www/www.site.dk/logs/access.log;
    error_log /var/www/www.site.dk/logs/error.log;

    location / {
            # This is cool because no php is touched for static content
            try_files $uri @use_php;
    }

    location @use_php {
            #The following line may have to be modified for desired behavior
            rewrite ^/(.+)$ /index.php last;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
    }
}

一些来自http://wiki.nginx.org/Drupal

相关内容