大网数据广告活动
服务器运维问答

Nginx Rewriting : PHP file inside of Location directive not served

· 52CE 我爱测

I am trying to redirect a subdirectory to a php file. Instead of serving the file, it downloads it, meaning that it does not read it as a php file. More exactly it does not enter the fastcgi location ~ \.php$ directive. Some inte…

问题原文

原帖链接 · Tristan · CC BY-SA 3.0

I am trying to redirect a subdirectory to a php file. Instead of serving the file, it downloads it, meaning that it does not read it as a php file.

More exactly it does not enter the fastcgi location ~ \.php$ directive.

Some interesting facts :

  • Everything works perfectly well for the admin bloc but not for the login block.
  • When I move the location ~ \.php$ directive above the login one and that I change ^~ /login into ~ /login, it works exept for the rewrite rule rewrite ^/login/(.+\.php)$ /$1 last;.

I did read the Wiki Nginx documentation on the topic so I am aware of the priorities in which nginx read the location directives but that does not tell me how to deal with this case! I also read

Here is the config:

server {
 listen  80;

 server_name tyba-one.com *.tyba-one.com;
 root /var/www/tyba/public_html;
 rewrite_log on;
 error_page 404 /404.php;
 client_max_body_size 4M;

 if ($host ~* ^www\.(.*)){
  set $host_without_www $1;
  rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
 }

 location = / {
   rewrite ^/$ /index.php last;
 }

 location ^~ /login {
  rewrite ^(/login)$ $1/ permanent;
  rewrite ^/login/$ /login.php last;
  rewrite ^/login/(.+\.php)$ /$1 last;
 }    

 location ^~ /admin {
  rewrite ^(/admin)$ $1/ permanent;
  rewrite ^/admin/$ /page_builder.php last;
  rewrite ^/admin/(.+\.php)$ /$1 last;
 }

 location ~ \.php$ {
  try_files $uri =404;
  include /opt/nginx/conf/fastcgi_params;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /var/www/tyba/public_html$fastcgi_script_name;
 }
}

I would like to understand what's going on and how to deal with such situations.

已采纳解答

原帖链接 · mgorven · CC BY-SA 3.0

nginx only processes one location block at each nesting level, and so /login.php is handled by the ^~ /login block and not the ~ \.php$ block. Since you only have rewrite statements in your location blocks, you actually don't need the location blocks at all (as long as the rewrite matches are specific enough, which yours are). Just move your rewrite statements into the root of the server block so that requests for /login and /admin still use the \.php$ location block:

rewrite ^/$ /index.php last;
rewrite ^(/login)$ $1/ permanent;
rewrite ^/login/$ /login.php last;
rewrite ^/login/(.+\.php)$ /$1 last;
rewrite ^(/admin)$ $1/ permanent;
rewrite ^/admin/$ /page_builder.php last;
rewrite ^/admin/(.+\.php)$ /$1 last;

location ~ \.php$ {
    ...
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-03-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。