2015-05-12 34 views
0

我已经在Nginx 1.8中部署了我的Web应用程序,并且还使用Nginx进行了反向代理配置。如何使用Nginx对除了特定URL以外的所有URL进行URL重写?

我想要以下事情发生。我能够实现1 & 2.我怎样才能在Nginx配置中实现5? 3 & 4是场景5的示例场景

  1. URL在浏览器http://localhost - >不得不index.html的显示
  2. URL http://localhost/app - >不得不proxy_pass到http://localhost:8081
  3. URL http://localhost/login - >有显示index.html作为http://localhost
  4. URL http://localhost/dashboard - >纷纷展示index.html作为http://localhost
  5. URL http://localhost/Anything_Other_than_app - >不得不解散玩 index.html作为http://localhost

我的问题是,这个index.html的有一个登录功能,对成功,登录重定向用户到URL http://localhost/dashboard和用户的显示列表。如果有任何用户名被点击,它将重定向到http://localhost/user以显示其详细信息。

用例:现在,如果我直接键入http://localhost/dashboardhttp://localhost/user URL,Nginx的是寻找该文件夹,我的根目录下的索引页,并给予404错误。相反,我想要的是显示Index.html页面,该页面具有根据会话存在或不存在显示用户列表或登录页面的逻辑。

解决方案,我们使用

location = /index.html { 
     root myTestApp; 
     } 
     location ~* .*\.png$ { 
     root myTestApp; 
     } 
     location ~* .*\.jpg$ { 
     root myTestApp;   
     } 
     location ~* .*\.css$ { 
     root myTestApp;   
     } 
     location ~* .*\.gif$ { 
     root myTestApp;   
     } 
     location ~* .*\.js$ { 
     root myTestApp; 
     } 

     location/{ 
      root myTestApp; 
      rewrite ^(.*)$ /index.html;   
     } 

     location /app { 
      proxy_pass http://localhost:8081; 
      proxy_set_header Host $host; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    

     } 
+0

你真的没有CSS,脚本,图片? –

+0

我已经添加了为什么我有这个问题的用例。希望这可以让问题更清楚。 Web应用程序是一个node.js应用程序,其中包含css,images&js文件。 – yathirigan

回答

1

location/{ 
    try_files /index.html =404; 
} 

location /app/ { 
    # proxy to your app 
} 
+0

当我用这个仍然给404.什么可能是错的? **粗体**代码位置/ { root myPOC; index index.html index.htm; try_files index.html = 404; } code ** bold **(myPOC是我在c:\ nginx-1.8.0中的web应用程序文件夹) – yathirigan

+0

您应该总是在'root'指令中使用绝对路径。 –

+1

你在'try_files'中错过了斜线,你不需要'index' –