1

Jetty server runs on localhost:8080 and successfully responses when I make requests via curl (putty):

curl -H "Content-Type: application/json" -X POST -d '{"message":"Hi"}' http://localhost:8080

I have following nginx.conf configuration:

server{
        listen 80;
        server_name 52.27.79.132;
        root /data/www;
        index index.html

        # static files: .png, .css, .js...
        location /static/ {
           root /data/www;
        }

        location ^~/api/*{
            proxy_pass        http://localhost:8080;
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header  Host $http_host;
        }

    }

# include /etc/nginx/sites-enabled/*;

Java servlet runs, when jetty server gets request to "/"

Browser successfully returns index.html page, but when javascript makes AJAX-request to 'http://52.27.79.132/api/' I get 404 error


Does anyone know why ?

1 Answers1

1

The regex is not right in your version. However, you don't actually need regex matching in your case, so you can use this version:

location /api {
    proxy_pass        http://localhost:8080;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
}

If you want to use regex for some reason, the first line should look like this:

location ^~ ^/api/.*

The dot means any character and asterisk means repeat 0 or more times.

In your original location line, you repeated / 0 or more times.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
  • Many thanks! finally I changed location to this one: ' location ^~ /api/ ' – Ildar Zaripov Aug 30 '16 at 20:43
  • Actually you need to use `location ^~ ^/api/`. The location you used will match any URI that contains `/api/` anywhere on it, like `/some/path/api/resource`. The `^` means start of line in regex, which eliminates the problem. – Tero Kilkanen Aug 30 '16 at 21:02