繁体   English   中英

Laravel 5.4在子目录Nginx中提供。 包含js文件时为404

[英]Laravel 5.4 Served in subdirectory Nginx. 404 when including js files

我的问题是在Nginx服务器(Ubuntu)上使用Laravel 5.4提供CSS和JS文件。 我已将服务器配置为从嵌套子目录(不在Web服务器的根目录中)为应用程序提供服务。 路由正常。 看到:

问题是我无法包含JS文件(可能还包括CSS)。 JS文件位于“ blog / public / js / posts.js”中。

下面随附的是我的站点配置,以及在刀片模板中包含JS文件的代码。 当您访问站点时,可以在控制台中查看错误。

服务器配置:

server {
    return 404;
}

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    listen 443 ssl;

    root /var/www/zwestwind.com/html;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    #server_name example.com www.example.com;
    server_name zwestwind.com www.zwestwind.com;

    ssl on;
    ssl_certificate /etc/nginx/ssl/pub_clf_origin.pem;
    ssl_certificate_key /etc/nginx/ssl/priv_clf_origin.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    # route to laravel project: $uri = /sandboxes/zypth/blog/public/index.php
    location ~ ^/sandboxes/zypth/blog {
        alias /var/www/zwestwind.com/html/sandboxes/zypth/blog/public;
        try_files $uri $uri/ @laravel;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            #fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/zwestwind.com/html/sandboxes/zypth/blog/public/index.php;
        }
    }

    location @laravel{
        rewrite /sandboxes/zypth/blog/(.*)$ /sandboxes/zypth/blog/index.php?/$1 last;
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

包含JS文件的刀片代码:

<script src="{{ URL::asset('posts.js', true) }}"></script>

我尝试过的事情:

  • 我尝试使用'js / posts.js'和许多其他变体,包括诸如'zwestwind.com/sandboxes/zypth/blog/public/posts.js'之类的硬编码路径都没有用(对于该协议,我只能发布2个链接(由于声誉)。
  • 添加位置块:location〜.js $ {...}以添加application / x-javascript的内容标题
  • 我已经在(http)blog.zwestind.com/posts中将虚拟主机服务器配置为服务相同的应用程序。 CSS / JS文件的所有路由和包含都可以在此处进行操作,请查看。 该子域的服务器配置与Laravel安装指南中完全相同。

我猜这个问题源于主域的服务器配置问题……即,监听“ ^ / sandboxes / zypth / blog”的位置块正在捕获JS文件的请求并修改URI,从而导致404。我该如何解决?

当脚本运行时,它将显示“ posts.js准备好了!” 在控制台中(有关示例,请参见http上的子域)。

谢谢。

PS:如果有人对为什么要在嵌套目录中提供此应用(以及更多)感到好奇,那是因为我想通过ONE域使用ONE SSL证书托管实时演示应用,同时还要学习如何配置Web服务器。 该服务器不适合大流量。

尝试包括这样的文件:

<script src="/js/posts.js"></script>

目录前的“ /”将请求发送到js和css目录所在的根(公用文件夹)。

我发现这样做更好,然后在nginx配置中添加了新的位置根或别名。

我对图像也是如此,并且效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM