资产未加载

时间:2020-03-26 06:06:15

标签: php symfony fosrestbundle nelmioapidocbundle

我有问题,我的资产未加载-找不到404

"nelmio/api-doc-bundle": "^3.6@dev"

我执行了php bin/console assets:installphp bin/console assets:install --symlink

本地有效,但是在测试服务器上无效...有人可以对此有所了解吗?

本地 要求网址: http://symfony.localhost/bundles/nelmioapidoc/swagger-ui/swagger-ui-bundle.js-正确加载

在测试服务器上 http://11.11.11.11/bundles/nelmioapidoc/swagger-ui/swagger-ui.css未加载

并返回错误

in /var/www/symfony/vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php (line 163)
Format 'html' not supported, handler must be implemented

我检查/var/www/symfony/public/bundles中的文件,看起来正确,文件nelmioapidoc中存在。我错过了什么?

fos rest配置:

fos_rest:
    body_listener:
        service: my_body_listener
    unauthorized_challenge: "Basic realm=\"Restricted Area\""
    access_denied_listener:
        # all requests using the 'json' format will return a 403 on an access denied violation
        json: true
    param_fetcher_listener: force
    format_listener:
        rules:
            - { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json ] }
            - { path: ^/, priorities: [ json, xml, html ], fallback_format: ~, prefer_extension: true }
    view:
        view_response_listener: 'force'
        formats:
            json: true
            jsonp: false
            xml: false
            rss: false
        mime_types:
            json: ['application/json', 'application/x-json']
    routing_loader:
        default_format:  json
    exception:
        enabled: true
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
            'Symfony\Component\HttpKernel\Exception\BadRequestHttpException': 400
        messages:
            Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
            Symfony\Component\HttpKernel\Exception\HttpException: true

但是我想这与fos rest没有关系。由于这是资产资源,因此应在不进行处理的情况下加载它。最重要的是,本地炒锅没有任何问题

另一件事,也许是服务器出现问题。我将Docker与nginx映像一起使用。这是我的conf nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

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

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
  open_file_cache max=100;
  client_body_temp_path /tmp 1 2;
  client_body_buffer_size 256k;
  client_body_in_file_only off;
}

daemon off;

和主机

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


    location / {
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /index.php/$1 last;
    }

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

    error_log /var/log/nginx/symfony_error.log;
    access_log /var/log/nginx/symfony_access.log;
}

测试服务器中文件的状态(我添加chmod 777 -R供应商/ nelmio) 在执行assts:install --symlink时(与推进值相同,没有--symlink时)

/var/www/symfony # stat public/bundles/nelmioapidoc/swagger-ui/swagger-ui.css
  File: public/bundles/nelmioapidoc/swagger-ui/swagger-ui.css
  Size: 154006      Blocks: 304        IO Block: 4096   regular file
Device: fc01h/64513d    Inode: 1038685     Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-03-26 08:43:56.025515192 +0000
Modify: 2020-03-25 16:45:47.667069605 +0000
Change: 2020-03-26 08:42:59.921460536 +0000
 Birth: -

有关本地比较示例(工作正常)

    /var/www/symfony # stat public/bundles/nelmioapidoc/swagger-ui/swagger-ui.css
  File: public/bundles/nelmioapidoc/swagger-ui/swagger-ui.css
  Size: 154006      Blocks: 304        IO Block: 4096   regular file
Device: 805h/2053d  Inode: 5797767     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-03-25 16:12:02.252465289 +0000
Modify: 2020-03-25 16:10:42.100983234 +0000
Change: 2020-03-25 16:10:42.100983234 +0000
 Birth: -

1 个答案:

答案 0 :(得分:0)

我通过对nginx default.conf进行一些更改来解决了这个问题,例如:

set $root_path '/var/www/{YOUR_SYMFONY_PROJECT}/public';

root $root_path;

location ~* ^/(bundles)/(.+)$ {
    root $root_path;
}

希望我能帮助您