gpt4 book ai didi

php - Nginx:将子域重写为其中的文件夹和文件

转载 作者:搜寻专家 更新时间:2023-10-31 20:57:37 25 4
gpt4 key购买 nike

我有 nginx 重写规则 - 它将所有子域请求从子重定向到文件夹:

server {
listen x.x.x.x:80;
server_name domain *.domain;

root /home/admin/web/domain/public_html/subs/$subdomain; # here is IF for subdomains
set $subdomain "";
if ($host ~* ^([a-z0-9-\.]+)\.domain$) {
set $subdomain $1;
}
if ($host ~* ^www.domain$) {
set $subdomain "";
}


index index.php;


location / { # here is rules for ROOT domain

root /home/admin/web/domain/public_html;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}

........

它运行良好,但我有一个问题。

在域和子域中,我有一个从 txt 文件获取数据的相同 php 脚本,如下所示:

file(key.txt);

我认为我的 php-fpm 模块不知道 nginx 规则并从 ROOT 域获取 SUB 中的数据——这是错误的。请帮助我添加 nginx 异常(exception)或添加规则以从 SUB 获取 SUB 中的 txt 数据。 Not_from_root_domain。谢谢。

最佳答案

您应该添加额外的位置来处理 php 文件。为了简化规则,我将根目录移动到 /tmp/subs/root 子目录。

server {
listen 80;
server_name domain.test *.domain.test;

set $subdomain "root";
if ($host ~* ^([a-z0-9-\.]+)\.domain.test$) {
set $subdomain $1;
}
if ($host ~* ^www.domain.test$) {
set $subdomain "root";
}

root /tmp/subs/$subdomain;

location / {
index index.php;

if (!-e $request_filename) {
rewrite ^(.*)$ /index.php last;
break;
}
}

location ~ /(.+\.php) {
index index.php;

fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi_params;
}
}

目录结构:

/tmp/subs/root/index.php
/tmp/subs/d1/index.php
/tmp/subs/d2/index.php
/tmp/subs/d2/key.txt

/tmp/subs/d2/index.php:

<?php
$file = file('key.txt');
print_r($file);

/tmp/subs/d2/key.txt

hello there

关于php - Nginx:将子域重写为其中的文件夹和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53837784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com