# Location problem with static content

**URL:** <https://www.ruby-forum.com/t/location-problem-with-static-content/237204>\
**Category:** NGINX\
**Created:** [October 29, 2013, 7:35pm UTC](https://www.ruby-forum.com/t/location-problem-with-static-content/237204 "2013-10-29T19:35:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [October 29, 2013, 7:35pm UTC](https://www.ruby-forum.com/t/location-problem-with-static-content/237204/1 "2013-10-29T19:35:02Z")

</div>

Hello,

I have created for my GitLab installation this entries in the  
configuration:

```
location /gitlab {
    root /home/gitlab/gitlab/public;
    try_files $uri $uri/index.html $uri.html @gitlab;
}

location @gitlab {
    proxy_read_timeout 300;
    proxy_connect_timeout 300;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;

    proxy_pass http://localhost:9080;
}

```

so I can use GitLab with [https://myserver/gitlab](https://myserver/gitlab), most functions of  
GitLab works well, but I have got a problem with the static content.  
I get this error:

[error] 4573#0: \*4 open() “/home/www/static.css” failed (2: No such file  
or directory)

The message is correct, because the main root dir (above the location)  
is set to /home/www, so the fallback mechanism works.  
But in my case I need a correct rule, that also static content, which  
have got the /gitlab URL part are mapped into /home/gitlab/gitlab/public

How can I tell my location rule, that static content is stored in the  
correct root folder?

Thanks

Phil

---

<div class="post-metadata">

**Author:** ![Philipp\_Kraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@Philipp\_Kraus](https://www.ruby-forum.com/u/Philipp_Kraus)\
**Post date:** [October 29, 2013, 9:31pm UTC](https://www.ruby-forum.com/t/location-problem-with-static-content/237204/2 "2013-10-29T21:31:18Z")

</div>

On Tue, Oct 29, 2013 at 07:34:15PM +0100, Philipp Kraus wrote:

Hi there,

> ```
> location /gitlab {
> root /home/gitlab/gitlab/public;
> try_files $uri $uri/index.html $uri.html @gitlab;
> 
> ```

I suspect that the “$uri/index.html” there may cause you problems. You  
may be better off using “$uri/” instead.

> I get this error:
> 
> [error] 4573#0: \*4 open() “/home/www/static.css” failed (2: No such file or  
> directory)

What url did you access to get this message?

What file on the filesystem did you want it to serve for that url?

> How can I tell my location rule, that static content is stored in the correct  
> root folder?

You may need to use “alias” rather than “root”; but that should become  
clear when you describe the url -\> filename mapping that you want.

## f

Francis D. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Philipp\_Kraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@Philipp\_Kraus](https://www.ruby-forum.com/u/Philipp_Kraus)\
**Post date:** [October 30, 2013, 11:16am UTC](https://www.ruby-forum.com/t/location-problem-with-static-content/237204/3 "2013-10-30T11:16:24Z")

</div>

Am 29.10.2013 um 21:30 schrieb Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Tue, Oct 29, 2013 at 07:34:15PM +0100, Philipp Kraus wrote:
> 
> Hi there,
> 
> > location /gitlab {  
> > root /home/gitlab/gitlab/public;  
> > try\_files $uri $uri/index.html $uri.html @gitlab;
> 
> I suspect that the “$uri/index.html” there may cause you problems. You  
> may be better off using “$uri/” instead.

I have changed it to

```
location /gitlab {
    alias /home/gitlab/gitlab/public;
    try_files $uri/ @gitlab;
}

```

that does not work also. I have tested it with the “try\_files $uri  
$uri.css”.

> > I get this error:
> > 
> > [error] 4573#0: \*4 open() “/home/www/static.css” failed (2: No such file or  
> > directory)
> 
> What url did you access to get this message?
> 
> What file on the filesystem did you want it to serve for that url?

The message shows:

2013/10/30 11:10:18 [error] 6692#0: \*5 open() “/home/www/static.css”  
failed (2: No such file or directory), client: , server: ,  
request: “GET /static.css HTTP/1.1”, host: “”, referrer:  
“[https://server/gitlab/profile/keys](https://server/gitlab/profile/keys)”

> > How can I tell my location rule, that static content is stored in the correct  
> > root folder?
> 
> You may need to use “alias” rather than “root”; but that should become  
> clear when you describe the url → filename mapping that you want.

I try to port this configuration

> <https://github.com/gitlabhq/gitlabhq/blob/master/lib/support/nginx/gitlab>

to the subdirectory, so GitLab is not called on [https://myserver/](https://myserver/) but  
rather [https://myserver/gitlab](https://myserver/gitlab)

Thanks for help

Phil

---

<div class="post-metadata">

**Author:** ![Philipp\_Kraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@Philipp\_Kraus](https://www.ruby-forum.com/u/Philipp_Kraus)\
**Post date:** [October 30, 2013, 7:48pm UTC](https://www.ruby-forum.com/t/location-problem-with-static-content/237204/4 "2013-10-30T19:48:42Z")

</div>

On Wed, Oct 30, 2013 at 11:15:42AM +0100, Philipp Kraus wrote:

> Am 29.10.2013 um 21:30 schrieb Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > On Tue, Oct 29, 2013 at 07:34:15PM +0100, Philipp Kraus wrote:

Hi there,

There are a few possible different reasons for things not to be working  
the way you want.

The best chance of getting things fixed if is there is clarity about  
what was tried and failed.

The best information is of the form “I did A, I got B, but I expected  
to get C”.

> ```
> alias /home/gitlab/gitlab/public;
> try_files $uri/ @gitlab;
> }
> 
> ```

That’s not what I intended to suggest you do.

try\_files $uri $uri/ $uri.html @gitlab;

where the $uri.html part is presumably gitlab-specific.

And “root” vs “alias” depends on what url to filename mapping you want  
to have.

> that does not work also.

So, next time, can you do something like

curl -i [http://server/gitlab](http://server/gitlab)

and see how what you get differs from what you expect to get?

What I’m guessing is that the file  
/home/gitlab/gitlab/public/gitlab/index.html contains something  
like . When you access the file via the  
url [http://server/gitlab](http://server/gitlab), the browser follows the link and asks for  
[http://server/static.css](http://server/static.css) – which nginx expects to refer to the file  
/home/www/static.css.

If you use the suggested try\_files, then when you access  
the url [http://server/gitlab](http://server/gitlab), you will be redirected to the  
url [http://server/gitlab/](http://server/gitlab/); when you access _that_ url, you’ll  
get the same file content, but now the browser will ask for  
[http://server/gitlab/static.css](http://server/gitlab/static.css), which nginx expects to refer to the  
file /home/gitlab/gitlab/public/gitlab/static.css.

> > What url did you access to get this message?
> > 
> > What file on the filesystem did you want it to serve for that url?
> 
> The message shows:
> 
> 2013/10/30 11:10:18 [error] 6692#0: \*5 open() “/home/www/static.css” failed (2:  
> No such file or directory), client: , server: ,  
> request: “GET /static.css HTTP/1.1”, host: “”, referrer:  
> “[https://server/gitlab/profile/keys](https://server/gitlab/profile/keys)”

So, the url is /static.css.

nginx tries to return the file /home/www/static.css.

Which file on the filesystem do you want nginx to return, when it gets  
a request for /static.css?

> I try to port this configuration  
> [gitlabhq/lib/support/nginx/gitlab at master · gitlabhq/gitlabhq · GitHub](https://github.com/gitlabhq/gitlabhq/blob/master/lib/support/nginx/gitlab)  
> to the subdirectory, so GitLab is not called on [https://myserver/](https://myserver/) but rather  
> [https://myserver/gitlab](https://myserver/gitlab)

From the extra log line, I guess that perhaps the file  
/home/gitlab/gitlab/public/gitlab/index.html contains instead something  
like . If that is the case, then the best  
thing you can do if you want to proxy the application “gitlab” behind  
the non-root-url /gitlab/, is to configure the application so that it  
knows that its root url is /gitlab/, not /.

After you’ve done that, the nginx config should be simpler.

Good luck with it,

## f

Francis D. removed\_email\_address@domain.invalid
