没有尾部斜杠的 nginx 配置
nginx configuration with no trailing slash
我正在尝试配置我的 nginx。我希望它提供一个 php 文件:dir/index.php
,但我可以通过 localhost/dir
请求(没有尾部斜线)访问它而不调用 nginx 301 redirect
。我尝试了此处找到的所有解决方案,但都失败了。你能解释一下我怎样才能达到这个目标吗?这是我的配置:
server {
access_log /var/log/nginx/access_log combined;
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/mock-api-server.git;
index index.php;
location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
您可以使用 try_files
将 /index.php
附加到 URI 的末尾。这必须是 try_files
语句的最后一个参数,因为新的 URI 是在不同的位置处理的。有关详细信息,请参阅 this document。
例如:
location / {
try_files $uri $uri/index.php?$args;
}
location ~* \.php$ {
try_files $uri =404;
...
}
第二个try_files
语句是为了避免passing uncontrolled requests to PHP。如果新的 URI 不对应于真实的 PHP 文件,则返回 404 响应。
我正在尝试配置我的 nginx。我希望它提供一个 php 文件:dir/index.php
,但我可以通过 localhost/dir
请求(没有尾部斜线)访问它而不调用 nginx 301 redirect
。我尝试了此处找到的所有解决方案,但都失败了。你能解释一下我怎样才能达到这个目标吗?这是我的配置:
server {
access_log /var/log/nginx/access_log combined;
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/mock-api-server.git;
index index.php;
location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
您可以使用 try_files
将 /index.php
附加到 URI 的末尾。这必须是 try_files
语句的最后一个参数,因为新的 URI 是在不同的位置处理的。有关详细信息,请参阅 this document。
例如:
location / {
try_files $uri $uri/index.php?$args;
}
location ~* \.php$ {
try_files $uri =404;
...
}
第二个try_files
语句是为了避免passing uncontrolled requests to PHP。如果新的 URI 不对应于真实的 PHP 文件,则返回 404 响应。