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

问题描述 投票:0回答:1

我的问题是在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上配置了一个vhost服务器来提供相同的应用程序。 CSS / JS文件的所有路由和包含都在那里工作,检查出来。该子域的服务器配置与Laravel安装指南中指定的完全相同。

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

当脚本工作时,它会说“posts.js已准备就绪!”在控制台中(有关示例,请参阅http上的子域)。

谢谢。

附:如果有人好奇为什么我想在嵌套目录中提供这个应用程序(以及更多),那是因为我想通过ONE域使用ONE SSL证书来托管实时演示应用程序,同时还学习如何配置Web服务器。此服务器不适用于繁忙的流量。

javascript laravel nginx http-status-code-404 subdirectory
1个回答
0
投票

尝试包含这样的文件:

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

目录之前的“/”将请求发送到根(公共文件夹),其中js和css目录是。

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

我对图像做同样的事情并且工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.