【问题标题】:NGiNX config to serve PHP app and Angular App on SubfolderNGiNX 配置为子文件夹上的 PHP 应用程序和 Angular 应用程序提供服务
【发布时间】:2019-12-04 05:21:54
【问题描述】:

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

我的根 PHP 应用程序位于 public_html/

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

任何帮助将不胜感激。

【问题讨论】:

  • 很好的问题。它不像大多数其他人那样过于冗长......美丽而简洁,并且非常容易阅读,因为它也完全适用于我试图做的事情。谢谢!

标签: php angular linux nginx centos


【解决方案1】:

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

在我的具体情况下,我将 PHP 和纯 HTML (Angular) 文件放在不同的文件夹中,但在同一个用户/域下。默认配置适用于 PHP 文件,但是当我访问运行 Angular 8 应用程序的文件夹 /app 时,我收到了 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;
}

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

【讨论】:

  • 谢谢你,不知道nginx很难弄明白。
猜你喜欢
  • 2018-11-23
  • 1970-01-01
  • 2016-06-18
  • 2014-05-08
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
相关资源
最近更新 更多