我想制作一个 WordPress Headless(在 /graphql uri 上使用 wpgraphql API),其中前端将是 next.js。我希望前端(Next.js)和后端(WordPress.admin、content 和 API)都在同一个域中。
为此,我希望所有的请求都只针对 WordPress/wp-admin/*
并且被定向到 WordPress /wp-content/*
。/graphql
所有其他 uri 将通过反向代理重定向到 localhost:3000 中的 next.js 服务器。下面是 nginx 配置文件。
server {
listen 80;
listen [::]:80;
root /var/www/wordpress;
index index.php index.html index.htm;
server_name localhost;
client_max_body_size 100M;
autoindex off;
# WordPress
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Next.js
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_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
}
}
任何帮助表示赞赏,谢谢
nginx 使用
location
指令指定的最长匹配前缀。因此,以下设置应该可以满足您的需求。您可以采取以下方法: