Nginx重写不适用于Wordpress

时间:2019-06-12 20:25:01

标签: nginx

我的wordpress主题具有以下功能。我的网站上有+ 35k注册用户,并且在每次新注册后刷新刷新规则时,我的CPU都增加到%100。因此,我禁用了主题重写功能(在add_filter之前添加//),并希望通过nginx重写来实现。

WordPress功能:

add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) {
    global $wpdb;
    $author_rewrite = array();
    $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");
    foreach($authors as $author) {
        $author_rewrite["u/({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
        $author_rewrite["u/({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
        $author_rewrite["u/({$author->nicename})/followers/?$"] = 'index.php?author_name=$matches[1]&type=followers';
        $author_rewrite["u/({$author->nicename})/comments/?$"] = 'index.php?author_name=$matches[1]&type=comments';
        $author_rewrite["u/({$author->nicename})/watchs/?$"] = 'index.php?author_name=$matches[1]&type=watchs';
        $author_rewrite["u/({$author->nicename})/watch/?$"] = 'index.php?author_name=$matches[1]&type=watch';
        $author_rewrite["u/({$author->nicename})/series/?$"] = 'index.php?author_name=$matches[1]&type=series';
        $author_rewrite["u/({$author->nicename})/notifications/?$"] = 'index.php?author_name=$matches[1]&type=notifications';
        $author_rewrite["u/({$author->nicename})/settings/?$"] = 'index.php?author_name=$matches[1]&type=settings';
        $author_rewrite["u/({$author->nicename})/lp/?$"] = 'index.php?author_name=$matches[1]&type=lost_password';
    }
    return $author_rewrite;
}

所以我将以下重写规则添加到了我的nginx conf中:

  rewrite ^/u/(.*)/$ http://sitename.com/index.php?author_name=$1;
  rewrite ^/u/(.*)/page/?(.*)$ http://sitename.com/index.php?author_name=$1&paged=$2;
  rewrite ^/u/(.*)/followers$ http://sitename.com/index.php?author_name=$1&type=followers;
  rewrite ^/u/(.*)/comments$ http://sitename.com/index.php?author_name=$1&type=comments;
  rewrite ^/u/(.*)/watchs$ http://sitename.com/index.php?author_name=$1&type=watchs;
  rewrite ^/u/(.*)/watch$ http://sitename.com/index.php?author_name=$1&type=watch;
  rewrite ^/u/(.*)/series$ http://sitename.com/index.php?author_name=$1&type=series;
  rewrite ^/u/(.*)/notifications$ http://sitename.com/index.php?author_name=$1&type=notifications;
  rewrite ^/u/(.*)/settings$ http://sitename.com/index.php?author_name=$1&type=settings;
  rewrite ^/u/(.*)/lp$ http://sitename.com/index.php?author_name=$1&type=lost_password;

当我访问此链接时:

http://sitename.com/u/admin/

地址栏就是这样:

http://sitename.com/?author_name=admin

因此,我不想更改地址栏。我将访问http://sitename.com/u/admin/,它将进行http://sitename.com/index.php?author_name=admin

我的nginx conf中有以下代码:try_files $ uri $ uri / /index.php?$query_string;

我也尝试遵循以下代码。

1)不变。地址栏看起来仍然是http://sitename.com/?author_name=admin

代码:

location /u {
  rewrite ^/u/(.*)/$ http://sitename.com/index.php?author_name=$1;
  rewrite ^/u/(.*)/page/?(.*)$ http://sitename.com/index.php?author_name=$1&paged=$2;
  rewrite ^/u/(.*)/followers$ http://sitename.com/index.php?author_name=$1&type=followers;
  rewrite ^/u/(.*)/comments$ http://sitename.com/index.php?author_name=$1&type=comments;
  rewrite ^/u/(.*)/watchs$ http://sitename.com/index.php?author_name=$1&type=watchs;
  rewrite ^/u/(.*)/watch$ http://sitename.com/index.php?author_name=$1&type=watch;
  rewrite ^/u/(.*)/series$ http://sitename.com/index.php?author_name=$1&type=series;
  rewrite ^/u/(.*)/notifications$ http://sitename.com/index.php?author_name=$1&type=notifications;
  rewrite ^/u/(.*)/settings$ http://sitename.com/index.php?author_name=$1&type=settings;
  rewrite ^/u/(.*)/lp$ http://sitename.com/index.php?author_name=$1&type=lost_password;
}

2)它将地址从http://sitename.com/u/admin/转换为http://sitename.com/?author_name=admin/,并将http://sitename.com/u/admin/watchs转换为http://sitename.com/?author_name=admin/watchs

代码

location /u {
  rewrite ^/u/(.*)/?$ http://sitename.com/index.php?author_name=$1;
  rewrite ^/u/(.*)/page/(.*)/?$ http://sitename.com/index.php?author_name=$1&paged=$2;
  rewrite ^/u/(.*)/followers/?$ http://sitename.com/index.php?author_name=$1&type=followers;
  rewrite ^/u/(.*)/comments/?$ http://sitename.com/index.php?author_name=$1&type=comments;
  rewrite ^/u/(.*)/watchs/?$ http://sitename.com/index.php?author_name=$1&type=watchs;
  rewrite ^/u/(.*)/watch/?$ http://sitename.com/index.php?author_name=$1&type=watch;
  rewrite ^/u/(.*)/series/?$ http://sitename.com/index.php?author_name=$1&type=series;
  rewrite ^/u/(.*)/notifications/?$ http://sitename.com/index.php?author_name=$1&type=notifications;
  rewrite ^/u/(.*)/settings/?$ http://sitename.com/index.php?author_name=$1&type=settings;
  rewrite ^/u/(.*)/lp/?$ http://sitename.com/index.php?author_name=$1&type=lost_password;
}

3)结果与第一个代码相同

代码:

location /u {
  rewrite ^/u/([\w-]+)/?$ http://sitename.com/index.php?author_name=$1;
  rewrite ^/u/([\w-]+)/page/([\w-]+)/?$ http://sitename.com/index.php?author_name=$1&paged=$2;
  rewrite ^/u/([\w-]+)/followers/?$ http://sitename.com/index.php?author_name=$1&type=followers;
  rewrite ^/u/([\w-]+)/comments/?$ http://sitename.com/index.php?author_name=$1&type=comments;
  rewrite ^/u/([\w-]+)/watchs/?$ http://sitename.com/index.php?author_name=$1&type=watchs;
  rewrite ^/u/([\w-]+)/watch/?$ http://sitename.com/index.php?author_name=$1&type=watch;
  rewrite ^/u/([\w-]+)/series/?$ http://sitename.com/index.php?author_name=$1&type=series;
  rewrite ^/u/([\w-]+)/notifications/?$ http://sitename.com/index.php?author_name=$1&type=notifications;
  rewrite ^/u/([\w-]+)/settings/?$ http://sitename.com/index.php?author_name=$1&type=settings;
  rewrite ^/u/([\w-]+)/lp/?$ http://sitename.com/index.php?author_name=$1&type=lost_password;
}

4)它没有更改地址栏,但显示404页面未找到错误。

代码

location /u {
  rewrite ^/u/([\w-]+)/?$ /index.php?author_name=$1;
  rewrite ^/u/([\w-]+)/page/([\w-]+)/?$ /index.php?author_name=$1&paged=$2;
  rewrite ^/u/([\w-]+)/followers/?$ /index.php?author_name=$1&type=followers;
  rewrite ^/u/([\w-]+)/comments/?$ /index.php?author_name=$1&type=comments;
  rewrite ^/u/([\w-]+)/watchs/?$ /index.php?author_name=$1&type=watchs;
  rewrite ^/u/([\w-]+)/watch/?$ /index.php?author_name=$1&type=watch;
  rewrite ^/u/([\w-]+)/series/?$ /index.php?author_name=$1&type=series;
  rewrite ^/u/([\w-]+)/notifications/?$ /index.php?author_name=$1&type=notifications;
  rewrite ^/u/([\w-]+)/settings/?$ /index.php?author_name=$1&type=settings;
  rewrite ^/u/([\w-]+)/lp/?$ /index.php?author_name=$1&type=lost_password;
}

5)它没有更改地址栏,但显示404页面未找到错误。

代码

location /u {
  rewrite ^/u/([\w-]+)/page/([0-9]+)/?$ /index.php?author_name=$1&paged=$2;
  rewrite ^/u/([\w-]+)/?$ /index.php?author_name=$1;
  rewrite ^/u/([\w-]+)/followers/?$ /index.php?author_name=$1&type=followers;
  rewrite ^/u/([\w-]+)/comments/?$ /index.php?author_name=$1&type=comments;
  rewrite ^/u/([\w-]+)/watchs/?$ /index.php?author_name=$1&type=watchs;
  rewrite ^/u/([\w-]+)/watch/?$ /index.php?author_name=$1&type=watch;
  rewrite ^/u/([\w-]+)/series/?$ /index.php?author_name=$1&type=series;
  rewrite ^/u/([\w-]+)/notifications/?$ /index.php?author_name=$1&type=notifications;
  rewrite ^/u/([\w-]+)/settings/?$ /index.php?author_name=$1&type=settings;
  rewrite ^/u/([\w-]+)/lp/?$ /index.php?author_name=$1&type=lost_password;
}

我的sitename.com Nginx Conf是:

server {
        server_name www.sitename.com;
        rewrite ^(.*) http://sitename.com$1 permanent;
        listen   8080;
        }
server {
        listen   8080;
        access_log off;
        # access_log   /home/sitename.com/logs/access_log;
        error_log on;
        # error_log /home/sitename.com/logs/error.log;  
        #add_header X-Frame-Options SAMEORIGIN;
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        root /home/sitename.com/public_html;
        include /etc/nginx/conf/ddos2.conf;
        # include /etc/nginx/conf/cors.conf;
        index index.php index.html index.htm;
        server_name sitename.com;
        try_files $uri $uri/ /index.php?$query_string;
        #modsecurity on;
        #modsecurity_rules_file /etc/nginx/modsec/main.conf;
#///////////////////////////////////////////////////////
# You can use only 1 rule (AAA or BBB or CCC or DDD or EEE)
# Please do not delete line AAA, BBB, CCC , DDD or EEE. Only Comment it if you do not use. 
# All of them need for [ Config Vhost For Plugin Cache ] function in [ Wordpress Blog Tools ]
#///////////////////////////////////////////////////////






location /u {
  rewrite ^/u/([\w-]+)/page/([\w-]+)/?$ /index.php?author_name=$1&paged=$2;
  rewrite ^/u/([\w-]+)/followers/?$ /index.php?author_name=$1&type=followers;
  rewrite ^/u/([\w-]+)/comments/?$ /index.php?author_name=$1&type=comments;
  rewrite ^/u/([\w-]+)/watchs/?$ /index.php?author_name=$1&type=watchs;
  rewrite ^/u/([\w-]+)/watch/?$ /index.php?author_name=$1&type=watch;
  rewrite ^/u/([\w-]+)/series/?$ /index.php?author_name=$1&type=series;
  rewrite ^/u/([\w-]+)/notifications/?$ /index.php?author_name=$1&type=notifications;
  rewrite ^/u/([\w-]+)/settings/?$ /index.php?author_name=$1&type=settings;
  rewrite ^/u/([\w-]+)/lp/?$ /index.php?author_name=$1&type=lost_password;
  rewrite ^/u/([\w-]+)/?$ /index.php?author_name=$1;
}










# If you use your rule, comment the line above , and  Uncoment 3 lines belows and set your rule within it.. (BBB)
#location / {
#Uncomment 3 lines and set your rules here!
#}

# Rule for wordpress + Plugin wp super cache. (CCC)
#include /etc/nginx/conf/supercache.conf;  

# Rule for wordpress + Plugin W3 Total Cache. (DDD)
#include /etc/nginx/conf/w3total.conf; 

# Rule for wordpress + Plugin WP-Rocket. (EEE)
#include /etc/nginx/conf/wprocket.conf; 


# Config Cache Static Files
include /etc/nginx/conf/staticfiles.conf;


#include /etc/nginx/conf/block.conf;
# Error Page
#error_page 403 /errorpage_html/403.html;
error_page 404 /404.html;
location = /404.html {
                root /usr/share/nginx/html;
                internal;
        }

#error_page 405 /errorpage_html/405.html;
#error_page 502 /errorpage_html/502.html;
#error_page 503 /errorpage_html/503.html;
#error_page 504 /errorpage_html/504.html;
#location ^~ /errorpage_html/ {
#   internal;
#    root /home/sitename.com;
#    access_log              off;
#}
        location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
        fastcgi_connect_timeout 250;
        fastcgi_send_timeout 250;
        fastcgi_read_timeout 250;
        fastcgi_buffer_size 256k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME /home/sitename.com/public_html$fastcgi_script_name;
        }

#include /etc/nginx/conf/phpstatus.conf;
include /etc/nginx/conf/drop.conf;

    }

0 个答案:

没有答案