微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Wordpress 中的空 GET 参数使用 Nginx

如何解决Wordpress 中的空 GET 参数使用 Nginx

我有一个Nginx 上运行的 wordpress(使用 FastCGI、PHP-FPM),4 个虚拟主机(4 个站点)在同一台机器上运行。一切都像魅力一样工作,但是在执行 GET 参数时会发生一些奇怪的事情,例如,我运行: example.com/wp-content/themes/kdi-ecommerce/templates/mobile-player.PHP?source=https://d343552mf.cloudfront.net/content/d32324m93k.m3u8

我尝试了此处显示的不同解决方案 (StackOverflow) 和其他一些地方,但没有一个有效,我尝试以许多不同的方式和位置更改 wordpress.conf 文件添加/删除/编辑,例如: 我基本上尝试了“try_files”参数的所有变体,以下是我尝试过的几个变体,以及当前在我的代码中配置为“工作”的变体:

 location / {
       #try_files $uri $uri/ /index.PHP?$args;
       #try_files $uri $uri/ /index.PHP?q=$uri&$args;
       try_files $uri $uri/ /index.PHP?$query_string;
    
    }

我还尝试在 # Fast-CGI Cache 配置上更改缓冲区大小,如下面的代码所示,虽然它实际上提高了我网站的加载速度,但它没有解决 GET 参数不起作用的问题。

我什至在浏览器上的 mobile-player.PHP添加了一行代码,它应该打印 GET 参数,但它返回的所有内容都是 NULL,就像 GET 参数没有被发送到我的后端一样,即使您可以在 URL 上看到它(如上所示的 URL 示例)。

我的 wordpress.conf 文件如下所示:

    # Restrictions
# disable logging for favicon and robots.txt
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;
}

# Deny all attempts to access hidden files such as .htaccess,.htpasswd,.DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
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;
   #try_files $uri $uri/ /index.PHP?q=$uri&$args;
   try_files $uri $uri/ /index.PHP?$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$ {
    include fastcgi-cache.conf;
    include fastcgi.conf; 
    fastcgi_intercept_errors on;
    #fastcgi_pass unix:run/PHP-fpm/www.sock;
    fastcgi_pass 127.0.0.1:9000;
    # 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;
    #proxy_read_timeout 300;
    #proxy_connect_timeout 6000;
    #proxy_send_timeout 6000;
    #send_timeout 60;
}

此外,在 fastcgi.conf 文件中,有一行代码应该传递要处理的 POST 和 GET 参数,但它对我不起作用:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

问题不仅影响我的 GET 参数,还影响 POST 参数,作为记录,我在 apache 上运行完全相同的站点,所有这些都在那里工作正常,所以我认为它与错误的配置有关Nginx 和 FastCGI 通信文件之间。

非常感谢所有答案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。