Nginx change root folder for specyfic url

I have a configuration file similar to this:

     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 need to change the root folder on html/backend for any URL with $host/backend/ . All rules for download pages should be the same, only the root folder should be changed.

How can i do this?

+6
source share
3 answers

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

 server_name localhost 127.0.0.1; 

also you need to have a root backend location inside it.

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

Nginx Beginner's Guide has the following 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 } 
0
source
 server { location / { root /data/www; } location /images/ { root /data; rewrite ^/images/(.+?)$ $1 break; #following is the explation } } 
  • use a break to continue; the root in the location will take effect
  • use the latter for internal simulation of the request; the root in the location will not take effect
  • use permanently 301 redirects;
  • use a redirect to a 302 redirect;
0
source

Source: https://habr.com/ru/post/948198/


All Articles