Nginx Try_files 重定向丢失查询字符串

Nginx Try_files 重定向丢失查询字符串

我正在尝试设置一个特定的重定向来强制重定向到特定子目录的 index.php(对于 Laravel),绕过现有的 index.html。

我想要捕获的 URL 看起来像这个 app/kit//?email= 。每个目录中都有一个 index.html 文件(由于某些业务原因,很难更改它)

重定向似乎有效,但是当我在 index.php 中解析 $_SERVER 时,我丢失了查询字符串。

我的 Nginx 配置如下:

server {
      server_name somedomain.com;
      root /home/www/preprod/current/public/;


        rewrite_log on;
        access_log /var/log/nginx/preprod-access.log;
        error_log /var/log/nginx/preprod-error.log notice;

        location ~* /kit/(.*)/index\.html {
              error_log /var/log/nginx/preprod-kit-error.log debug;
              try_files /index.php?$query_string /dev/null;
        }

      location / {
          index index.php index.html;
          try_files $uri $uri/ /index.php?$query_string;
          client_max_body_size 0;
          autoindex off;
          allow all;
      }

      location ~ \.php$ {
        error_log /var/log/nginx/preprod-php-error.log debug;
        #try_files $uri =404;
        #fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_intercept_errors off;
        fastcgi_connect_timeout 300s;
        fastcgi_read_timeout 15m;
        fastcgi_send_timeout 600s;
        fastcgi_keep_conn on;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        client_max_body_size 0;
        include snippets/fastcgi-php.conf;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
}

除这个应用程序外,所有应用程序都运行正常。

编辑1:snippets/fastcgi-php.conf

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

#fastcgi_index index.php;
include fastcgi.conf;

答案1

我找到了一个解决方案,我用的是这个:

 location ~* /kit/(.*)/(.*) {
        error_log /var/log/nginx/preprod.adsconsole.com-kit-error.log debug;
        rewrite /kit/(.*)/(.*) /index.php$is_args$args;
}

正则表达式显然可以写得更好,但如果它可以帮助某人!

相关内容