Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisburton's avatar

Nginx setup for subdirectory

Quick info:

I have my main site: site.com and I installed Laravel 5 in a subdirectory named 'laravel'.

I tried setting up a virtual host laravel.conf but I'm not able to get anything working. Any tutorial I try to follow, it's usually for setting up Laravel in a subdirectory but accessing it at the root domain level. I want to access Laravel via site.com/laravel and not site.com.

Laravel.conf file

server {
    listen 80;

    root /var/www/html/laravel/public;
    index index.php index.html index.htm;

    server_name site.com;

    access_log /var/log/nginx/localhost.laravel-access.log;
    error_log  /var/log/nginx/locahost.laravel-error.log error;


    location /laravel {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt  { log_not_found off; access_log off; }


# ERROR
    error_page 404 /index.php; 


# HHVM
    include hhvm.conf; 


# DENY HTACCESS   
    location ~ /\.ht {
        deny all;
    }
}
0 likes
19 replies
bashy's avatar

First, put the main "root" to the top level path (which shows laravel folder).

root /var/www/html;

Now change this part so when you view /laravel it will have a new root.

location /laravel {
    root /public;
    try_files $uri $uri/ /index.php?$query_string;
}

*Not tested but you can play around with the root part inside the location, you may need to use

root /laravel/public;
chrisburton's avatar

@bashy

Hmm. Neither root options seem to work. Let me paste my default.conf nginx file. Just in case that has anything to do with it. Also, thank your for taking time to help me out on this one. I really appreciate it.

default.conf

server {
    listen 80;
    server_name www.chrisburton.me;
    return 301 $scheme://chrisburton.me$request_uri;
}

server {
    listen 80 default_server;
    #listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.php index.html index.htm;

    # Make site accessible from http://chrisburton.me/
    server_name chrisburton.me www.chrisburton.me;

    # HHVM Config
    include hhvm.conf;

    location / {
        # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /;
    }



    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }



    # Deny access to .htaccess
    location ~ /\.ht {
        deny all;
    }



    # Error Pages
    error_page 404 /;

    # 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;
    #}
}

laravel.conf

server {
    listen 80;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name chrisburton.me;

    access_log /var/log/nginx/localhost.laravel-access.log;
    error_log  /var/log/nginx/locahost.laravel-error.log error;


    location /laravel {
        root /laravel/public;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt  { log_not_found off; access_log off; }


    # ERROR
    error_page 404 /index.php; 


    # HHVM
    include hhvm.conf; 


    # DENY HTACCESS   
    location ~ /\.ht {
        deny all;
    }
}

Note: /laravel = 403 Forbidden while /laravel/public produces a blank page.

bashy's avatar

Oh sorry, un-enable the laravel.conf file and put the location block in the default one.

location /laravel {
    root /laravel/public;
    try_files $uri $uri/ /index.php?$query_string;
}

Not sure how log files work so you could put them in the location block, never tried or looked into it.

bashy's avatar

Yeah, may need something like this

location /laravel/ {
    try_files $uri $uri/ /laravel/public/index.php?$args;
}
chrisburton's avatar

Same result. It only work on /public. I think I may just give up here. I really appreciate your help on this.

bashy's avatar

Just done some testing and this works for me. Although you may need to rename the URI part to something else than what the actual folder IS. Since it will always show the directory over the rewrite.

location /laravel {
    try_files $uri $uri/ /laradir/public/index.php?q=$uri&$args;
}
chrisburton's avatar

@bashy That doesn't work either. I'm wondering if there's something within my application that I need to change?

bashy's avatar

Show us what directories you have and what your Nginx configs look like. I've tested it myself and it works fine.

Also what exactly "doesn't" work? Message? Snippet from logs?

chrisburton's avatar

@bashy Hope this helps.

Log:

2015/01/16 04:15:28 [error] 3969#0: *10 directory index of "/var/www/html/laravel/" is forbidden, client: 194.145.208.63, server: chrisburton.me, request: "GET /laravel/ HTTP/1.0", host: "chrisburton.me"
2015/01/16 05:54:35 [error] 4852#0: *1 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 05:54:39 [error] 4852#0: *1 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 05:56:46 [error] 4877#0: *3 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 05:56:51 [error] 4877#0: *3 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 06:02:37 [error] 4877#0: *8 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 06:03:34 [error] 4877#0: *8 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 15:30:34 [error] 11064#0: *166 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 15:31:35 [error] 11064#0: *166 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 15:31:36 [error] 11064#0: *166 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 15:34:42 [error] 11207#0: *168 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 15:34:44 [error] 11207#0: *168 directory index of "/var/www/html/laravel/" is forbidden, client: 70.174.86.2, server: chrisburton.me, request: "GET /laravel/ HTTP/1.1", host: "chrisburton.me"
2015/01/16 15:35:13 [emerg] 11237#0: unknown "forbidden" variable
2015/01/16 15:35:39 [emerg] 11263#0: unknown "forbidden" variable

Nginx config:

server {
    listen 80;
    server_name www.chrisburton.me;
    return 301 $scheme://chrisburton.me$request_uri;
}
server {
    listen 80 default_server;
    #listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.php index.html index.htm;

    # Make site accessible from http://chrisburton.me/
    server_name chrisburton.me;

    # HHVM Config
    include hhvm.conf;

    location / {
        # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /;
    }




    # LARAVEL

    location /laravel {
        try_files $uri $uri/ /laradir/public/index.php?q=$uri&$args;
    }





    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #location ~ \.php$ {
    #    fastcgi_split_path_info ^(.+\.php)(/.+)$;

        # With php5-fpm:
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
    #    fastcgi_index index.php;
    #    include fastcgi_params;
    #}



    # Deny access to .htaccess
    location ~ /\.ht {
        deny all;
    }



    # Error Pages
    error_page 404 /;

    # 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;
    #}
}

root = var/www/html (This is where all my site files are located)

Directories:

chrisburton's avatar
chrisburton
OP
Best Answer
Level 3

Solved it by just pointing it to a subdomain.

I really appreciate your help, @bashy. Thanks for trying.

bashy's avatar

Well for one you don't have matching directory names...

try_files $uri $uri/ /laradir/public/index.php?$query_string;

Doesn't match the folder you have called "laravel". Rename the folder to something other than laravel so it rewrites properly.

chrisburton's avatar

@bashy Even when I tried the following examples after changing laravel subdirectory to laradir, nothing worked:

location /laravel {
    try_files $uri $uri/ /laradir/public/index.php?$query_string;
}
location /laradir {
    try_files $uri $uri/ /laradir/public/index.php?$query_string;
}
bashy's avatar
  • Actual folder name = laradir
  • URI segment name = /laravel
  • Location = /laravel
  • Rewrite = /laradir/

Works for me. Also can't help when "nothing worked" does not say what happened or what the error was.

Sub domain is fine, if that's what you want, I'll leave this here in case.

chaospower's avatar

Please take a look at

http://serverfault.com/questions/218818/nginx-projects-in-subfolders and http://stackoverflow.com/questions/27785372/config-nginx-for-laravel-in-a-subfolder

location ~ /laravel/.+\.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME /var/www/wiki/public$fastcgi_script_name;
}

Seems to be a fastcgi issue. That's why we are getting 403. Your root should be a full path.

http://stackoverflow.com/questions/16683046/how-to-install-laravel-4-to-a-web-host-subfolder-without-publicly-exposing-app

http://stackoverflow.com/questions/27785372/config-nginx-for-laravel-in-a-subfolder

bashy's avatar

@cent040 Thanks. If the above person is right, it should just need this (I had it already set when testing).

# if the next line in yours still contains $document_root
# consider switching to $request_filename as it provides
# better support for directives such as alias
fastcgi_param  SCRIPT_FILENAME    $request_filename;

Please or to participate in this conversation.