Nginx setup for subdirectory

Published 3 years ago by chrisburton

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;
    }
}
Best Answer (As Selected By chrisburton)
chrisburton

Solved it by just pointing it to a subdomain.

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

bashy
bashy
3 years ago (1,002,020 XP)

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

@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
bashy
3 years ago (1,002,020 XP)

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.

chrisburton

Ok. I've finally got it working, somewhat. It works if I visit http://chrisburton.me/laravel/public but I get a 403 forbidden at /laravel.

How can I resolve this to make it viewable at /laravel?

bashy
bashy
3 years ago (1,002,020 XP)

Yeah, may need something like this

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

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

bashy
bashy
3 years ago (1,002,020 XP)

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

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

bashy
bashy
3 years ago (1,002,020 XP)

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

@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

@maytham That's for setting up multiple domains with Nginx. Unrelated.

chrisburton
chrisburton

Solved it by just pointing it to a subdomain.

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

bashy
bashy
3 years ago (1,002,020 XP)

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

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

Please sign in or create an account to participate in this conversation.