在 Nginx 中创建虚拟目录时出现问题
Problem creating virtual directory in Nginx
我有一个 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还是不行。
问题出在 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所述
Your try_files statement should specify /api/index.php in the last parameter.
我有一个 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还是不行。
问题出在 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所述
Your try_files statement should specify /api/index.php in the last parameter.