【问题标题】:Nginx - location with root in other directory and PHPNginx - 在其他目录和 PHP 中的根位置
【发布时间】:2017-03-02 00:49:30
【问题描述】:

我尝试使用 Nginx 设置类似于“Apache Alias”的位置,但我无法处理此文件夹中的 PHP 脚本。

这是我的文件夹结构(用于开发环境):

/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)

我尝试了不同的解决方案,但都没有奏效。

这是我最好的 Nginx 配置:

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

有了这个,我的cut/public/ 文件夹的所有内容都被重定向到dev/public/index.php 并被解释(我猜是try_file 的原因)。

这就是为什么欢迎您的帮助。

最终解决方案

在@richard-smith 的回答之后,这是实施的解决方案:

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

【问题讨论】:

    标签: php nginx nginx-location


    【解决方案1】:

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

    使用嵌套的location 块来隔离/cut 子目录,并在顶级使用^~ 修饰符以避免其他顶级正则表达式location 块干扰(参见this documentation)。

    alias 指令(参见this documentation)用于将/cut 映射到/var/www/cut/publicroot 指令只能连接,这会使/var/www/cut/public/cut(你不想要)。

    但是,我不建议将alias 指令与try_files 指令一起使用,因为this long term issue

    因此,解决方案是将/cut 静默重写为/cut/public 并使用root /var/www 的值。

    例如:

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

    【讨论】:

    • 嗯,我试试这个,但是cut/public 中的非 php 文件呢? try_file 会起作用吗(因为根现在是 /var/www 而不是 /var/www/cut/public)?
    • 是的。静态文件的 URI 是 /cut/assets/app.js,它被第一个块静默更改为 /cut/public/assets/app.js,然后通过 roottry_files 指令解析为 /var/www/cut/public/assets/app.js
    • 是的,工作。但是 /cut 有问题:这个调用进行重定向,我得到 /cut/public 作为 URI。也许我需要在调用 PHP 后端之前删除它?
    • 啊啊找到了!我在^~ /cut 中添加/?rewrite ^/cut/?(.*)$ /cut/public/$1 last; 并且它有效......然后回答接受,@Richard Smith
    • 你可能对其他裸目录有问题,nginx/cut/public 中添加了一个斜杠。但我正要建议为/cut 添加/?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多