【问题标题】:nginx change root folder for specific urlnginx更改特定url的根文件夹
【发布时间】:2013-06-24 14:15:58
【问题描述】:

我有一个像下面这样的配置文件:

服务器 { 听 80; server_name 本地主机; #charset utf-8; 根 html/laravel/public; 索引 index.html 索引.php; #如果没有索引文件则浏览文件夹 自动索引开启; # 强制不 www if ($host ~* ^www\.(.*)) { 设置 $host_without_www $1; 重写 ^/(.*)$ $scheme://$host_without_www/$1 永久; } # 直接提供静态文件 位置 ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log 关闭; #expires max; } # 删除尾部斜杠(防止 SEO 重复内容问题) if (!-d $request_filename) { 重写 ^/(.+)/$ /$1 永久; } # 规范化 codeigniter url 端点 # 如果您的默认控制器不是“欢迎”,您应该更改以下内容 # if ($request_uri ~* ^(/lobby(/index)?|/index(.php)?)/?$) # { # 重写 ^(.*)$ / 永久; # } # 从所有控制器中删除尾随“索引” if ($request_uri ~* index/?$) { 重写 ^/(.*)/index/?$ /$1 永久; } # 除非请求是针对有效文件(图像、js、css 等),否则发送到引导程序 if (!-e $request_filename) { 最后重写 ^/(.*)$ /index.php?/$1; 休息; } # 将服务器错误页面重定向到静态页面 /50x.html # error_page 500 502 503 504 /50x.html; 位置 = /50x.html { 根html; } 位置/后端/ { 根/html/前端; } 位置 ~ \.php$ { 包括 fastcgi.conf; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; 包括 fastcgi_params; } 位置 ~ /\.ht { 全部否认; } # 全部捕获 #error_page 404 /index.php; # 位置 ~ \.php$ { # try_files $uri =404; # fastcgi_pass unix:/tmp/php.socket; # fastcgi_index index.php; # #include fastcgi_params; # 包括 /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/ 的任何网址,我必须将根文件夹更改为html/backend。加载页面的所有规则必须相同,只有根文件夹必须更改。

我该怎么做?

【问题讨论】:

    标签: nginx path root


    【解决方案1】:
    server {
      location / {
        root /data/www;
      }
    
      location /images/ {
        root /data;
        rewrite ^/images/(.+?)$ $1 break; #following is the explation
      }
    }
    
    • 使用 break 继续; root in location 将生效
    • 使用last内部模拟请求; location 中的root 不会生效
    • 使用永久进行301重定向;
    • 使用重定向到302重定向;

    【讨论】:

      【解决方案2】:

      将 127.0.0.1 添加到 server_name 以便能够使用您在评论 127.0.0.1 中提供的链接

      server_name localhost 127.0.0.1;
      

      您还需要在backend 位置内包含root

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

      【讨论】:

      • 不,不是那样的。这只是一个测试。它不起作用。当我打开 127.0.0.1/backend 时,仍然打开 root /html/laravel/public。这个块不会改变任何东西。
      • 它是否适用于 localhost/backend ? ,如果确实需要将 127.0.0.1 添加到 server_name,我编辑了我的答案。
      【解决方案3】:

      我在这里大胆猜测一下:

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

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

      location ~ \.php${ ... }
      

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

      【讨论】:

        【解决方案4】:

        Nginx Beginner's Guide 有这个例子:

        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
        
        }
        

        【讨论】:

          【解决方案5】:

          如果我正确理解了这个问题,您可以使用alias 仅更改特定位置的操作系统搜索路径:

          定义指定位置的替换。例如,根据“/i/top.gif”的请求进行以下配置,将发送文件/data/w3/images/top.gif。

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

          【讨论】:

            【解决方案6】:

            您需要定义新位置并使用别名而不是 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;
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2018-02-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-04-04
              • 1970-01-01
              相关资源
              最近更新 更多