根据请求 url 更改根目录

时间:2021-04-07 13:41:37

标签: php nginx

我们有一个网站,我们使用 Laravel 来处理最终用户可用的部分,并为管理面板使用旧的遗留代码库。

当前 root 是当前部署版本的 public 文件夹。

网站文件夹结构如下所示:

admin/
  |____index.php <-- desired entrypoint for admin-related requests
app/

public/
  |____index.php <-- main entrypoint for website

resources/

routes/

因此,当有人想要访问管理面板时,他们会转到 example.com/admin

这是我们当前使用的 nginx 配置文件。

example.com

server {
    server_name example.com;
    root /var/www/example.com/current/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location /admin {
        proxy_pass http://localhost:3000;
    }

    error_page 404 /index.php;
    
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

example.com/admin

server {
    listen 3000;
    server_name example.com;
    root /var/www/example.com/current/admin;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

我不是很精通 ng​​inx 服务器配置,希望在这方面得到一些帮助。

编辑 1

使用 map 的配置文件:

编辑 2

更新了下面的配置以使其工作。

map $uri $siteroot {
    # This didn't work, per the accepted answer.
    # ^/admin  /var/www/example.com/current/admin;

    # This works great!
    ^/admin   /var/www/example.com/current;
    default   /var/www/example.com/current/public;
}

server {
    server_name example.com;
    root $siteroot;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name example.com;
    return 404; # managed by Certbot
}

2 个答案:

答案 0 :(得分:1)

如果您尝试更改根目录,Nginx 将显示未找到错误。

因为,即使您更改了 root,Nginx 也会尝试使用请求路径查找文件。

例如,如果将 /admin 路径的根目录更改为 /app/admin,Nginx 将在 /app/admin/admin 中找到文件。这就是您无法访问文件的原因。

尝试在 rewrite /admin(.*) $1 break; 位置块插入 /admin 行并设置根路径,或使用 alias 表达式。

答案 1 :(得分:0)

我会为此使用 map 指令,例如

map $uri $siteroot {
    ~^/admin  /var/www/example.com/current/admin;
    default   /var/www/example.com/current/public;
}

server {
    server_name example.com;
    root $siteroot;
    ...
}