Nginx / FastCGI - 调用子目录中的 PHP 文件

Nginx / FastCGI - 调用子目录中的 PHP 文件

我们在调用域子目录中的 php 文件时遇到问题。

运行正常且没有问题的操作:

example.com/mod/eco/file.php

但是当我们的域名与虚拟子目录放在一起时,它就不能像这样工作了:

example.com/b2b-dk/mod/eco/file.php

这是 nginx 配置:

server {
    listen 80;
    listen [::]:80;
    server_name .example.com;
    root /home/forge/example.com;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
      index /index.php;

      rewrite ^/admin462twqbah/?$ /admin462twqbah/index.php permanent;
      rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
      rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
      rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
      rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
      rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
      rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
      rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
      rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
      rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
      rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
      rewrite ^/c/([a-zA-Z-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1.jpg last;
      rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
      try_files $uri $uri/ /index.php?$args;
    }

    location /b2b-dk/ {
      rewrite ^/b2b-dk/(.*)$ /$1 last;
      try_files $uri $uri/ /index.php?$args;
    }

    location /b2b-eu/ {
      rewrite ^/b2b-eu/(.*)$ /$1 last;
      try_files $uri $uri/ /index.php?$args;
    }

    location /b2b-us/ {
      rewrite ^/b2b-us/(.*)$ /$1 last;
      try_files $uri $uri/ /index.php?$args;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/example.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /b2b-dk/.+\.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        include fastcgi_params;
    }

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

所以我尝试让这部分工作:

location ~ /b2b-dk/.+\.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
            include fastcgi_params;
        }

但没有任何运气——我做错了什么?

答案1

通过更改论坛 PHP 文件的块可以解决这个问题location,如下所示:

location ~ /b2b-dk/.+\.php$ {
    rewrite ^/b2b-dk/(.*)$ /$1 last;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
    include fastcgi_params;
}

相关内容