有点背景,我基本上希望一些网站通过wordpress和其他部分进行操作,通过角度7进行操作。
所以website.com
将转到wordpress主页,website.com/signin
将转到角度网站。这可能吗?我一直在玩nginx设置,这是我到目前为止所拥有的。不幸的是,它迫使一切都通过wordpress网站,所以当我点击/signin
,我得到一个生成404的wordpress。
路由比这更复杂,理想的情况是将某些路径开始白名单并将其路由到角度,同时将其他任何路由到wordpress(对于相对通用的404)。例如,
website.com/signin --
website.com/users/:id |
website.com/signout |- all of these route to angular
website.com/otherModels/:id |
website.com/another --
website.com/everything-else-goes-to-wordpress
我可以使用wordpress或angular来工作,但它们似乎正在使用竞争索引文件。我的角度使用index.html和wordpress使用index.php(而如果我没有弄错的话,劫持其目录中的所有其他调用)。这是我用于wordpress的nginx文件:
server {
root /var/www/website.com;
index index index.html index.php index.htm index.nginx-debian.html;
server_name website.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_buffers 8 16k; # increase the buffer size for PHP-FTP
fastcgi_buffer_size 32k; # increase the buffer size for PHP-FTP
fastcgi_connect_timeout 60;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
角度:
server {
root /path/to/angular-project/dist;
index index index.html index.php index.htm index.nginx-debian.html;
server_name website.com;
location / {
proxy_pass http://localhost:4200;
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;
try_files $uri $uri/ $uri$args /index.html;
}
}
我显然可以将位置添加到其他文件中,但是当我这样做时,无论在location / {}
中设置哪个都有效,而另一个则没有。例如,如果我将wordpress文件更改为包含:
location = /signin {
root /path/to/angular-project/dist; // I've tried using alias as well
proxy_pass http://localhost:4200;
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;
try_files $uri $uri/ $uri$args /index.html;
}
什么都没发生。我仍然得到我的wordpress 404(而不是角度网站)。我可以将上面的代码改为location = /signin { return 404; }
,我得到一个nginx 404,所以我知道位置工作正常。事实上,我得到一个nginx 404错误,直到我将/index.html
添加到try_files
行的末尾。这让我相信try_files /index.html
在某种程度上没有被重定向到(被劫持)wordpress。这反过来也发生了。在angular / wordpress之间,在location / {}
中设置的最小值确定404应该由另一个定义的位置。
有没有办法阻止wordpress / angular劫持所有“索引”文件?
我正在躲避Richard Smith的评论。基本上发生了什么(据我所知),Wordpress正在使用我在index
中定义的所有东西,其中包括index.html
(角项目的根)。为了避免这种情况,我特意告诉nginx将index.html请求路由到angular。
location = index.html {
root /path/to/angular-project/dist; // I've tried using alias as well
proxy_pass http://localhost:4200;
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;
}
这迫使通过index.html专门路由到路由到我的角度应用程序。一旦我做了这个改变,我就能够将特定的链接指向angular,同时保留默认(也称为其他任何)路由到wordpress。但是,这引起了其他错误。所有js / css / etc文件仍然通过wordpress进行路由。这就是我所做的(尽管可能有一种更简单的方法)。
location ~* .(js|css|png|jpg|jpeg|svg|otf)$ {
root /path/to/angular-project/dist;
try_files $uri @wordpress_files;
}
location @wordpress_files {
root /var/www/website.com;
try_files $uri $uri/ $uri$args;
}
完成后,我的网站将首先尝试以角度查找特定文件。如果失败了,它将加载wordpress的版本。如果文件具有相同的名称,则无法处理,但如果是这种情况,则它们应该是相同的版本,因此加载也应该是安全的。