Nginx - 在其他目录和PHP中使用root的位置

I tried to set a location similar as "Apache Alias" with Nginx but I don't able to process PHP script in this folder.

Here is my folder structure (for Dev environment):

/var/www
+-  dev/public/ <-- This is my normal Web root : "/"
|   +- assets/
|   |  +- app.css
|   |  +- app.js
|   |
|   +-  index.php
|   +-  favicon.png
|    
+-  cut/public/ <-- This must like an "Apache Alias" : "/cut"
    +- assets/
    |  +- app.css
    |  +- app.js
    |
    +-  index.php
    +-  other_other_file.php (why not)

I've tried different solutions but none of them are working.

Here is my best Nginx configuration :

server {
    listen   80;

    server_name _;
    root  /var/www/dev/public/;
    index index.php index.html;
    autoindex on;

    # Logs
    rewrite_log on;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    # Serving files
    location / {
        try_files $uri $uri/ @php;
    }

    location /cut {
        root /var/www/cut/public;
        try_files $uri $uri/ @php;
    }

    # PHP
    location @php {
        rewrite ^(.*)/?$ /index.php$is_args$args last;
    }

    location ~* \.php(/|$) {
        fastcgi_pass  php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param DOCUMENT_ROOT $document_root;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

With this one, all the content of my cut/public/ folder is redirected to the dev/public/index.php and interpreted (cause of try_file, I presume).

That is why your help would be welcome.

Final Solution

After the answer of @richard-smith, here's the implemented solution :

server {
    listen   80;

    server_name _;
    root  /var/www/dev/public/;
    index index.php index.html;

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location ^~ /cut {
        rewrite ^/cut/?(.*)$ /cut/public/$1 last;
    }

    location ^~ /cut/public {
        root /var/www/;
        try_files $uri $uri/ /cut/index.php$is_args$args;

        location ~* \.php(/|$) {
            fastcgi_pass  php:9000;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param DOCUMENT_ROOT $document_root;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~* \.php(/|$) {
        fastcgi_pass  php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param DOCUMENT_ROOT $document_root;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

1个回答



并行运行两个PHP应用程序,您需要一个公共文档根目录,或者您需要两个 location~ * \ .php </ code>(或类似)块以确保将正确的 SCRIPT_FILENAME </ code>发送到fastcgi后端。</ p>

使用嵌套 location </ code> blocks隔离 / cut </ code>子目录,并使用顶层的 ^〜</ code>修饰符来避免其他顶级正则表达式 location </ code>块 干扰(参见本文档)。</ p>
\ n

alias </ code>指令(参见此文档< / a>)用于将 / cut </ code>映射到 / var / www / cut / public </ code>。 root </ code>指令只能连接,这将使 / var / www / cut / public / cut </ code>(你不想要)。</ p>

但是,我建议不要将 alias </ code>指令与 try_files </ code>指令一起使用,因为这个长期问题。</ p>

因此,一个解决方案是默默地将 / cut </ code>重写为< 代码> / cut / public </ code>并使用值 root /var/www.


nn

例如:</ p>

  location ^〜/ cut {
rewrite ^ / cut(。*)$ / cut / public $ 1 last;
}
location ^〜/ cut / public {
root / var / www;

try_files $ uri $ uri / /cut/index.php$is_args$args;
nn location~ * \ .php(/ | $){
fastcgi_pass php:9000;
fastcgi_split_path_info ^(。+ \ .php)(/。*)$;
包括fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
}
}
</ code> </ pre>
</ div>

展开原文

原文

Running two PHP applications side-by-side, you either need a common document root, or you need two location ~* \.php (or similar) blocks to ensure the correct SCRIPT_FILENAME is sent to the fastcgi backend.

Use nested location blocks to isolate the /cut subdirectory, and use the ^~ modifier at the top level to avoid other top level regular expression location blocks from interfering (see this documentation).

The alias directive (see this documentation) is used to map /cut to /var/www/cut/public. The root directive can only concatenate, which would make /var/www/cut/public/cut (which you do not want).

However, I would not recommend using the alias directive with the try_files directive because of this long term issue.

So, a solution would be to silently rewrite /cut to /cut/public and use a value of root /var/www.

For example:

location ^~ /cut {
    rewrite ^/cut(.*)$ /cut/public$1 last;
}
location ^~ /cut/public {
    root /var/www;
    try_files $uri $uri/ /cut/index.php$is_args$args;

    location ~* \.php(/|$) {
        fastcgi_pass  php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

duanmao1319
duanmao1319 我试图在/ cut / public下放一些子文件夹,它与索引,静态文件和php文件完美配合。
大约 4 年之前 回复
dqgo99177
dqgo99177 你可能有其他裸目录的问题,nginx从/ cut / public里面添加一个尾部斜杠。 但我正要建议添加/? for / cut。
大约 4 年之前 回复
dongyu7074
dongyu7074 啊找到了! 我加 /? 在^〜/ cut:重写^ / cut /?(。*)/ cut / public / $ 1 last; 它的确有效...然后接受答案,@理查德史密斯
大约 4 年之前 回复
douzhengzuo7283
douzhengzuo7283 是的,工作。 但是/ cut有问题:这个调用进行重定向,我得到/ cut / public作为URI。 也许我需要在调用PHP后端之前删除它?
大约 4 年之前 回复
dongsaohu6429
dongsaohu6429 是。 静态文件的URI是/cut/assets/app.js,由第一个块静默更改为/cut/public/assets/app.js,然后解析为/ var / www / cut / public / assets / app .js由root和try_files指令组成。
大约 4 年之前 回复
duanraotun1674
duanraotun1674 嗯,我是这样但是切割/公共中的非PHP文件怎么样? try_file是否有效(因为root现在是/ var / www而不是/ var / www / cut / public)?
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐