0

我的问题是在Nginx服务器(Ubuntu)上使用Laravel 5.4来提供CSS和JS文件。我已将服务器配置为从嵌套子目录(不在Web服务器的根目录中)为应用程序提供服务。路由正在工作。请参阅:Laravel 5.4在Nginx的子目录中提供。 404当包括js文件

问题是,我不能包括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 /职位尝试。 js'和许多其他变体,包括硬编码路径,如'zwestwind.com/sandboxes/zypth/blog/public/posts.js'无济于事(与prot由于声望,我只能发布2个链接)。
  • 添加位置块:location〜.js $ {...}添加应用程序的内容头/ x-javascript
  • 我已经配置了vhost服务器以在(http)博客上提供相同的应用程序。 zwestind.com/posts。所有路由和CSS/JS文件包含在那里工作,检查出来。该子域的服务器配置与Laravel安装指南中的指定完全相同。

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

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

谢谢。

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

回答

0

尝试包括这样的文件:

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

在“/”之前的目录将请求发送到根(公用文件夹),其中JS和CSS目录是。

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

我对图像做同样的事情,它工作正常。