nginx为css / static文件返回404

时间:2018-03-10 12:18:07

标签: php .htaccess web-services symfony nginx

我在一个docker容器中使用以下非常基本的default.conf在nginx上运行symfony:

server {
    server_name localhost;
    root /var/www/symfony/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
}

我的nginx.conf看起来像这样:

worker_processes 4;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

pid         /var/run/nginx.pid;

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile on;

    keepalive_timeout 65;

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript
               text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
}

不知怎的,我无法从public /目录中获取静态文件,nginx说无法找到/var/www/symfony/public/style.css(404) - 但该文件肯定存在于给定路径中。所以我猜我的nginx配置一定是错的,但我找不到错误。在重写为index.php之前,以下代码段不应该首先返回现有文件(我从symfony收到404错误,所以即使文件存在,它也会重写为index.php):

location / {
  try_files $uri /index.php$is_args$args;
}

编辑:我没有使用资产。

1 个答案:

答案 0 :(得分:0)

解决了它,我不得不重写css文件夹。缺少的是:



location ~ ^/css/*(/|$) {
  access_log off;
  expires max;
}