如何在一个域中运行 Wordpress-CMS 和 Angular 9 发行版?

如何在一个域中运行 Wordpress-CMS 和 Angular 9 发行版?

我正在尝试将我的 Angular 通用应用程序和 CMS 安装在一个域中。因此,我想将 CMS(即 Wordpress)放在子目录中。我当前的目录结构如下所示:

/httpdocs
|---dist        ...contains the Angular app
|---rest        ...contains backend REST-API files written in PHP
|---cms         ...contains the CMS Wordpress installation

我的ngxinx配置是:

location ~ /rest {
    try_files $uri $uri/ /rest/index.php$is_args$args;
}

location ^~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location ~ / {
    proxy_pass http://127.0.0.1:4000;
    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;
}

和我的.htaccess

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} ^/rest/
RewriteRule ^(.*)$ /rest/index.php#  [QSA,L,NC]

RewriteBase /cms/

RewriteRule ^wp-(.*)$ /cms/wp-$1 [QSA,L,NC]
RewriteRule ^packages/(.*)$ /cms/packages/$1 [QSA,L,NC]
RewriteRule ^\?page_id=(.*)$ /cms/index.php?page_id=$1 [QSA,L,NC]
RewriteRule ^news/(.*)$ /cms/news/(.*)$1 [QSA,L,NC]

RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /cms/index.php [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

我想要实现的目标是:

https://MYURL.com/                  -> opens my Angular app
https://MYURL.com/rest/xxx          -> calls the REST-API of my Angular app
https://MYURL.com/wp-admin          -> opens my CMS Admin
https://MYURL.com/wp-json/wp/v2/... -> calls the CMS REST-API

但无论我尝试什么,不管是运行 Angular 应用程序还是 CMS,都无法同时运行。有人看到我在哪里出错了吗?是否也有可能在 ngnix 中完成所有操作。不幸的是,我对此并不熟悉。

答案1

您的要求很不寻常。您可以尝试这个:

server {
    ...
    root /path/to/httpdocs;

    # serve any request starting with `/rest` with the following location
    location ^~ /rest {
        try_files $uri $uri/ /rest/index.php$is_args$args;
        location ~ \.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }

    # assuming every request starting with '/wp-' and every other request for PHP file
    # (except those starting with '/rest') are WordPress related
    location ~ (^/wp-|\.php$) {
        root /path/to/httpdocs/cms;
        try_files $uri $uri/ /index.php$is_args$args;
        location ~ \.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }

    # serve all the other requests with the Angular app
    location / {
        proxy_pass http://127.0.0.1:4000;
        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;
    }
}

根据 Ivan 的代码编辑版本:

# serve any request starting with `/rest` with the following location
location ~ /rest {
    try_files $uri $uri/ /rest/index.php$is_args$args;
    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/var/run/php7-fpm.sock;
    }
}

# assuming every request starting with '/wp-' and every other request for PHP file
# (except those starting with '/rest') are WordPress related
#location ~ (^/wp-|\.php$) {
location ~ /wp- {
    root /var/www/vhosts/speedskatingnews.info/ngx;
    index index.php index.html index.htm;
    try_files $uri $uri/ /cms/index.php?q=$uri&$args;

    location ~ \.php {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/var/run/php7-fpm.sock;
    }
}

# serve all the other requests with the Angular app
location ~ / {
    proxy_pass http://127.0.0.1:4000;
    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;
}

我需要做一些修改才能让它工作。现在只有一个问题:无法调用/wp-admin。需要调用/cms/wp-admin。这本身不是什么大问题,但例如/wp-json/wp/v2/pages/5346?_fields=title,link,content,slug/正在工作。但为什么?如果/wp-admin不起作用,/wp-json也应该不起作用……所以我不确定我编辑的代码是否真的适用。

相关内容