Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx change root folder for specific url

Tags:

path

nginx

root

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?

like image 668
kabra Avatar asked Jun 27 '13 07:06

kabra


People also ask

How do I change my Nginx URL?

NGINX Return directive The easiest and cleaner way to rewrite an URL can be done by using the return directive. The return directive must be declared in the server or location context by specifying the URL to be redirected.

What is Try_files in Nginx?

The try_files directive can be used to check whether the specified file or directory exists; NGINX makes an internal redirect if it does, or returns a specified status code if it doesn't.

What is Sendfile Nginx?

The nginx HTTP server has a directive named sendfile , which can tell it to use the Linux sendfile() system call to do I/O without copying to an intermediate memory buffer. That should increase the I/O rate and reduce memory use.


1 Answers

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 will not take effect
  • use permanent to 301 redirect;
  • use redirect to 302 redirect;
like image 178
xsilen T Avatar answered Sep 23 '22 12:09

xsilen T