如果没有找到nginx服务从静态尝试反向代理

时间:2015-02-17 22:30:29

标签: nginx

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

尝试todo的是,当在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;
    }

}

所以基本上,如果我想让它尝试并将其传递给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;
    }

}

2 个答案:

答案 0 :(得分:13)

您过度思考,单个位置可以处理它然后给它一个后备

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

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

注意:

  1. 不需要第二个根位置,它已经定义了 服务器块
  2. 我已删除$uri/,因为您的服务器中没有index
  3. 我也删除了/index.html,如果您确实要使用它,那么您可能希望将其定义为服务器块中的索引并将$uri/放回

    server {
      index index.html;
      location / {
        try_files $uri $uri/ @proxy;
      }
    }
    
  4. 我不知道app_root在哪里,但我假设它是在其他地方定义的上游。

答案 1 :(得分:-2)

试试这个Mo:

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;
    error_page 404 = @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;
    }
}

location @404 {
    proxy_pass http://app_root;
}

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

}