【发布时间】: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。