【问题标题】:Nginx single application configNginx 单应用配置
【发布时间】:2014-07-24 03:47:27
【问题描述】:

我正在使用 nginx 编写一个 AngularJS 单页应用程序。

我刚刚从 apache 切换到 nginx,但我无法使我的配置文件正常工作。 我正在尝试将所有内容重写为 index.html 以让 Angular 进行路由。

我的nginx.conf如下:

server {
  index index.html;

  location / {
    expires -1;
    add_header Pragma "no-cache";
    add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
    try_files $uri $uri/ /index.html;
  }
}

是否可以像我的.htaccess 那样将我的nginx.conf 文件放在项目根目录中?

【问题讨论】:

  • 我不明白为什么您的try_files 不起作用,但要回答您的第二个问题,Nginx 不会加载随机放置在目录中的配置文件。也不应该……这是一个严重的性能问题。你可以在启动时为 Nginx 包含任何你想要的 conf 文件。对我来说,我将配置文件放在代码中的 nginx 目录中,并让 Nginx 包含 projects/*/nginx/nginx.conf

标签: angularjs nginx


【解决方案1】:

您不想在项目根目录中使用 nginx.conf,这不是必需的。此外,您不希望直接更改 nginx.conf,而是需要 /etc/nginx/sites-available 中不同网站的特定文件,您可以在 /etc/nginx/sites-enabled 中使用ln 启用这些文件。

就配置而言:

server {
 root /var/www/mysite/; #or whereever your site files are
 index index.html;

 location /{
  try_files $uri $uri/ =404;
 }
}

您缺少告诉 nginx 站点所在位置的根部分。

【讨论】:

    猜你喜欢
    • 2013-09-05
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2022-06-23
    相关资源
    最近更新 更多