【问题标题】:nginx returns 404 for css/static filesnginx 为 css/静态文件返回 404
【发布时间】:2018-08-18 22:10:42
【问题描述】:

我在 docker 容器中的 nginx 上运行 symfony,具有以下非常基本的 default.conf:

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 之前,下面的 sn-p 不应该先返回现有文件(我从 symfony 收到 404 错误,所以即使文件存在,它也会以某种方式重写 index.php):

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

编辑:我没有使用资产。

【问题讨论】:

    标签: php .htaccess web-services symfony nginx


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-07
      • 2017-06-05
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 2023-02-18
      • 2018-09-28
      • 2016-09-24
      相关资源
      最近更新 更多