title: Laravel 5.4 Served in subdirectory Nginx. 404 when including js files
body: <p>My problem is serving CSS and JS files using Laravel 5.4 on an Nginx server (Ubuntu). I have configured the server to serve the application from a nested sub-directory (not in the root of the web server). Routing is working. See:</p>
  
  <ul>
  <li><a href="https://zwestwind.com/sandboxes/zypth/blog/tasks" rel="nofollow noreferrer">https://zwestwind.com/sandboxes/zypth/blog/tasks</a></li>
  <li><a href="https://zwestwind.com/sandboxes/zypth/blog/posts" rel="nofollow noreferrer">https://zwestwind.com/sandboxes/zypth/blog/posts</a></li>
  <li>and just ..blog/ as well.</li>
  </ul>
  
  <p>The problem is that I cannot include JS files (and likely css). The JS file is in "blog/public/js/posts.js".</p>
  
  <p>Attached below is my site configuration, and the code to include a JS file in a blade template. You can view the error in the console when you hit the site.</p>
  
  <p>Server Configuration:</p>
  
  <pre><code>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;
      #}
  }
  </code></pre>
  
  <p>Blade code to include the JS file:</p>
  
  <pre><code>&lt;script src="{{ URL::asset('posts.js', true) }}"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>Things that I have tried:</p>
  
  <ul>
  <li>I have tried using 'js/posts.js' and many other variations, including hardcoded paths such as 'zwestwind.com/sandboxes/zypth/blog/public/posts.js' to no avail (with the protocol, I can only post 2 links due to reputation).</li>
  <li>Adding a location block: location ~ .js$ {...} to add a content-header of application/x-javascript</li>
  <li>I have configured a vhost server to serve the same application at (http)blog.zwestind.com/posts. All routing and inclusion of CSS/JS files works there, check it out. The server config for that sub domain is exactly as specified in the Laravel installation guide.</li>
  </ul>
  
  <p>I am guessing this issue stems from a server config problem with the primary domain... ie) the location block listening for "^/sandboxes/zypth/blog" is catching the request for the JS file and modifying the URI, thus resulting in a 404. How would I fix this?</p>
  
  <p>When the script works, it will say "posts.js is ready!" in the console (see the sub domain on http for an example).</p>
  
  <p>Thanks.</p>
  
  <p>P.S. If anyone is curious as to WHY I want to serve this app (and more) in a nested directory, it is because I want to host live demo apps using ONE SSL cert via ONE domain while also learning how to configure web servers. This server is not intended for heavy traffic.</p>