nginx vhost不提供静态文件

时间:2011-11-03 13:27:38

标签: nginx

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,images,js)而不做任何事情,让php处理所有其他请求。所有不是物理文件的东西都应该传递给php。

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

1 个答案:

答案 0 :(得分:0)

这将完成工作

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/;
    index index.php index.html;

    location / {
        try_files $uri $uri/ @fastcgi;
    }

    location ~ .+\.php$ {
        location ~ \..*/.*\.php$ { return 400; }
        error_page 418 = @fastcgi;
        return 418;
    }

    location @fastcgi {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        ...
    }
}
相关问题