使用nginx进行Web应用程序路由

我尝试使用nginx服务webapp,并且一切正常,而我请求的根URL,如http://xxx.xxx.xxx.xxx:8002/ (我没有一个域名,所以IP地址和端口是用过的)

该应用程序有它自己的内部路由,我希望能够从浏览器的地址栏,如http://xxx.xxx.xxx.xxx:8002/profile使用它。

我得到404,因为nginx查找根文件夹中的目录,我猜。

我改变了我的configuration看起来像这样:

server { listen 8002 default_server; server_name _; root /home/ubuntu/sites/mysite-frontend; location ~ /.* { rewrite "/.*" / last; } } 

现在我得到状态代码500,日志显示如下:

2016/09/08 08:36:27 [error] 29869#0:* 3在处理“/”时重写或内部redirect周期,client:yyy.yyy.yyy.yyy,server:_,request:“GET / profile HTTP / 1.1“,主机:”xxx.xxx.xxx.xxx:8002“

我应该如何改变我的configuration,使其工作?

是你的web应用/index.htmlindex指令的默认值是index.html ,这就是默认configuration与/ URI一起工作的原因。

你应该添加一个try_files指令 ,默认的动作是/index.html ,在这种情况下,指向一个不存在的静态文件的所有URI将被路由到你的webapp。

 server { listen 8002 default_server; index index.html; root /home/ubuntu/sites/mysite-frontend; location / { try_files $uri $uri/ /index.html; } } 

我已经明确指出了index指令(但在这种情况下不需要)。