question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NginX /location/ works but without img, css and js

See original GitHub issue

I’m trying to selfhost grocy under the subdirectory /grocy/, I can move between /grocy/stockoverview and /grocy/calendar etc., but it won’t load any css, image or script.

I’ve installed it from the AUR, here is my configuration: /etc/webapps/grocy/config.php

Setting('MODE', 'demo');
Setting('BASE_PATH', '/grocy');
Setting('BASE_URL', '/grocy');

/etc/nginx/nginx.conf

[...]
http {
    server  {
        listen 443 ssl http2 default_server;
        [...]
        location /grocy/ {
            proxy_pass http://127.0.0.1:5555;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_buffering off;
           proxy_request_buffering off;
        }
        [...]
    }
    server  {
        listen 127.0.0.1:5555;
        root /usr/share/webapps/grocy/public/;
        index index.php;
        location /  {
            try_files $uri /index.php$is_args$query_string;
            add_header X-Content-Type-Options "" always;
        }
        location ~* \.(php|inc)$    {
            include fastcgi.conf;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
        }
    } 
}

/etc/systemd/system/php-fpm.service

[...]
ReadWritePaths=/usr/share/webapps/grocy/data/
[...]

URLs are generated like this:

<link rel="icon" type="image/png" sizes="16x16" href="/grocy/img/appicons/favicon-16x16.png?v=2.6.1">
<link rel="manifest" href="/grocy/img/appicons/site.webmanifest?v=2.6.1">

[..]
<li class="nav-item nav-item-sidebar" data-toggle="tooltip" data-placement="right" title="Stock overview" data-nav-for-page="stockoverview">
    <a class="nav-link discrete-link" href="/grocy/stockoverview">
        <i class="fas fa-box"></i><span class="nav-link-text">Stock overview</span>
    </a>
</li>

If I try to open /grocy/img/appicons/favicon-16x16.png?v=2.6.1 I get:

Type: Slim\Exception\HttpNotFoundException
Code: 404
Message: Not found.
File: /usr/share/webapps/grocy/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php
Line: 93

If I try to change ‘BASE_URL’ to “/grocy/public/”, where the static assets seems to be located, I get the above error anyway, and I also get that error with the previously working URLs like /grocy/calendar.

This is what I see under /grocy/calendar calendar


EDIT I get the same results with nginx.conf

[...]
        location /grocy/ {
            proxy_pass http://127.0.0.1:5555/grocy/;
[...]
        location /grocy/  {
            try_files $uri /index.php$is_args$query_string;
[...]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
berrndcommented, Mar 23, 2020

Thanks for the feedback.

0reactions
AlphaJackcommented, Mar 23, 2020

I’ve already tried it, but unfortunately that’s not the issue.
I finally found a way to make it work: remove the BASE_PATH and add a trailing slash in the proxy_pass directive. These are the changes I made to my original configuration in the first post, if someone else can test this subdirectory configuration I think it may be an useful addition to the community installation guides.

/etc/webapps/grocy/config.php

Setting('BASE_PATH', '');        # left it empty, resources will be loaded from 127.0.0.1:5555/
Setting('BASE_URL', '/grocy');   # needed to generate URLs that match the NginX location below

/etc/nginx/nginx.conf

[...]
        location /grocy/ {       # location of the public 0.0.0.0:443 server
            proxy_pass http://127.0.0.1:5555/; #added the trailing slash here
[...]
        location /  {            # location of the internal 127.0.0.1:5555 server
            try_files $uri /index.php$is_args$query_string;
[...]
Read more comments on GitHub >

github_iconTop Results From Across the Web

nginx not serving css/js images - Stack Overflow
You have one location that sends all requests to PHP. You are not serving static files (such as js/css) with Nginx.
Read more >
Pitfalls and Common Mistakes | NGINX
This works. Putting root inside of a location block will work and it's perfectly valid. What's wrong is when you start adding location...
Read more >
Nginx ingress - all of css js and images 404 · Issue #333
I can access it via http, but only the html is accessible, all of its css js and images 404. ingress apiVersion: extensions/v1beta1...
Read more >
Nginx location directive examples - DigitalOcean
The location directive within NGINX server block allows to route request to correct location within the file system. The directive is used ...
Read more >
How to configure nginx to find subfolders for /js /css related to ...
I'm not sure why your index.html did work though not having tried it myself. One reason could be that because your location coincidentally ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found