php - Nginx - 在其他目录和 PHP 中具有 root 的位置

标签 php nginx nginx-location

我尝试使用 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 应用程序,您要么需要一个共同的文档根目录,要么需要两个 location ~*\.php (或类似的) block 来确保正确的 SCRIPT_FILENAME 被发送到 fastcgi 后端。

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

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

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

因此,一个解决方案是静默地将 /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;
    }
}

关于php - Nginx - 在其他目录和 PHP 中具有 root 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40147326/

相关文章:

ruby-on-rails - 使用 Nginx + Gzip + Unicorn 时缺少 Content-Length header

Nginx try_files 在传递变量路径时加载空白页

c++ - nginx rtmp compile for windows error U1052 & rtmp-module-master/config not found

php - 根据批准状态和特定订单项目更改 WooCommerce 订单状态

PHP & MySQL - 删除表行问题

php - Laravel + NGINX 给出 403 禁止

NGINX 健康检查需要上游

php - 不同条件下的计数

php - 如何知道电子邮件地址是否无效?

c# - Asp.net Core 获取客户端的远程 IP 总是返回 127.0.0.1