【问题标题】:Problem creating virtual directory in Nginx在 Nginx 中创建虚拟目录的问题
【发布时间】:2021-01-15 18:19:16
【问题描述】:

我有一个 Yii2 项目,它在 apache 上运行良好,设置如下:

DocumentRoot "/var/www/html/test/backend/web/"
    ServerName test.local

    <Directory "/var/www/html/test/backend/web/">
            RewriteEngine on
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . index.php
            DirectoryIndex index.php
            Require all granted
    </Directory>

    Alias /api /var/www/html/test/api
    <Directory /var/www/html/test/api>
            RewriteEngine on
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . index.php
            DirectoryIndex index.php
            Require all granted
    </Directory>

现在我需要迁移到 Nginx。我已经能够弄清楚主网站的配置。 但是对于 '/api' 虚拟目录,如果在 URL 中的 '/api/' 之后添加任何内容,请求将被重定向到主 index.php 而不是 api 目录中的那个。 我当前的 Nginx 设置是:

server {
    listen 80;
    root /var/www/test/backend/web/;
    index index.php;
    server_name test.local;

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

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\. {
            deny all;
    }

    location /api {
            root /var/www/test;

            index index.php;
            try_files $uri $uri/ /index.php$is_args$args;

            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            }

            location ~ /\. {
                    deny all;
            }
    }
}

我也试过了~^/api 但还是不行。

【问题讨论】:

  • 你需要使用location ^~ /api { ... }
  • 与位置相同 ^~ /api { ... } 例如test.local/api/v1/users/create 与此位置不匹配。
  • 您的try_files 语句应在最后一个参数中指定/api/index.php

标签: apache nginx yii2


【解决方案1】:

问题在于server{} 中的root 指令。 工作配置是:

server {
    listen 80;
    index index.php;
    server_name test.local;

    location / {
            try_files $uri $uri/ /index.php$is_args$args;
            root /var/www/test/backend/web/;

             location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            }

            location ~ /\. {
                    deny all;
            }

    }

    location /api {
            root /var/www/test;
            index index.php;

            try_files $uri $uri/ /test/index.php$is_args$args;

            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            }

            location ~ /\. {
                    deny all;
            }
    }
}

也如Richard Smith 提到的那样

您的 try_files 语句应在最后一个参数中指定 /api/index.php。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    相关资源
    最近更新 更多