2015-02-17 43 views
6

我有一個應用程序是使用angularjs開發的,並且在訪問dist /文件夾時加載整個應用程序。nginx服務於靜態如果沒有發現嘗試反向代理

什麼我試圖待辦事項是,當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; 
    } 

} 

所以基本上,如果URL 404'ed on angularjs我想它嘗試並將它傳遞給proxy_pass http://app_root;任何人都可以建議如何實現此設置?

感謝,

UPDATE

所以我想提出的方法,通過「穆罕默德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; 
    } 

} 
+0

那麼當您渲染404頁面時,實際的網址是什麼? – 2015-02-17 23:17:07

回答

8

你思前想,一個地方可以處理它,然後給它一個回退

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是,但我假設它是在其他地方定義的上游。
+0

你好,穆罕默德,我試着接近你提出的上面,它沒有完全解決,我更新了我的問題,並添加了更多來自nginx conf的詳細信息。請問你能指教一下嗎? – 2015-02-19 12:23:08

+0

解釋此行 'try_files $ uri $ args $ uri $ args/$ uri//index.html @proxy;' – 2015-02-19 12:25:10

+0

其靈感來源於此帖https://gist.github.com/cjus/b46a243ba610661a7efb因爲angularjs通過$ uri和$ args爲了呈現index.html,我嘗試了從不同手冊中選擇的幾個方法,而這個方法只適用於其他的手冊,其他方面也是如此。但在刷新頁面時出現問題 – 2015-02-19 12:33:57

-1

試一下這個莫:

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; 
} 

}