1

I try to serve a webapp with nginx, and everything goes fine while I request root url, like http://xxx.xxx.xxx.xxx:8002/ (I don't have a domain name, so IP address and port are used)

The app has it's own internal routing and I'd like to be able to use it from browser's address bar, like http://xxx.xxx.xxx.xxx:8002/profile.

I get 404, since nginx looks for directories in root folder, I guess.

I changed my config to look like this:

server {
    listen 8002 default_server;
    server_name _;
    root /home/ubuntu/sites/mysite-frontend;

    location ~ /.* {
        rewrite "/.*" / last;
    }
}

and now I get status code 500, and log shows the following:

2016/09/08 08:36:27 [error] 29869#0: *3 rewrite or internal redirection cycle while processing "/", client: yyy.yyy.yyy.yyy, server: _, request: "GET /profile HTTP/1.1", host: "xxx.xxx.xxx.xxx:8002"

How should I change my config in order to make it work?

0

Is your webapp /index.html. The default value for the index directive is index.html, which is why the default configuration works with the / URI.

You should add a try_files directive with the a default action of /index.html, in which case all URIs which point to a non-existent static file will be routed to your webapp.

server {
    listen 8002 default_server;
    index index.html;
    root /home/ubuntu/sites/mysite-frontend;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

I have made the index directive explicit (but it is not necessary in this case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.