NGINX - WORDPRESS - PHP-FPM:查询参数(GET)未传递到子文件夹中

NGINX - WORDPRESS - PHP-FPM:查询参数(GET)未传递到子文件夹中

我有一个使用 NGINX(和 Php-fpm)运行的 wordpress 网站,有 4 个虚拟主机,一切正常,除非我想传递查询字符串。看,如果我这样做,它就可以正常工作:

example.com/phpfile.php?var=123

但如果我尝试

exaple.com/subfolder/subfolder2/phpfile.php?var=123

这是我的/nginx/snippets/wordpress.conf:

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

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
    try_files $uri /index.php?$args;
}

   
location ~ /\. {
    deny all;
}

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}
# End Restrictions

# Caching
set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
        set $no_cache 1;
}
if ($query_string != "") {
  set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
  set $skip_cache 1;
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
  set $skip_cache 1;
}
#end Caching

# WordPress single site rules.
location / {
    try_files $uri $uri/ /index.php?$args;
}
   
#location /wp-content/themes/kdi-ecommerce/templates/ {
#    try_files $uri $uri/ /index.php$is_args$query_string;
#}


# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(eot|otf|woff|woff2|ttf|rss|atom|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
    access_log off; log_not_found off; expires max;
}

# Media: images, icons, video, audio send expires headers.
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

# CSS and Javascript send expires headers.
location ~* \.(?:css|js)$ {
  expires 1y;
  access_log off;
  add_header Cache-Control "public";
}
# HTML send expires headers.
location ~* \.(html)$ {
  expires 7d;
  access_log off;
  add_header Cache-Control "public";
}

# Browser caching of static assets.
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
  expires 7d;
  add_header Cache-Control "public, no-transform";
}

# Enable Gzip compression in NGNIX.
gzip on;
gzip_disable "msie6";

gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;

# Fast-CGI Cache configuration
location ~ \.php$ {
#location ~ [^/]\.php(/|$){
    #fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    #if (!-f $document_root$fastcgi_script_name) {
    #    return 404;
    #}

    include fastcgi-cache.conf;
    include fastcgi.conf; 
    #fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_pass unix:run/php-fpm/www.sock;
    #enable cache
    add_header X-WP-Cache $upstream_cache_status;
    
    # Skip cache based on rules in snippets/fastcgi-cache.conf.
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;

    # Define memory zone for caching. Should match key_zone in fastcgi_cache_path above.
    fastcgi_cache restorebindemo;

    # Define caching time.
    fastcgi_cache_valid 60m;

    #PHP-FTP Buffers
    fastcgi_buffers 8 4k;
    fastcgi_buffer_size 8k;

    #increase timeouts
    fastcgi_read_timeout 60s;
    fastcgi_connect_timeout 60s;
    fastcgi_send_timeout 60s;
}

我已经尝试了所有类型的 try_files 变体;我不知道它是否是位置/块配置。

这是 fastcgi.conf:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

如上所述,如果我将需要使用参数执行的 php 文件放在 / 中(与 wp-config.php 文件位于同一位置),它可以起作用,但放在子目录中则不行。

答案1

你有:

  1. 要么重写到根目录的路径,例如使用自己的位置,/subfolder/subfolder2/要么使用重写指令:
rewrite ^/subfolder/subfolder2/([^/]+\.php)$ $1 last;
  1. 或者不使用fastcgi_split_path_info分割路径,只需尝试从位置模式提供捕获的脚本名称,例如添加其他位置或~ \.php$像这样重写您的位置:
- location ~ \.php$ {
+ location ~ ^/(?:subfolder/subfolder2/)?(?<script_name>[^/]+\.php)$ {
    ...
    fastcgi_param SCRIPT_NAME $script_name;
  }

相关内容