NGiNX 配置为子文件夹中的 PHP 应用程序和 Angular 应用程序提供服务

NGiNX config to serve PHP app and Angular App on Subfolder

我正在寻找一个配置示例来将我的 CentOS 服务器配置为 运行 同一域中的 PHP 和 Angular 应用程序。 PHP 目前运行良好,两个应用程序都将 运行 在普通用户(非 root)帐户下。

我的根 PHP 应用位于 public_html/

我的 Angular 应用程序位于 public_html/app

我们将不胜感激。

人们劫持了这个问题,他们没有提供答案,而是抱怨没有提供示例代码。我最终自己找到了解决方案,所以我想分享它以防有人遇到同样的问题:

在我的具体情况下,我有 PHP 和普通 HTML (Angular) 文件在不同的文件夹中但在相同的 user/domain 下。默认配置适用于 PHP 文件,但是一旦我访问了文件夹 /app,其中我有 Angular 8 应用程序 运行,我收到了 502 错误。为了解决这个问题,我在默认配置文件中添加了以下内容:

location /app/ {
    root /home/YOURUSER/public_html/app/;
    try_files $uri $uri/ /index.html /index.htm /index;
    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;
}
#Angular seems to have issues if you are not specific about CSS and JS files in headers
location ~ \.css {
    add_header Content-Type text/css;
}

location ~ \.js {
    add_header Content-Type application/x-javascript;
}

这似乎现在可以正常工作了。