nginx从静态服务(如果找不到)尝试反向代理

标签 nginx

我有一个使用angularjs开发的应用程序,访问dist/文件夹时将加载整个应用程序。

尝试做的是,当在angularjs上找不到页面时,尝试反向代理时,我尝试执行以下设置,但是nginx不允许在单个块中两次设置相同的位置

server {
    listen 80;
    server_name example.com;
    keepalive_timeout 60;
    client_max_body_size 10M;
    root /var/lib/www/dist;
    charset utf-8;

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

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_buffering off;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        if (!-f $request_filename) {
            proxy_pass http://app_root;
            break;
        }
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /var/lib/app/etc/templates;
    }

}

所以基本上,如果我在angularjs上使用URL 404,那么我希望它尝试将其传递给proxy_pass http://app_root;,那么任何人都可以建议如何实现此设置?

谢谢,

更新

因此,我正在尝试“Mohammad AbuShady”提出的方法,并将我的nginx设置更新为以下设置,但仍然无法正常工作,而是尝试在AngularJS应用中查找页面,而不移至@proxy up_stream设置
upstream app_root {
    server unix:/tmp/app_root.sock fail_timeout=0;
}

server {
    listen 80;
    server_name example.com;
    keepalive_timeout 60;
    client_max_body_size 10M;
    root /var/lib/www/dist;
    charset utf-8;

    location / {
        expires -1;
        add_header Pragma "no-cache";
        add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
        root /var/lib/www/dist;
        try_files $uri$args $uri$args/ $uri/ /index.html @proxy;
    }

    location @proxy {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_buffering off;
        if (!-f $request_filename) {
            proxy_pass http://app_root;
            break;
        }
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /var/lib/app/etc/templates;
    }

}

最佳答案

您考虑不周,单个位置可以处理它,然后进行后备

location / {
  # omitted extra settings
  # check notes below
  try_files $uri @proxy;
}

location @proxy {
  # omitted proxy settings
  proxy_pass http://app_root;
}

笔记:
  • 不需要第二个根目录,它已经在
    服务器块
  • 我已删除$uri/,因为您的服务器中没有index
  • 我还删除了/index.html,如果您想使用它,则可能需要将其定义为服务器块中的索引,然后将$uri/放回
    server {
      index index.html;
      location / {
        try_files $uri $uri/ @proxy;
      }
    }
    
  • 我不知道app_root在哪里,但是我假设它是在其他地方定义的上游。
  • 关于nginx从静态服务(如果找不到)尝试反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28572392/

    相关文章:

    javascript - gzip_static 不适用于 nginx

    尽管有 php.ini 设置,PHP 不记录脚本错误

    docker - 如何使用 nginx 容器代理传递到端口 80 上的节点 docker 容器

    bash - 尝试 cURL 松弛时获取无效负载

    linux - 如何处理 Varnish 堆栈中的 Cookie

    django - Nginx 路由 Django Web App 链接到错误的路径

    redirect - nginx 从客户端浏览器缓存中清除 301 永久

    ruby-on-rails - 空 zip 存档 : Couldn't find template for digesting: projects/repositories/archive

    macos - nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use) 在 mac 上

    node.js - 在本地 - Nginx 容器上的 React 应用程序,具有 Node Express 容器的代理