kabra
kabra

Reputation: 1316

nginx change root folder for specific url

I have a configuration file like this one below:

    server {

        listen       80;
        server_name  localhost;

        #charset utf-8;
        root   html/laravel/public;
        index  index.html index.php;

        #browse folders if no index file
        autoindex on;

        # enforce NO www
        if ($host ~* ^www\.(.*))
        {
            set $host_without_www $1;
            rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
        }

        # serve static files directly
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
            access_log off;
            #expires max;
        }

        # removes trailing slashes (prevents SEO duplicate content issues)
        if (!-d $request_filename)
        {
            rewrite ^/(.+)/$ /$1 permanent;
        }

        # canonicalize codeigniter url end points
        # if your default controller is something other than "welcome" you should change the following
        # if ($request_uri ~* ^(/lobby(/index)?|/index(.php)?)/?$)
        # {
        #     rewrite ^(.*)$ / permanent;
        # }

        # removes trailing "index" from all controllers
        if ($request_uri ~* index/?$)
        {
            rewrite ^/(.*)/index/?$ /$1 permanent;
        }

        # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
        if (!-e $request_filename)
        {
            rewrite ^/(.*)$ /index.php?/$1 last;
            break;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /backend/ {
            root /html/frontend;
        }

        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }

        # catch all
        # error_page 404 /index.php;

        # location ~ \.php$ {
        # try_files $uri =404;
        #         fastcgi_pass  unix:/tmp/php.socket;
        #         fastcgi_index index.php;
        #         #include fastcgi_params;
        #         include /home/tamer/code/nginx/fastcgi_params;
        # }
        # access_log /home/tamer/code/laravel/storage/logs.access.log;
        # error_log  /home/tamer/code/laravel/storage/logs.error.log;
    }

I have to change root folder to html/backend for any url with $host/backend/. All rules for load pages have to be the same, only root folder have to change.

How can I do that?

Upvotes: 17

Views: 51859

Answers (6)

xsilen T
xsilen T

Reputation: 1753

server {
  location / {
    root /data/www;
  }

  location /images/ {
    root /data;
    rewrite ^/images/(.+?)$ $1 break; #following is the explation
  }
}
  • use break to continue; the root in location will take effect
  • use last to internal simulate request; the root in location maybe will not take effect
  • use permanent to 301 redirect;
  • use redirect to 302 redirect;

Upvotes: 8

Mansoor Kayani
Mansoor Kayani

Reputation: 21

You need to define new location and use alias instead of root or else the behaviour would be funky. Also you need to define location for .php to use $request_filename.

location /backend {
    alias /html/backend;
    try_files $uri $uri/ /index.php$is_args$args;

    location ~ \.php$ {
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

Upvotes: 0

Unicorn
Unicorn

Reputation: 1417

If I understood the question correctly you can use alias to change just the OS search path for a specific location:

Defines a replacement for the specified location. For example, with the following configuration on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

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

Upvotes: 0

Randy Lam
Randy Lam

Reputation: 689

I'll take a wild guess here:

location /backend/ {
    root /html/backend;
    try_files $uri $uri/ /index.php?_url=$uri&$args;
}

This means: all requests to .../backend/* will be redirected to the location block of php followed after:

location ~ \.php${ ... }

and php will handle those requests as backend scripts

Upvotes: 1

pevinkinel
pevinkinel

Reputation: 411

Nginx Beginner's Guide has this example:

server {
  location / {
    root /data/www;
  }

  location /images/ {
    root /data;
  }
}

So in theory this should work for you:

server {
  listen       80;
  server_name  localhost;

  location / {
    root html/laravel/public;
  }

  location /backend/ {
    root html/backend;
  }

  # common config goes here

}

Upvotes: 0

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42899

adding 127.0.0.1 to server_name to be able to use the link you provided in the comment 127.0.0.1

server_name localhost 127.0.0.1;

also you still need to have the backend location with root inside it.

location /backend/ {
    root /html/backend;
}

Upvotes: 1

Related Questions