首页 > 解决方案 > 使用 Nginx 通过博客域仅提供 Wordpress API

问题描述

我想使用 WP API 将我的 WP 博客集成到非 WP 站点中。

我的博客目前位于,site.com/blog因此 API 托管在site.com/blog/wp-json

我的非 WP 网站位于site.com

当用户访问site.com/blogsite.com/blog/<slug>我想为非 WP 网站提供服务时,但如果我访问site.com/blog/wp-json/*site.com/blog/wp-admin(基本上任何带有 的/blog/wp-*)由 Wordpress 提供服务。

我正在使用 Nginx 并进行此设置sites-available

location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    rewrite ^/([^/]+)\/([^/]+)/[0-9]+.html$ /vinyl/$1-$2/ permanent;

    #add_header Cache-Control 'no-store';
}

location ~ /blog/.*\.php$ {
    root /var/www/websitename;
    index index.php index.html index.htm;
    set $php_root /var/www/websitename/blog;

    include /etc/nginx/fastcgi_params;
    try_files $uri =404;
    fastcgi_read_timeout 180;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

}


location /blog {
    root /var/www/websitename;
    index index.php index.html index.htm;
    set $php_root /var/www/websitename/blog;

    try_files $uri $uri/ /blog/index.php$is_args$args;
}

标签: wordpressnginx

解决方案


location ~ ^/blog/wp-.*$ {
 #go to wordpress
}

location ~ ^/blog {

 #go to non-wordpress
}

按照这个顺序,任何以 /blog/wp-ANYTHING 开头的东西都会进入 wordpress。并且如果没有被这个位置括号占用,如果它以/blog开头,它将转到非wordpress


推荐阅读