我已经在Nginx 1.8中部署了我的Web应用程序,并且还使用Nginx进行了反向代理配置。如何使用Nginx对除了特定URL以外的所有URL进行URL重写?
我想要以下事情发生。我能够实现1 & 2.我怎样才能在Nginx配置中实现5? 3 & 4是场景5的示例场景
- URL在浏览器http://localhost - >不得不index.html的显示
- URL http://localhost/app - >不得不proxy_pass到http://localhost:8081
- URL http://localhost/login - >有显示index.html作为http://localhost
- URL http://localhost/dashboard - >纷纷展示index.html作为http://localhost
- URL http://localhost/Anything_Other_than_app - >不得不解散玩 index.html作为http://localhost
我的问题是,这个index.html的有一个登录功能,对成功,登录重定向用户到URL http://localhost/dashboard和用户的显示列表。如果有任何用户名被点击,它将重定向到http://localhost/user以显示其详细信息。
用例:现在,如果我直接键入http://localhost/dashboard或http://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;
}
你真的没有CSS,脚本,图片? –
我已经添加了为什么我有这个问题的用例。希望这可以让问题更清楚。 Web应用程序是一个node.js应用程序,其中包含css,images&js文件。 – yathirigan