NGINX 虚拟子目录正在提供 PHP 文件下载服务

NGINX 虚拟子目录正在提供 PHP 文件下载服务

我的服务器响应以下虚拟子目录路径:

www.domain.com/us
www.domain.com/ca
www.domain.com/fr-ca
www.domain.com/spa

其中每个都是 的别名www.domain.com

  • 如果我尝试访问www.domain.com/some/virtual/path,它会被正确地交给我index.php并由 PHP-FPM 处理。

  • 如果我尝试访问www.domain.com/us/some/virtual/path,它会被正确地交给我index.php并由 PHP-FPM 处理

  • 但是,如果我尝试调用www.domain.com/us/file.php,NGINX 会尝试将文件作为下载提供。但如果没有虚拟路径,PHP-FPM 会对其进行适当处理。

我的虚拟子目录路径由我的 NGINX 配置中的此部分管理:

    ####
    # Catch virtual locations: /us, /ca, /fr-ca, /spa
    ####
    location ~ ^\/(?:(?<currentSite>us|ca|fr-ca|spa)(?:\/|$))(?<realPath>.*) {
        try_files /$realPath /$realPath/;
        break;
    }

server {
    listen       443 ssl;
    listen       80;
    listen   [::]:80 default ipv6only=on;
    server_name  localhost;

    ssl_certificate      /certs/cert.pem;
    ssl_certificate_key  /certs/cert.key;

    root   ${LANDO_WEBROOT};
    index index.php index.html index.htm;

    ######
    # CloudFlare limit for headers is 8K - development should simulate this

    large_client_header_buffers 4 8k;

    ####
    # Catch virtual locations: /us, /ca, /fr-ca, /spa
    ####
    location ~ ^\/(?:(?<currentSite>us|ca|fr-ca|spa)(?:\/|$))(?<realPath>.*) {
        try_files /$realPath /$realPath/;
        break;
    }

    ####
    # Manage the REAL locations
    ####
    location /js {
        index index.html
        expires 100d;
        add_header Pragma public;
        add_header Cache-Control "public";
        try_files $uri $uri/ =404;
        break;
    }

    location /media {
        index index.html
        expires 100d;
        add_header Pragma public;
        add_header Cache-Control "public";
        try_files $uri $uri/ =404;
        break;
    }

    location /skin {
        index index.html
        expires 100d;
        add_header Pragma public;
        add_header Cache-Control "public";
        try_files $uri $uri/ =404;
        break;
    }

    location @handler {
        rewrite / /index.php;
    }

    location / {
        try_files $uri $uri/ @handler;
    }

    location ~ \.php$ {
        # Try to load the file if requested, if not found, rewrite to @missing
        # This should pass all requests through index.php

        try_files $uri =404;

        fastcgi_param MAGE_IS_DEVELOPER_MODE true;
        fastcgi_buffers 256 500k; #Allow a greater amount of data to be included in cookies
        fastcgi_buffer_size 1000k;  #Allow a greater amount of data to be included in cookies

        fastcgi_pass fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_connect_timeout 300s;
        fastcgi_send_timeout 300s;
        fastcgi_read_timeout 300s;
        include fastcgi_params;
    }
}

答案1

您可以使用rewrite...last从 URI 中删除语言前缀,以便配置中的其余位置可以正确处理它。特别是,.phpURI 需要由location ~ \.php$块处理。

例如:

location ~ ^/(?:(?<currentSite>us|ca|fr-ca|spa)(?:/|$))(?<realPath>.*) {
    rewrite ^ /$realPath last;
}

或者更简单地说:

rewrite ^/(us|ca|fr-ca|spa)(?:/(.*))? /$1 last;

这个文件了解详情。

相关内容