0

I'm trying to move my theme files outside of document root. I'm using nginx for the server and I've tried using alias and root directives in a location block. Nothing I've tried works and I suspect it's the try_files directive inside location /. I have my themes files in a folder at /var/www/naturesflowllc/ui/assets

Here is my nginx conf file.

server {
    listen 80;
    listen [::]:80;
    server_name my.beupnow.dev.kahunacoding.com;

    root /var/www/naturesflowllc/mybeupnow/public_html;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_read_timeout 180;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location /assets/ {
       alias /var/www/naturesflowllc/ui/assets/;
       # root /var/www/naturesflowllc/ui/;

       autoindex off;
       # autoindex on;
    }

    location ~ /\.ht {
        deny all;
    }

    error_log  /var/www/naturesflowllc/application/logs/my_beupnow_error.log debug;
    access_log /var/www/naturesflowllc/application/logs/my_beupnow_access.log;
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
}

I've tried alias and root and autoindex on and off nothing seems to work.

EDIT: So I want to have my assets served like this:

<link href="/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

and this:

<script src="/assets/global/plugins/jquery.min.js" type="text/javascript"></script>

from my ui folder which is like this:

|____assets
| |____apps
| | |____css
| | | |____inbox.css
| | | |____inbox.min.css
| | | |____ticket.css
| | | |____ticket.min.css
| | | |____todo-2.css
| | | |____todo-2.min.css
| | | |____todo.css
| | | |____todo.min.css
| | |____img
| | |____scripts
| | | |____calendar.js
| | | |____calendar.min.js
| | | |____inbox.js
| | | |____inbox.min.js
| | | |____todo-2.js
| | | |____todo-2.min.js
| | | |____todo.js
| | | |____todo.min.js
| |____global
| | |____css
| | | |____components-md.css
| | | |____components-md.min.css
| | | |____components-rounded.css
| | | |____components-rounded.min.css
| | | |____components.css
| | | |____components.min.css
| | | |____plugins-md.css
| | | |____plugins-md.min.css
| | | |____plugins.css
| | | |____plugins.min.css
| | |____img
| | | |____accordion-plusminus.png
| | | |____ajax-loading.gif
| | | |____ajax-modal-loading.gif
| | | |____datatable-row-openclose.png
... (truncated)

I want to remove the assets folder from here /var/www/naturesflowllc/mybeupnow/public_html and relocate it to /var/www/naturesflowllc/ui/assets/

I hope this clarifies what I'm trying to achieve.

EDIT: I wasted a couple of hours on this only to find out I was editing the wrong nginx.conf file 8-(. Works like it should with either root or alias.

1
  • Please edit your question to include a sample URL to an asset file, and the corresponding path of the file on the file system. Root seems like the proper directive in this case, you just have to get the parameter to match the URL.
    – Tim
    Oct 18, 2017 at 17:53

1 Answer 1

1

https://nginx.ru/en/docs/http/ngx_http_core_module.html#alias When location matches the last part of the directive’s value:

location /images/ {
    alias /data/w3/images/;
}

it is better to use the root directive instead:

location /images/ {
    root /data/w3;
}

It's your case.


I create an example for you and tried it at my ubuntu:

server {
        listen 80;
        server_name 127.0.0.1;
        root /tmp/www/;

        # 1s location
        location / {
            try_files $uri $uri/ /index.php;
        }

        # 2nd location
        location ~ \.php$ {
            proxy_pass http://127.0.0.1:8080;
        }

        #3d location
        location /assets/ {
        root /tmp/somedir/;
        }
}

http://127.0.0.1/ - "1st location (searching index file in /tmp/www), then 2nd location because where is no index file in my /tmp/www"

http://127.0.0.1/assets/some.js - "3d location (returns /tmp/somedir/assets/some.js)"

It works for me with nginx 1.10.3. If not for you, so please provide your nginx access and error logs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.