nginx更改根文件夹为特定的url

yftpprvb  于 2023-03-01  发布在  Nginx
关注(0)|答案(6)|浏览(158)

我有一个配置文件,如下所示:

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

我必须改变根文件夹为$host/backend/的任何网址与$host/backend/。所有规则的加载页面必须是相同的,只有根文件夹必须改变。
我该怎么做呢?

z6psavjg

z6psavjg1#

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

  location /images/ {
    root /data;
    rewrite ^/images/(.+?)$ $1 break; #following is the explation
  }
}
  • 使用break继续;根位置将生效
  • 使用last进行内部模拟请求;位置中根可能不会生效
  • 使用永久到301重定向;
  • 使用重定向到302重定向;
zbq4xfa0

zbq4xfa02#

将www.example.com添加127.0.0.1到server_name,以便能够使用您在注解127.0.0.1中提供的链接

server_name localhost 127.0.0.1;

同样,您仍然需要在backend位置中包含root

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

x6492ojm3#

我来大胆猜测一下:

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

这意味着:所有到.../backend/* 的请求将被重定向到php的location块,然后:

location ~ \.php${ ... }

php将把这些请求作为后端脚本来处理

ztigrdn8

ztigrdn84#

Nginx初学者指南有这样的例子:

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

  location /images/ {
    root /data;
  }
}

所以理论上这对你来说是可行的:

server {
  listen       80;
  server_name  localhost;

  location / {
    root html/laravel/public;
  }

  location /backend/ {
    root html/backend;
  }

  # common config goes here

}
lxkprmvk

lxkprmvk5#

如果我没理解错的话,您可以使用alias只更改特定位置的操作系统搜索路径:
定义指定位置的替换位置。例如,在请求“/i/top.gif”时使用以下配置,将发送文件/data/w3/images/top. gif。

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

wlwcrazw6#

你需要定义一个新的位置,并使用别名代替root,否则行为会很奇怪。你也需要为.php定义一个位置来使用$request_filename。

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

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

相关问题